Connecting Tech Pros Worldwide Help | Site Map

Where clause

  #1  
Old October 27th, 2008, 02:55 PM
JJ297
Guest
 
Posts: n/a
Having problems adding this where clause (where
week_start_date='20081003') I have two dates in the database but I
want the count from only 20081003.

SELECT PAN,
SUM(CASE WHEN Cat=1 THEN 1 ELSE 0 END) AS Drycd1,
SUM(CASE WHEN Cat=2 THEN 1 ELSE 0 END) AS Drycd2,
SUM(CASE WHEN Cat=3 THEN 1 ELSE 0 END) AS Drycd3,
SUM(CASE WHEN Cat=4 THEN 1 ELSE 0 END) AS Drycd4,
SUM(CASE WHEN Cat=5 THEN 1 ELSE 0 END) AS Drycd5,
SUM(CASE WHEN Cat=6 THEN 1 ELSE 0 END) AS Drycd6,
SUM(CASE WHEN Cat=7 THEN 1 ELSE 0 END) AS Drycd7,
COUNT(*) AS CountTotal
FROM

(
SELECT PAN,1 AS cat FROM DryCD1
UNION ALL
SELECT PAN,2 FROM DryCD2
UNION ALL
SELECT PAN,3 FROM DryCD3
UNION ALL
SELECT PAN,4 FROM DryCD4
UNION ALL
SELECT PAN,5 FROM DryCD5
UNION ALL
SELECT PAN,6 FROM DryCD6
UNION ALL
SELECT PAN,7 FROM DryCD7
)t
GROUP BY PAN
  #2  
Old October 27th, 2008, 02:55 PM
Plamen Ratchev
Guest
 
Posts: n/a

re: Where clause


Assuming the column is in the tables you union, then you can add the
WHERE filter to each table, like:

SELECT PAN,1 AS cat FROM DryCD1
WHERE week_start_date='20081003'
UNION ALL ...


Or, you can add it as column to the derived table (you have to add it to
all SELECT statements) and then filter later:

....
FROM (
SELECT PAN,1 AS cat, week_start_date FROM DryCD1
UNION ALL
SELECT PAN,2, week_start_date FROM DryCD2
....) AS T
WHERE week_start_date = '20081003'

--
Plamen Ratchev
http://www.SQLStudio.com
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
using conditional statements in where-clause pwiegers@gmail.com answers 5 February 2nd, 2007 10:45 AM
Calculated Values in a WHERE clause Jim.Mueksch@wellsfargo.com answers 2 January 4th, 2007 02:05 PM
does DB2 support SELECT - INTO statement with ORDER BY clause? GreatAlterEgo answers 26 January 31st, 2006 07:05 PM
group by clause Query help aj70000@hotmail.com answers 2 July 23rd, 2005 08:30 AM
Is it possible to re-reference a column alias from a select clause in another column of the same select clause? malcolm answers 5 July 20th, 2005 04:58 AM