Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Need help with Count function and temporary tables
I have data like this in a two column temporary table -

ID               Age

23586         3
23586         3
23586         2
23586         2
23586        1
23586        1
23586        1
23586        1
23586        1

I need to create a temporary table that look like this:

ID            Age1           Age2        Age3       Age4

23586       5                 2              2            0

However, what I get is this:

23586       5                NULL       NULL       NULL
23586      NULL            2            NULL       NULL
23586      NULL           NULL       2             NULL

Here is the query that I am using...
select managed_object_id, (select count(Age) where Age = 1) As Age1,
(select count(Age) where Age = 2) as Age2,
(select count(Age) where Age = 3) as Age3,
(select count(Age) where Age = 4) as Age4
into #enhancementCount from #enhancements
group by managed_object_id, Age

Where's my mistake?

Thanks-
Danielle


Report this thread to moderator Post Follow-up to this message
Old Post
Danielle
11-22-05 01:24 AM


Re: Need help with Count function and temporary tables
declare @v table (ids int, age int)

INSERT INTO @v (ids, age)
VALUES (23586, 3)
INSERT INTO @v (ids, age)
VALUES (23586, 3)
INSERT INTO @v (ids, age)
VALUES (23586, 2)
INSERT INTO @v (ids, age)
VALUES (23586, 2)
INSERT INTO @v (ids, age)
VALUES (23586, 1)
INSERT INTO @v (ids, age)
VALUES (23586, 1)
INSERT INTO @v (ids, age)
VALUES (23586, 1)
INSERT INTO @v (ids, age)
VALUES (23586, 1)
INSERT INTO @v (ids, age)
VALUES (23586, 1)


SELECT v.ids
, COUNT(CASE WHEN age = 1 THEN 1 END) AS Age1
, COUNT(CASE WHEN age = 2 THEN 1 END) AS Age2
, COUNT(CASE WHEN age = 3 THEN 1 END) AS Age3
, COUNT(CASE WHEN age = 4 THEN 1 END) AS Age4
FROM @v v
GROUP BY ids


Report this thread to moderator Post Follow-up to this message
Old Post
getinked
11-22-05 01:24 AM


Re: Need help with Count function and temporary tables
your problem w/ your query is you do not want to group by the age =)


Report this thread to moderator Post Follow-up to this message
Old Post
getinked
11-22-05 01:24 AM


Re: Need help with Count function and temporary tables
getlinked -

You are a dream! Can you explain the logic a bit? Why does the CASE
statement work ? And is the implied ELSE of the CASE statements the '0'
that I see in the output table?

Thanks! :-)

Danielle


Report this thread to moderator Post Follow-up to this message
Old Post
Danielle
11-22-05 01:24 AM


Re: Need help with Count function and temporary tables
Its not so much in the case statement.  The group by clause is where
the logic was out of place in your query.  if you add the age field to
the group by in my query you will get the result set you had previous.
In the case statement if you dont specify a value for the other records
it just skips them, but the way i wrote it, if yoiu have say age 19 you
will get two records so you have to specify all the ages.  if you want
an all inclusive statement to cover x age you would have to write it a
little different.

cheers


Report this thread to moderator Post Follow-up to this message
Old Post
getinked
11-22-05 01:24 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 02:48 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006