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.
| Author |
Topic |
|
cariduit
Starting Member
7 Posts |
Posted - 2010-02-11 : 00:49:03
|
| Hi all,I've got a problem with the query.I use this query :SELECT a.PartID, b.OtherID, SUM(a.Qty) AS 'Hasil Produksi', '0' AS 'Hasil Repair'FROM Trans_BHPDt201001 AS a INNER JOINMs_Part201001 AS b ON a.PartID = b.PartID INNER JOIN Trans_BHPHd201001 AS c ON a.NoBukti = c.NoBuktiWHERE (a.LocationID LIKE 'WH 2%') AND (c.Tgl BETWEEN '2010/01/01' AND '2010/01/31')GROUP BY a.PartID, b.OtherIDthe result is like this :001 | RI301 | 335 | 0in another side, I also have this query :SELECT d.PartID, f.OtherID, '0' AS 'Hasil Produksi', SUM(d.Qty) AS 'Hasil Repair'FROM Trans_GdgMoveDt201001 AS d INNER JOIN Ms_Part201001 AS f ON d.PartID = f.PartID INNER JOIN Trans_GdgMoveHd201001 AS g ON d.NoBukti = g.NoBuktiWHERE (g.LocationID_From LIKE 'QC N') AND (g.LocationID_To LIKE 'WH 2%') AND (g.Tgl BETWEEN '2010/01/01' AND '2010/01/31')GROUP BY d.PartID, f.OtherIDthe result from this query is 001 | RI301 | 0 | 10how to create the query to make the result like this ? 001 | RI301 | 335 | 10please inform me.Thank you so much |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-02-11 : 01:47:04
|
Must be something like this:SELECT ISNULL(dt1.PartID,dt2.PartID) as PartID, ISNULL(dt1.OtherID,dt2.OtherID) as OtherID, ISNULL(dt1.[Hasil Produksi],0) as [Hasil Produksi], ISNULL(dt2.[Hasil Repair],0) as [Hasil Repair]FROM( SELECT a.PartID, b.OtherID, SUM(a.Qty) AS [Hasil Produksi], '0' AS [Hasil Repair] FROM Trans_BHPDt201001 AS a INNER JOIN Ms_Part201001 AS b ON a.PartID = b.PartID INNER JOIN Trans_BHPHd201001 AS c ON a.NoBukti = c.NoBukti WHERE (a.LocationID LIKE 'WH 2%') AND (c.Tgl BETWEEN '20100101' AND '20100131') GROUP BY a.PartID, b.OtherID) as dt1FULL OUTER JOIN( SELECT d.PartID, f.OtherID, '0' AS [Hasil Produksi], SUM(d.Qty) AS [Hasil Repair] FROM Trans_GdgMoveDt201001 AS d INNER JOIN Ms_Part201001 AS f ON d.PartID = f.PartID INNER JOIN Trans_GdgMoveHd201001 AS g ON d.NoBukti = g.NoBukti WHERE (g.LocationID_From LIKE 'QC N') AND (g.LocationID_To LIKE 'WH 2%') AND (g.Tgl BETWEEN '20100101' AND '20100131') GROUP BY d.PartID, f.OtherID) as dt2ON dt1.PartID = dt2.PartID and dt1.OtherID = dt2.OtherID EDIT: I think a FULL OUTER JOIN is better... No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
cariduit
Starting Member
7 Posts |
Posted - 2010-02-11 : 02:07:54
|
| thank dude..but how about this one?I have my table called "Master Part" where the structure like this :001 | RI301 002 | RI302003 | RI303004 | RI304and also have the table called "Qty_Produksi" where the structure like this :001 | 0002 | 3Also have the table called "Qty_Repair" like this :001 | 5003 | 6I want the final result like this :001 | RI301 | 0 | 5002 | RI302 | 3 | 0003 | RI303 | 0 | 6004 | RI304 | 0 | 0your help is needed.Many thanks. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-02-11 : 02:10:34
|
See my edit in my first answer.I think that should solve it. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
cariduit
Starting Member
7 Posts |
Posted - 2010-02-11 : 02:19:25
|
| Dear web,from my first case, it already solved it, but the problem is, from the query only display the 'RI%' section that only got from the first query.after I try the query, the other 'RI' that not in the list of 'MS_Part' it didn't show up.can you help me? |
 |
|
|
cariduit
Starting Member
7 Posts |
Posted - 2010-02-11 : 02:24:35
|
| webfred, if I want to insert another two select again, how about the syntax?is it like this ?SELECT{}FROM{ SELECT { }}FULL OUTER JOIN{ SELECT { }}FULL OUTER JOIN{ SELECT { }}FULL OUTER JOIN{ SELECT { }}or, something like the else? please help me.. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-02-11 : 02:30:51
|
quote: Dear web,from my first case, it already solved it, but the problem is, from the query only display the 'RI%' section that only got from the first query.after I try the query, the other 'RI' that not in the list of 'MS_Part' it didn't show up.
This?select m.PartID, m.OtherID, isnull(Qty_Produksi.Qty,0) as Qty_Produksi, isnull(Qty_Repair.Qty,0) as Qty_Repairfrom [Master Part] mleft join Qty_Produksi on Qty_Produksi.PartID = m.PartIDleft join Qty_Repair on Qty_Repair.PartID = m.PartID No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-02-11 : 02:38:17
|
quote: Originally posted by cariduit webfred, if I want to insert another two select again, how about the syntax?is it like this ?SELECT{}FROM{ SELECT { }}FULL OUTER JOIN{ SELECT { }}FULL OUTER JOIN{ SELECT { }}FULL OUTER JOIN{ SELECT { }}or, something like the else? please help me..
Yes it is possible like this but it depends...Look here to see the different joins:http://msdn.microsoft.com/en-us/library/ms187518(SQL.90).aspx No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-11 : 02:39:49
|
| Can there be multiple records per PartID in tables like Qty_Produksi,Qty_Repair etc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
cariduit
Starting Member
7 Posts |
Posted - 2010-02-11 : 02:46:02
|
| no webfred,actually like this I have a table called 'MS_Part' where the data like this :001 | RI301002 | RI302003 | RI303004 | RI304005 | RI305the first table called 'Produksi' have the data like this :001 | 10002 | 5001 | 2the second table called 'Repair' have the data like this :001 | 5003 | 6the third table called 'Retur' have the data like this :003 | 2004 | 1and the last table called 'Sales' have the data like this :001 | 5003 | 7the result should be like this :001 | RI301 | 12 | 5 | 0 | 5002 | RI302 | 5 | 0 | 0 | 0003 | RI303 | 0 | 6 | 2 | 7004 | RI304 | 0 | 0 | 1 | 0005 | RI305 | 0 | 0 | 0 | 0need your help so much. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-11 : 02:54:46
|
quote: Originally posted by cariduit no webfred,actually like this I have a table called 'MS_Part' where the data like this :001 | RI301002 | RI302003 | RI303004 | RI304005 | RI305the first table called 'Produksi' have the data like this :001 | 10002 | 5001 | 2the second table called 'Repair' have the data like this :001 | 5003 | 6the third table called 'Retur' have the data like this :003 | 2004 | 1and the last table called 'Sales' have the data like this :001 | 5003 | 7the result should be like this :001 | RI301 | 12 | 5 | 0 | 5002 | RI302 | 5 | 0 | 0 | 0003 | RI303 | 0 | 6 | 2 | 7004 | RI304 | 0 | 0 | 1 | 0005 | RI305 | 0 | 0 | 0 | 0need your help so much.
SELECT m.PartID,m.OtherID,SUM(CASE WHEN Cat=1 THEN TotalQty ELSE 0 END) AS TotalQty1,SUM(CASE WHEN Cat=2 THEN TotalQty ELSE 0 END) AS TotalQty2,SUM(CASE WHEN Cat=3 THEN TotalQty ELSE 0 END) AS TotalQty3,SUM(CASE WHEN Cat=4 THEN TotalQty ELSE 0 END) AS TotalQty4FROM MS_Part mLEFT JOIN (SELECT PartID,SUM(Qty) AS TotalQty,1 AS CatFROM ProduksiGROUP BY PartIDUNION ALLSELECT PartID,SUM(Qty),2FROM RepairGROUP BY PartIDUNION ALLSELECT PartID,SUM(Qty),3FROM ReturGROUP BY PartIDUNION ALLSELECT PartID,SUM(Qty),4FROM SalesGROUP BY PartID)tON t.PartID=m.PartIDGROUP BY m.PartID,m.OtherID ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-02-11 : 03:01:40
|
After each answer you're coming up with a modified question...Maybe visakh can give a better help  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
cariduit
Starting Member
7 Posts |
Posted - 2010-02-11 : 03:02:55
|
quote: Originally posted by visakh16 Can there be multiple records per PartID in tables like Qty_Produksi,Qty_Repair etc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
yes visak.. there could be multiple recoreds in other tables |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-11 : 03:07:29
|
quote: Originally posted by cariduit
quote: Originally posted by visakh16 Can there be multiple records per PartID in tables like Qty_Produksi,Qty_Repair etc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
yes visak.. there could be multiple recoreds in other tables
see below mockup of your scenario with solutionselect * into #ms_partfrom(select '001' as partid,'RI301' as Otherid union allselect '002','RI302' union allselect '003','RI303' union allselect '004','RI304' union allselect '005','RI305' )t select * into #Produksifrom(select '001' as partid,10 AS Qty union allselect '002',5 union allselect '001', 2 )t select * into #Repairfrom(select '001' as partid,5 AS Qty union allselect '003',6 )t select * into #Returfrom(select '003' as partid,2 AS Qty union allselect '004',1 )t select * into #Salesfrom(select '001' as partid,5 AS Qty union allselect '003',7 )t SELECT m.PartID,m.OtherID,SUM(CASE WHEN Cat=1 THEN TotalQty ELSE 0 END) AS TotalQty1,SUM(CASE WHEN Cat=2 THEN TotalQty ELSE 0 END) AS TotalQty2,SUM(CASE WHEN Cat=3 THEN TotalQty ELSE 0 END) AS TotalQty3,SUM(CASE WHEN Cat=4 THEN TotalQty ELSE 0 END) AS TotalQty4FROM #MS_Part mLEFT JOIN (SELECT PartID,SUM(Qty) AS TotalQty,1 AS CatFROM #ProduksiGROUP BY PartIDUNION ALLSELECT PartID,SUM(Qty),2FROM #RepairGROUP BY PartIDUNION ALLSELECT PartID,SUM(Qty),3FROM #ReturGROUP BY PartIDUNION ALLSELECT PartID,SUM(Qty),4FROM #SalesGROUP BY PartID)tON t.PartID=m.PartIDGROUP BY m.PartID,m.OtherIDdrop table #MS_Partdrop table #Produksidrop table #Repairdrop table #Returdrop table #Salesoutput--------------------------------------------001 RI301 12 5 0 5002 RI302 5 0 0 0003 RI303 0 6 2 7004 RI304 0 0 1 0005 RI305 0 0 0 0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
cariduit
Starting Member
7 Posts |
Posted - 2010-02-11 : 03:17:56
|
| dear visak,in your syntax query, the items in MS_part must be list all?how about if I have thousand of items in MS_part? should I list of all? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-11 : 03:23:37
|
quote: Originally posted by cariduit dear visak,in your syntax query, the items in MS_part must be list all?how about if I have thousand of items in MS_part? should I list of all?
sorry didnt get youwhat do you mean by list all?I just showed all the values for mocking up your scenario.the solution is only this partSELECT m.PartID,m.OtherID,SUM(CASE WHEN Cat=1 THEN TotalQty ELSE 0 END) AS TotalQty1,SUM(CASE WHEN Cat=2 THEN TotalQty ELSE 0 END) AS TotalQty2,SUM(CASE WHEN Cat=3 THEN TotalQty ELSE 0 END) AS TotalQty3,SUM(CASE WHEN Cat=4 THEN TotalQty ELSE 0 END) AS TotalQty4FROM #MS_Part mLEFT JOIN (SELECT PartID,SUM(Qty) AS TotalQty,1 AS CatFROM #ProduksiGROUP BY PartIDUNION ALLSELECT PartID,SUM(Qty),2FROM #RepairGROUP BY PartIDUNION ALLSELECT PartID,SUM(Qty),3FROM #ReturGROUP BY PartIDUNION ALLSELECT PartID,SUM(Qty),4FROM #SalesGROUP BY PartID)tON t.PartID=m.PartIDGROUP BY m.PartID,m.OtherID remember to put your actual tables instead of the 3 ones shown above------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|