Click here to Skip to main content
15,886,742 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i have a simple query,by executing this query i am getting following output

MSIL
AcId AcNm         Adress   City  NetAmount VatPercentage   VatAmount

56  Rahul Pharma  JM Road  Pune 2596999.80          0.00   2596999.81
56  Rahul Pharma  JM Road  Pune  479500.00          5.00    479500.00
56  Rahul Pharma  JM Road  Pune    2753.73         12.50      2753.73


in this case the AcId,AcNm,Address,City this coloumn are repeated thrice as there are three differant NetAmounts, vat percentages,VatAmounts .I am trying to get all data in a single row instead of three row
MSIL
AcId AcNm  Adress City NetAmount1 VatPercentage1  VatAmount1
NetAmount2 VatPercentage2  VatAmount2  NetAmount3 VatPercentage3   VatAmount3

56  Rahul Pharma  JM Road  Pune 2596999.80 0.00  2596999.81 79500.00     5.00 479500.00 2753.73  12.50  2753.73



is this possible by using CASE in query, How to Do this Pls help
Posted
Updated 23-Dec-10 2:02am
v3

I believe you want PIVOT[^]
 
Share this answer
 
Comments
thatraja 23-Dec-10 13:07pm    
good answer
Although it's not possible in the general case, you can do it when you know the maximum number of items an account can possibly get. For example, if you know that the number is three or less, you can combine windowing functions of SQL Server with the generic GROUP BY to get your results.

Here's a quick example (in your case, be prepared to write a lot more SQL)
SQL
create table t (id int, name varchar(max), amt decimal(13, 10))
To oversimplify, in my example name uniquely identifies an account, and id is a surrogate key. The select for up to three items looks as follows:
SQL
select name, max(amt1) as amt1, max(amt2) as amt2, max(amt3) as amt3 from
(select
    name
,   case ROW_NUMBER() OVER(PARTITION BY name ORDER BY id) when 1 then amt else null end as amt1
,   case ROW_NUMBER() OVER(PARTITION BY name ORDER BY id) when 2 then amt else null end as amt2
,   case ROW_NUMBER() OVER(PARTITION BY name ORDER BY id) when 3 then amt else null end as amt3
from t
) as ttt
group by name
If the data in the table looks like this
MSIL
1   xyz 11.0000000000
2   xyz 22.0000000000
3   xyz 33.0000000000
4   abc 44.4000000000
5   abc 55.5000000000

query result will look like this:
MSIL
abc 44.4000000000   55.5000000000   NULL
xyz 11.0000000000   22.0000000000   33.0000000000
If a name has more than three amounts associated with it, the data for rows for and over will be dropped.

Edit: as Mark pointed out in his answer above, you can use pivot to simplify the query a lot. It will also let you change the number of columns more easily:
SQL
SELECT name, [1] as amt1, [2] as amt2, [3] as amt3
FROM (SELECT name, ROW_NUMBER() OVER(PARTITION BY name ORDER BY id) as sequence, amt FROM t) AS src
PIVOT (max(amt) FOR sequence IN ([1], [2], [3])) AS pvt
 
Share this answer
 
v2
Hi
SQL
Use Union All like

Select  AcId, AcNm, Adress, City, NetAmount, VatPercentage, VatAmount

From table name Where your condition

Union All

Select  AcId, AcNm, Adress, City, NetAmount, VatPercentage, VatAmount
From table name Where your condition


...
...
...

Is this what you were looking for?

Thanks
 
Share this answer
 
v3
Comments
[no name] 23-Dec-10 11:18am    
Inefficient query
simply user union all operator of sql server it will do this work for u
 
Share this answer
 
Comments
[no name] 23-Dec-10 11:16am    
Union will not add columns to the rows as requested

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