Re: SQL Query



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
.



Relevant Pages

  • Tabel refresh
    ... I have a table which list the statuses of certain tasks. ... The database ... their status updates. ... query output while I have the query in table/grid view. ...
    (microsoft.public.access.modulesdaovba)
  • Re: "Identical" query SQL Throws Error
    ... I can only guess that the issue was one of the many bugs in the JET ... query engine, so I can't pinpoint it for you. ... The second query of course got that value from the first query, ...
    (microsoft.public.access.modulesdaovba)
  • Re: Obfuscator and VCL.NET Apps
    ... Do they scan the bugs by the QC ... > number, by rating, by voting, or something else? ... I also query on an area and some days I just query ...
    (borland.public.delphi.non-technical)
  • Re: Posible bugs in MSDE 2000 Service packs
    ... | remove the AND E.BARCODE_ID LIKE '%' in MSDE with SP ... | considering that with MSDE RTM i can run my queries ... But, if it is a bugs, i hope microsoft can ... If part of your query is AND E.BARCODE_ID LIKE '%' then an item where BARCODE_ID ...
    (microsoft.public.sqlserver.msde)
  • Re: SQL query in web reporting
    ... You have a few bugs: ... You are referening two tables that I do not know about: ... >I have written this query, and I keep getting this error message when I try ... Prev by Date: ...
    (microsoft.public.sms.admin)