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 |
|
daniel50096230
Yak Posting Veteran
99 Posts |
Posted - 2011-09-01 : 20:47:22
|
| Dear all,I have a set of records here:Product_ID Product_Group StatusNY11-1 NY11 ANY11-2 NY11 ANY12-1 NY12 INY12-2 NY12 ANY13-1 NY13 AThe results that I expect is as below:Product_Group Product_ID StatusNY11 NY11-1 ANY11 NY11-2 ANY12 NY12-2 ANY13 NY13-1 AI tried to group by only when the product exist more than one time and there are different status within same product group. How can I do that? Because i tried to use group by but it'll group by all no matter there are different status within same product group or don't have. |
|
|
ben_53
Yak Posting Veteran
67 Posts |
Posted - 2011-09-01 : 21:31:17
|
| "I tried to group by only when the product exist more than one time and there are different status within same product group"but in your expected result set you have same status within same product group ?NY11 NY11-1 ANY11 NY11-2 ANY12 NY12-2 ANY13 NY13-1 A |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-01 : 23:54:58
|
| based on your very limited sample data and given output, what i feel is its just a matter of adding a simple filter asWHERE Status='A'to your select query which selects data from table------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
daniel50096230
Yak Posting Veteran
99 Posts |
Posted - 2011-09-02 : 00:18:54
|
| Let me be more specified.Product_ID Product_Group Status QtyNY11-1 NY11 A 1NY11-2 NY11 A 2NY12-1 NY12 I 2NY12-2 NY12 A 3NY13-1 NY13 A 4The results that I expect is as below:Product_Group Product_ID Status QtyNY11 NY11-1 A 1NY11 NY11-2 A 2NY12 NY12-2 A 5NY13 NY13-1 A 4What I need is sum the Qty if the product group is same but status is not same. Let see product group NY12, there are two products within this group with one status is 'I' and another is 'A'. So this record should be sum together into one. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-02 : 00:24:49
|
sounds like below as per latest explanationSELECT t.Product_Group, t.Product_ID, t.Status,t.Qty + COALESCE(t1.AltQty,0)FROM Table tOUTER APPLY (SELECT SUM(Qty) AS AltQty FROM Table WHERE Product_Group = t.Product_Group AND Status <> 'A' )t1WHERE t.Status='A' ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-02 : 00:25:57
|
| One doubt, can there more than one record for the same Product_Group with same status value?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|