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.
| Author |
Topic |
|
EvilTadpole
Starting Member
9 Posts |
Posted - 2006-04-03 : 17:20:41
|
| I have a table that includes four date fields. I'm trying to write a statement that will assign a 0 or 1 to each date field if a date is supplied (0 = no date, 1 = date), then I would like to add the the 0 and 1's togethor to determine for each set of items how many dates are supplied. Below is where I started. I need to take a count of rows, and group by typeid with only the total amount of dates supplied for each.select count(itemno)Items, typeid, case when date1 = '00000000' then 0 else 1 end + case when date2 = '00000000' then 0 else 1 end + case when date3 = '00000000' then 0 else 1 end + case when date4 = '00000000' then 0 else 1 end from producttablegroup by typeidWhen I run this I get:Server: Msg 8120, Level 16, State 1, Line 1Column 'producttable.date1' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.Server: Msg 8120, Level 16, State 1, Line 1Column 'producttable.date2' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.Server: Msg 8120, Level 16, State 1, Line 1Column 'producttable.date3' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.Server: Msg 8120, Level 16, State 1, Line 1Column 'producttable.date4' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.I want the end result to look like this:Items typeid (datecounts)45632 1 03545 1 14558 1 264654 1 34654 1 446218 2 07834 2 1etc etc. Please help!!ThanksErika |
|
|
EvilTadpole
Starting Member
9 Posts |
Posted - 2006-04-03 : 18:17:47
|
| I figured it out, by putting the case statements in the group by clause too it was able to work... Yeah for me!!! |
 |
|
|
|
|
|
|
|