Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Need query help

Author  Topic 

sbt1
Yak Posting Veteran

89 Posts

Posted - 2005-01-27 : 07:12:39
I have data in a table that I'm trying to get a summary of. It's designed to simply show whether or not there is anything scheduled for each customer in each of the 12 months of the year.

The records have fields for a Customer name, plus each month of the year (which contain either 0 or some integer). There may be multiple records for each Customer.

The data looks like this:

Customer Jan Feb Mar Apr ...
Alpha 0 2 1 0
Beta 3 0 3 2
Beta 0 1 1 0
Beta 0 0 3 2
Charlie 1 1 0 1

What I'm trying to do is get the query to return this ('x's or some symbol to indicate that there is something > 0 for a customer in each month):
Alpha x x
Beta x x x x
Charlie x x x

Here's the query I have so far:
SELECT DISTINCT
Customer,
CASE Jan WHEN '' THEN '' ELSE '*' END,
CASE Feb WHEN '' THEN '' ELSE '*' END,
CASE Mar WHEN '' THEN '' ELSE '*' END,
CASE Apr WHEN '' THEN '' ELSE '*' END,
CASE May WHEN '' THEN '' ELSE '*' END,
CASE Jun WHEN '' THEN '' ELSE '*' END,
CASE Jul WHEN '' THEN '' ELSE '*' END,
CASE Aug WHEN '' THEN '' ELSE '*' END,
CASE Sep WHEN '' THEN '' ELSE '*' END,
CASE Oct WHEN '' THEN '' ELSE '*' END,
CASE Nov WHEN '' THEN '' ELSE '*' END,
CASE Dec WHEN '' THEN '' ELSE '*' END
FROM ProfileDB

but what it's doing is returning a separate line for each corresponding line in the data (using the example above, it would give me 3 rows for Beta.

How can I fix this? Thx in advance for any help.

JimL
SQL Slinging Yak Ranger

1537 Posts

Posted - 2005-01-27 : 07:34:07

Select Custname,
Jan1 = case when Sum(jan) > 0 then
Then 'X'
Else Cast(0 as varchar(1))
end
feb1 = case when Sum(feb) > 0 then
Then 'X'
Else Cast(0 as varchar(1))
end
mar1 = ..............................

from yourtable
group by Custname

Jim
Users <> Logic
Go to Top of Page
   

- Advertisement -