Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need to find solution to this self-join problem

Author  Topic 

gaitchis
Starting Member

5 Posts

Posted - 2011-10-26 : 23:37:08
I have a query that is not acting as it needs to. It is conducting a self-join based on 2 fields and then finding the difference on 2 other fields. However when run, the resultset will list much more that what I am requiring. Running SQL Server 2008 r2.

SELECT dbo.tvMAST_STKO_STPO_Sel.MATNR, dbo.tvMAST_STKO_STPO_Sel.IDNRK, dbo.tvMAST_STKO_STPO_Sel.WERKS AS Plant1,
dbo.tvMAST_STKO_STPO_Sel.STLAN AS BOMUsagePlant1, dbo.tvMAST_STKO_STPO_Sel.STLNR AS BillOfMaterialPlant1,
dbo.tvMAST_STKO_STPO_Sel.STLAL AS AltBOMPlant1, dbo.tvMAST_STKO_STPO_Sel.POSNR AS OperationPlant1,
dbo.tvMAST_STKO_STPO_Sel.MENGE AS QuantityPlant1, tvMAST_STKO_STPO_Sel_1.WERKS AS Plant2,
tvMAST_STKO_STPO_Sel_1.STLAN AS BOMUsagePlant2, tvMAST_STKO_STPO_Sel_1.STLNR AS BillOfMaterialPlant2,
tvMAST_STKO_STPO_Sel_1.STLAL AS AltBOMPlant2, tvMAST_STKO_STPO_Sel_1.POSNR AS OperationPlant2,
tvMAST_STKO_STPO_Sel_1.MENGE AS QuantityPlant2, dbo.tvMAST_STKO_STPO_Sel.BU
FROM dbo.tvMAST_STKO_STPO_Sel INNER JOIN
dbo.tvMAST_STKO_STPO_Sel AS tvMAST_STKO_STPO_Sel_1 ON dbo.tvMAST_STKO_STPO_Sel.MATNR = tvMAST_STKO_STPO_Sel_1.MATNR AND
dbo.tvMAST_STKO_STPO_Sel.WERKS <> tvMAST_STKO_STPO_Sel_1.WERKS AND
dbo.tvMAST_STKO_STPO_Sel.IDNRK = tvMAST_STKO_STPO_Sel_1.IDNRK AND
dbo.tvMAST_STKO_STPO_Sel.MENGE <> tvMAST_STKO_STPO_Sel_1.MENGE

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-27 : 02:30:37
very little information provided for suggesting something as we dont know how data is stored in your tables and also what output you're expecting and what you got instead etc.
Please provide some sample data from tables (only required columns) and then explain what you're trying to achieve.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -