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 2005 Forums
 Transact-SQL (2005)
 Count(*)

Author  Topic 

sjesweak
Starting Member

10 Posts

Posted - 2007-07-26 : 12:37:35
Hello -

I have a syntax issue. I have 2 columns in a table. First column is Station ID which has values that range from 1-4. My second column is weight of a substance that ranges from 0-120.

I want do a sql query that returns 3 columns.

Column 1 is the station ID
Column 2 is the Count of each weight that is > 116
Column 3 is the Count of each weight that is < 116

I have a query which can do 2 of the three as shown.

SELECT COUNT(*) AS Expr1, CarbonFillStationBankA
FROM CompleteTable
WHERE (DTEntered BETWEEN DATEADD(hour, 7, @StartDate) AND DATEADD(hour, 15, @StartDate)) AND (CarbonWeight < 116.2)
GROUP BY CarbonFillStationBankA
and this returns

7 1
27 2
3 3
12 4

What do I have to do to get a third count column to show the count of those weights > 116?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-07-26 : 12:49:41
SELECT StationID, SUM(CASE WHEN Substance <= 116 THEN 1 ELSE 0 END),
SUM(CASE WHEN Substance > 116 THEN 1 ELSE 0 END)
FROM Table
GROUP BY StationID
ORDER BY StationID



E 12°55'05.76"
N 56°04'39.42"
Go to Top of Page

sjesweak
Starting Member

10 Posts

Posted - 2007-07-26 : 12:59:13
If thanks were candy bars you would have a fed ex truck with a life time supply on it's way to your doorstep :)
Go to Top of Page

sjesweak
Starting Member

10 Posts

Posted - 2007-07-26 : 13:08:37
How would I format the syntax if I wanted to have all values between a certain date and time with a DTEntered field as the date/time column.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-07-26 : 15:37:21
add the condition to the where clause

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page
   

- Advertisement -