Sql question 
Author Message
 Sql question
Hello,

I have a sql question.

SELECT C1,COUNT(*) FROM T1
WHERE C2 = 5
AND GROUP BY C1

SELECT C1,COUNT(*) FROM T1
WHERE C2 <> 5
AND GROUP BY C1

These two queries should be in one SQL like Select c1,count(*) as
c2eq5,count(*) as c2ne5 from t1
group by c1

Is is possible in DB2 V5 on os390

Thanks



Tue, 29 Apr 2003 03:00:00 GMT
 Sql question


Quote:
> Hello,

> I have a sql question.

> SELECT C1,COUNT(*) FROM T1
> WHERE C2 = 5
> AND GROUP BY C1

> SELECT C1,COUNT(*) FROM T1
> WHERE C2 <> 5
> AND GROUP BY C1

> These two queries should be in one SQL like Select c1,count(*) as
> c2eq5,count(*) as c2ne5 from t1
> group by c1

> Is is possible in DB2 V5 on os390

Yes. YMMV

SELECT C1, SUM(EQ5) AS C2EQ5, SUM(NE5) AS C2NE5
FROM (SELECT C1, COUNT(*) AS EQ5, 0 AS NE5
              FROM T1
              WHERE C2 = 5
                    UNION ALL
              SELECT C1, 0, COUNT(*)
              FROM T1
              WHERE C2 ^= 5)
GROUP BY C1

You'll most likely have to use the appropriate scalar function on
the COUNT(*)'s and zero literals to fit the expected domain of your
count; in other words coerce the type.

Glenn



Wed, 30 Apr 2003 03:00:00 GMT
 
 [ 2 post ] 

 Relevant Pages 

1. Access question -> SQL question

2. Update question and SQL question

3. Simple SQL Question for the SQL Gurus

4. SQL question (not really sql server programming)

5. SQL Gurus: Time SQL question

6. SQL Server T-SQL Question

7. SQL Question: Best way to pass array data to SQL Server

8. SQL question for SQL Gurus

9. more dynamic SQL-Server SQL question

10. ms-sql sql question, isnull function

11. SQL Q: Everybody loves a good SQL question


 
Powered by phpBB® Forum Software