Re: SQL Query
- From: Hugo Kornelis <hugo@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 10 Jan 2006 23:00:18 +0100
On Mon, 9 Jan 2006 00:40:02 -0800, Mukesh Garg wrote:
>Hi all, i am not able to make up a query according to my need. can anyone
>help. Following is my simple query.
>
>select udf_name [Severity],st_name [Status], count(2) [count]
>from bugs, statuses, user_defined_attribute
>where udf_id = bg_user_defined_attribute
>and bg_status = st_id
>group by udf_name,st_name
>order by udf_name
>
>i want it to be shown in following manner: distinct "udf_name" is shown as
>rows and distinct "st_name" as columns and corrospond to them come the count
>for respective
>
>say
> st_name[1] st_name[2]
>udf_name 1 2
>
>and so on.
>
>Please help
>Regards
>Mukesh
Hi Mukesh,
It's easier to help you if you post the structure of your tables (as
CREATE TABLE statements), some sample data (as INSERT statements) and
expected results.
Based on what you write and a fair number of assumptions, try something
like this:
SELECT udf_name,
SUM(CASE WHEN st_name = '1' THEN 1 ELSE 0 END) AS "st_name[1]",
SUM(CASE WHEN st_name = '2' THEN 1 ELSE 0 END) AS "st_name[2]"
FROM bugs, statuses, user_defined_attribute
WHERE udf_id = bg_user_defined_attribute
AND bg_status = st_id
GROUP BY udf_name
If this doesn't do what you want, then check out www.aspfaq.com/5006.
--
Hugo Kornelis, SQL Server MVP
.
- Prev by Date: Re: Conditional 'Order By' statement
- Next by Date: Re: Is there any way to make this query faster?
- Previous by thread: Re: Conditional 'Order By' statement
- Next by thread: Re: Is there any way to make this query faster?
- Index(es):
Relevant Pages
|
|