Thursday, February 16, 2012

Cross Tab Query

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
Daud Akbar
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
|||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,
"Nigel Rivett" wrote:

> 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
>
|||On Sun, 6 Feb 2005 22:29:02 -0800, Daud wrote:

>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.
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
(Remove _NO_ and _SPAM_ to get my e-mail address)

No comments:

Post a Comment