Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Ex:-
Item Name       QTY          Price           Category
 
ABC             10           200              X
ABC             20           300              Y
ABC             40           900              Z


Need Result :- 

Item Name       Qty                Price           Category

ABC             30(10+20)        500(300+200)         X+Y
ABC             40                   900              Z

In this case X,Y,Z is category and we use group by clause on this. kindly suggest the query for this.
Posted
Comments
Rahul Kumar 16-Jul-14 5:33am    
You need to do something like this
SELECT column1, column2, (column1+column2) AS SumColumn FROM Table1
ArunRajendra 16-Jul-14 5:58am    
Guess the question is typo error. What he means is adding rows.
Rahul Kumar 16-Jul-14 6:15am    
Yes you are right :)
ArunRajendra 16-Jul-14 5:34am    
Why only x&y is clubbed and not z? what the condition for clubbing.
ashish 29 16-Jul-14 6:16am    
Example :- X is credit customer and Y is Deposite customer and Z is cash customer. We know that total qty of a item which is purchase (credit and deposite (in 1 column)) and cash for seperate colmn.

1 solution

Very simplistically ...

You can do this by running a query that gets all of the category X and Y rows, a separate query that gets the category Z rows and union them together. You don't want to group by category, you will need to group by the Item Name. For example:
SQL
SELECT [Item Name],Sum(Qty) AS QTY ,Sum(Price) AS PRICE,'X+Y' as Category
FROM test
WHERE Category IN ('X','Y')
GROUP BY [Item Name]
UNION ALL
SELECT [Item Name],Sum(Qty),Sum(Price),'Z' as Category
FROM test
WHERE Category = 'Z'
GROUP BY [Item Name]

Which gives these results
ITEM NAME	QTY	PRICE	CATEGORY
ABC 		30 	500 	X+Y 
ABC 		40 	900 	Z 

However, seeing your comment
Quote:
We know that total qty of a item which is purchase (credit and deposite (in 1 column)) and cash for seperate colmn.
then it seems that you want the results further combined into something like
ITEM NAME	QTYXY	PRICEXY	QTYZ PRICEZ
ABC 		30 	500 	40   900 
so you can join the results of the query above to itself based on [Item Name] to get both sets of results in a single row e.g.
WITH TempTable AS (
  SELECT Item,Sum(Qty) AS QTY ,Sum(Price) AS PRICE,'X+Y' as Category
  FROM test
  WHERE Category IN ('X','Y')
  GROUP BY Item
  UNION ALL 
  SELECT Item,Sum(Qty),Sum(Price),'Z' as Category
  FROM test
  WHERE Category = 'Z'
  GROUP BY Item
  )
SELECT T1.ITEM, T1.QTY AS QtyXY, T1.PRICE AS PriceXY, 
      T2.QTY as QtyZ, T2.PRICE as PriceZ
FROM TempTable T1 
INNER JOIN TempTable T2 ON T1.Item=T2.Item AND T2.Category='Z'
WHERE T1.Category = 'X+Y'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900