Author |
Topic |
pmwhelan
Starting Member
5 Posts |
Posted - 2010-08-25 : 06:11:49
|
HiI have two tables 'Stations' and 'AnnualData'.Stations is a list of numerous stations around the world that hold temperature data.AnnualData is the data that these stations have recorded.I have Stations.NightLight which is either 'A', 'B' or 'C'and Stations.StationType which is either 'R', 'U' or 'S'Would it be possible for every year (AnnualData.Year) to find out the amount of stations that are 'A' and 'R'?Then the ones that are 'A' and 'U' etcThanks |
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2010-08-25 : 06:51:50
|
sample data and output should really make this one easy for people to help you with answers. |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-08-25 : 06:53:59
|
Seems you need to use PIVOT. Search for the same in this forumMadhivananFailing to plan is Planning to fail |
 |
|
pmwhelan
Starting Member
5 Posts |
Posted - 2010-08-25 : 07:45:15
|
Stations--------StationCode | NightLight | StationType1 | A | U2 | B | S3 | A | S4 | C | U5 | C | S6 | A | R7 | A | S8 | B | U9 | A | S10 | C | R11 | C | U12 | A | SAnnualData----------StationCode | Year1 | 20102 | 20103 | 20104 | 20105 | 20106 | 20107 | 20108 | 20109 | 201010 | 201011 | 201012 | 2009 (so these is no row here for 2010)So in this case I'd like to return 9 columnsAR | AU | AS | BR | BU | BS | CR | CU | CS1 | 1 | 3 | 0 | 1 | 1 | 1 | 2 | 1thanks |
 |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-08-25 : 08:13:50
|
If those are the only 9 combinations then you can use case whenSELECT SUM(CASE WHEN NightLight = 'A' and StationType = 'R' THEN 1 ELSE 0 END) as AR, SUM(CASE WHEN NightLight = 'A' and StationType = 'S' THEN 1 ELSE 0 END) as [AS], SUM(CASE WHEN NightLight = 'A' and StationType = 'U' THEN 1 ELSE 0 END) as AUetcJimEveryday I learn something that somebody else already knew |
 |
|
pmwhelan
Starting Member
5 Posts |
Posted - 2010-08-25 : 08:45:06
|
Thanks jimf that's brilliant!It's almost perfect.It returns one row.I would like it to return a row for every 'year' in my annual data.So it should return ....So in this case I'd like to return 9 columnsYear | AR | AU | AS | BR | BU | BS | CR | CU | CS2010 | 1 | 1 | 3 | 0 | 1 | 1 | 1 | 2 | 12009 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0thanks so much |
 |
|
michael.appleton
Posting Yak Master
160 Posts |
Posted - 2010-08-25 : 08:49:32
|
This should really be done in the front end. As you can see, SQL can be quite messy for changing the presentation of data. |
 |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-08-25 : 09:00:38
|
You can join to that tableSELECT a.[Year],SUM(CASE WHEN NightLight = 'A' and StationType = 'R' THEN 1 ELSE 0 END) as AR, SUM(CASE WHEN NightLight = 'A' and StationType = 'S' THEN 1 ELSE 0 END) as [AS], SUM(CASE WHEN NightLight = 'A' and StationType = 'U' THEN 1 ELSE 0 END) as AUFROM Stations sINNER JOIN AnnualData a ON s.StationCode = a.StationCodeGROUP BY a.[year] JimEveryday I learn something that somebody else already knew |
 |
|
pmwhelan
Starting Member
5 Posts |
Posted - 2010-08-25 : 10:55:03
|
thanks jimf you're a lifesaverthat worked a charm! |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-08-25 : 10:57:29
|
Just remember, if you get a new combination of {NightLight, StationType}you will have to add a new line to the "SUM(CASE..." part of the query. N 56°04'39.26"E 12°55'05.63" |
 |
|
|