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 |
|
Maccaman
Starting Member
16 Posts |
Posted - 2009-10-17 : 07:14:47
|
| Hello,I was wondering if someone could help write a query.I have the following table titled 'Fish_data'Month Fish.No Size.Cat AliveJANUARY 1 A YesJANUARY 2 A YesJANUARY 3 A YesJANUARY 4 A YesJANUARY 5 A YesJANUARY 6 A YesJANUARY 7 A YesJANUARY 8 A YesJANUARY 9 A YesJANUARY 10 A YesFEBRUARY 1 A NoFEBRUARY 2 A NoFEBRUARY 3 A YesFEBRUARY 4 A YesFEBRUARY 5 A YesFEBRUARY 6 B YesFEBRUARY 7 B YesFEBRUARY 8 B YesFEBRUARY 9 B YesFEBRUARY 10 B YesMARCH 1 A NoMARCH 2 A NoMARCH 3 B YesMARCH 4 B YesMARCH 5 B YesMARCH 6 B YesMARCH 7 B YesMARCH 8 B YesMARCH 9 B NoMARCH 10 B Nowhich describes the size category for 10 individual fish and if they are alive in a certain month. I want to create 2 queries that I just don't know how to create yet. In the first query, I want to count the number of fish in each category in each month, but when a fish is dead then that particular fish's category becomes 'D'. For example, in January fish number 1 would be category 'A', but in February fish number 1 would be in category 'D' not 'A' as it appears in the data. So from this data set, the query would return:Month Category Category.CountJanuary A 10January B 0January D 0February A 3February B 5February D 2March A 0March B 6March D 4The second query is a lot more difficult (I think anyway). Ideally this query would return the number of movements from each category in one month, to another or the same category in the next month. So from this data I would like to have a query that produces the following results:FromMonth ToMonth FromSize ToSize TransitionsJanuary February A A 5January February A B 3January February A D 2January February B A 0January February B B 0January February B D 0January February D A 0January February D B 0January February D D 0February March A A 0February March A B 3February March A D 0February March B A 0February March B B 3February March B D 2February March D A 0February March D B 0February March D D 2I am a novice SQL user so I don't know how complicated it would be to create such a query, but any help would be greatly appreciated. Thanks,Maccaman"I know the human being and fish can co-exist peacefully" - George W. Bush. Obviously Mr Bush has never been fishing with me :) |
|
|
Maccaman
Starting Member
16 Posts |
Posted - 2009-10-26 : 01:21:20
|
| Okay,I have the first query.....SELECT Month, Cat, Count(*) AS CntFROM (SELECT Month, CASE WHEN Alive = 'No' THEN 'D' ELSE Cat END AS Cat FROM Fish_data) AS fishGROUP BY Month, CatBut im really stuck on the second query....Does anyone have any suggestions? Perhaps even creating a view to add columns to the table and then writing a query on top of that to achieve this?Thanks to anyone in advance who can help. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
|
|
|
|
|