| Author |
Topic |
|
~J~
Starting Member
2 Posts |
Posted - 2005-12-15 : 12:59:04
|
| Hi,I was wondering if someone could help me achieve a view I'm after creating.I currently have a table which is as follows...[CODE] P 44100 SAL ADM 2005 11 1000 200 P 44100 SAL ADM 2005 10 500 90 P 44100 SAL ADM 2005 9 75 34 P 44100 SAL ADM 2004 11 800 1300 P 44100 SAL ADM 2004 10 500 400 P 44100 SAL ADM 2004 9 50 100[/CODE] The last 2 columns are of interest to me. What I want to achieve (using the above as an example) are 3 rows which read as follows:[CODE]11 1000 200 800 130010 500 90 500 4009 75 34 50 100[/CODE] What I'm basically trying to achieve is a view that has the last 2 columns for the current year and all periods, with the last 2 columns of the previous year (and relating period) added to the end.I really can't get my head around how to do this, but I'm sure it can be done!Any help I'd really appreciate it.TIA |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-12-15 : 13:31:06
|
Here's one way to do it:set nocount onselect 'P' c1, 44100 c2, 'SAL' c3, 'ADM' c4, 2005 c5, 11 c6, 1000 c7, 200 c8into #tempinsert #tempselect 'P', 44100, 'SAL', 'ADM', 2005, 10, 500, 90 union allselect 'P', 44100, 'SAL', 'ADM', 2005, 9, 75, 34 union allselect 'P', 44100, 'SAL', 'ADM', 2004, 11, 800, 1300 union allselect 'P', 44100, 'SAL', 'ADM', 2004, 10, 500, 400 union allselect 'P', 44100, 'SAL', 'ADM', 2004, 9, 50, 100select c.c6 ,c.c7 ,c.c8 ,p.c7 ,p.c8from #temp cleft join #temp p on p.c6 = c.c6 and p.c5 + 1 = c.c5where p.c6 is not nullorder by c.c6 desc/*output:c6 c7 c8 c7 c8 ----------- ----------- ----------- ----------- ----------- 11 1000 200 800 130010 500 90 500 4009 75 34 50 100*/ Be One with the OptimizerTG |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-12-15 : 14:08:32
|
| [code]USE NorthwindGOSET NOCOUNT ONCREATE TABLE myTable99( Col1 char(1) , Col2 int , Col3 char(3) , Col4 char(3) , [year] char(4) , Col5 int , Col6 int , Col7 int)GOINSERT INTO myTable99(Col1, Col2, Col3, Col4, [year], Col5, Col6, Col7)SELECT 'P', 44100, 'SAL', 'ADM', '2005', 11, 1000, 200 UNION ALLSELECT 'P', 44100, 'SAL', 'ADM', '2005', 10, 500, 90 UNION ALLSELECT 'P', 44100, 'SAL', 'ADM', '2005', 9, 75, 34 UNION ALLSELECT 'P', 44100, 'SAL', 'ADM', '2004', 11, 800, 1300 UNION ALLSELECT 'P', 44100, 'SAL', 'ADM', '2004', 10, 500, 400 UNION ALLSELECT 'P', 44100, 'SAL', 'ADM', '2004', 9, 50, 100GOSELECT Keys.Col5, Col6_2004, Col7_2004, Col6_2005, Col7_2005 FROM (SELECT DISTINCT Col5 FROM myTable99) AS Keys LEFT JOIN (SELECT Col5, Col6 AS Col6_2004, Col7 AS Col7_2004 FROM myTable99 WHERE [year] = '2004') AS Y2004 ON Keys.Col5 = Y2004.Col5 LEFT JOIN (SELECT Col5, Col6 AS Col6_2005, Col7 AS Col7_2005 FROM myTable99 WHERE [year] = '2005') AS Y2005 ON Keys.Col5 = Y2005.Col5GOSET NOCOUNT OFFDROP TABLE myTable99GO[/code]Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
~J~
Starting Member
2 Posts |
Posted - 2005-12-15 : 15:44:07
|
| That's great, many thanks for the help, really appreciate it. |
 |
|
|
|
|
|