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)
 View Question

Author  Topic 

a1234
Starting Member

1 Post

Posted - 2008-07-21 : 01:49:53
I have to write a view in sql server 2005 on
a single table that looks like this:
ID(int),
Type(int)
Status(int),
Date1 (date),
Date2 (date)
the view must return:
1. foreach type in the table - count of all the items with status = 1
2. foreach type in the table - the max of date1 with status = 2
2. foreach type in the table - the max of date1 with status = 3

the tpye can be 1..100
status 1..50

How to write this view?
Thanks!!!

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-07-21 : 01:55:17
[code]select Type,
sum(case when Status = 1 then 1 else 0 end) as [count of all the items with status = 1],
max(case when Status = 2 then Date1 end) as [the max of date1 with status = 2],
max(case when Status = 3 then Date1 end) as [the max of date1 with status = 3]
from yourtable
group by Type[/code]




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -