all groups > sql server mseq > february 2005 >
You're in the

sql server mseq

group:

Cross Tab Query


Cross Tab Query Daud
2/3/2005 3:55:01 AM
sql server mseq:
How can I cross-tab the query it was simple in MS-Access but i don't know how
to do it in SQL. the thing i want is as follows:

Simple Query

Item Year Qty
A 1991 10
B 1991 15
C 1991 20

Cross-Tab Query (Items)
ITEMS
Year A B C
1991 10 15 20
--
RE: Cross Tab Query Nigel Rivett
2/6/2005 10:29:02 AM
select year ,
A = (select QTY from tbl t2 where t.year = t2.year and item = 'A') ,
B = (select QTY from tbl t2 where t.year = t2.year and item = 'B') ,
C = (select QTY from tbl t2 where t.year = t2.year and item = 'C')
from tbl t
group by year
order by year

RE: Cross Tab Query Daud
2/6/2005 10:29:02 PM
Than u very much dear u were the only who replied my and understand the
issue. But unfortunately u have give the example for static entry I need
dynamic one in the form that whatever records are entered in the table in the
form they autometically comes in this way.
The table name is T_GCombDetail and the fields are:
Comb T3 Per
1 80286 50
1 37395 50
2 80286 100
3 37385 100
4 80286 30
4 37395 70

I need the result in this way after quering
Comb 80286 37395
1 50 50
2 100
3 100
4 30 70

Waiting for your response,
With Regards,


[quoted text, click to view]
Re: Cross Tab Query Hugo Kornelis
2/7/2005 12:35:01 PM
[quoted text, click to view]

Hi Daud,

There is no easy way to do this in SQL Server. Since this is basically a
formatting issue, the obvious recommendation is to do this at the client,
which is the "proper" place for formatting in a tiered architecture.

If you really must do this at the server, you'll either have to use
dynamic SQL to generate a query like the one Nigel suggests, with the
"correct" values taken from your table, or you can use a third party
product, like RAC4SQL (www.rac4sql.com). Or you could wait for the release
of SQL Server 2005 - I've read that this version will provide crosstab
functionality.

Best, Hugo
--

AddThis Social Bookmark Button