| Author |
Topic |
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-05-29 : 05:23:16
|
| TableAID NO Quantity Co3 Co411 1 33 23 1911 2 45 56 211 3 56 79 111 2 78 54 622 1 87 45 333 2 65 84 944 1 45 13 544 2 789 500 1644 3 87 21 2844 2 4 13 30I wish to show out sum(quantity) and max(Co3) when NO = 2 and max(Co4) when NO = 3 how can i do it??sql code---------SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[TableA]( [ID] [int] NULL, [NO] [int] NULL, [Quantity] [int] NULL, [Co3] [int] NULL, [Co4] [nchar](10) NULL) ON [PRIMARY]INSERT INTO [dbo].[TableA]([ID], [NO], [Quantity], [Co3], [Co4])SELECT 11, 1,33,23,19 UNION ALLSELECT 11,2,45,56,2 UNION ALLSELECT 11,3,56,19,1 UNION ALLSELECT 11,2,78,54,6 UNION ALLSELECT 22,1,87,45,3 UNION ALLSELECT 33,2,65,84,9 UNION ALLSELECT 44,1,45,13,5 UNION ALLSELECT 44,2,789,500,16 UNION ALLSELECT 44,3,87,21,28 UNION ALLSELECT 44,2,4,13,30GoMy test query which does workselect m.ID, a.Quantity, b.Co3, c.Co4 from(((select ID, sum(quantity) as quantityfrom TableAgroup by ID)afull join(SELECT ID, MAX(Co3) as Co3from TableAwhere No = 2group by ID)b on a.ID=b.ID)full join (SELECT ID, MAX(Co4) as Co4from TableAwhere No IN 3group by ID)c on b.ID=c.ID)Result enquiry11,171,56,122,1,87,45,333,2,65,84,944,925,21,30 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-29 : 05:30:14
|
[code]SELECT ID, SUM(Quantity), MAX(CASE WHEN No = 2 THEN Co3 ELSE NULL END), MAX(CASE WHEN No = 3 THEN Co4 ELSE NULL END)FROM Table1GROUP BY ID[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-05-29 : 05:33:49
|
| urs result...11 290 56 1 22 87 NULL NULL33 65 84 NULL44 925 500 28 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-29 : 05:40:24
|
Yes. There are no records for ID 22 with "NO" either 2 or 3.Beacuse you result is inverted for ID 44. You take max of Co4 then no = 2,but for ID 11 you take max of co4 when no = 3! E 12°55'05.63"N 56°04'39.26" |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-05-29 : 05:41:37
|
| woops i forgotten...thx peso u r the great...hop u get urs mvp xD |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-29 : 05:44:41
|
Thank you.I don't think your query works. It doesn't parse correctly.quote: Originally posted by waterduck My test query which does workselect m.ID, a.Quantity, b.Co3, c.Co4 from(((select ID, sum(quantity) as quantityfrom TableAgroup by ID)afull join(SELECT ID, MAX(Co3) as Co3from TableAwhere No = 2group by ID)b on a.ID=b.ID)full join (SELECT ID, MAX(Co4) as Co4from TableAwhere No IN 3group by ID)c on b.ID=c.ID)
E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-29 : 05:48:39
|
If I change your query so it does parse, you get same result as my suggestion.select a.ID, a.Quantity, b.Co3, c.Co4from ( select ID, sum(quantity) as quantity from @TableA group by ID ) AS afull join ( SELECT ID, MAX(Co3) as Co3 from @TableA where No = 2 group by ID ) AS b on a.ID=b.IDfull join ( SELECT ID, MAX(Co4) as Co4 from @TableA where No = 3 group by ID ) AS c on b.ID=c.ID E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|
|
|