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 |
see199
Starting Member
21 Posts |
Posted - 2007-06-14 : 00:12:54
|
repairJob(repairJobID, description)->('overhaul','blablabla...')->('gearbox','blablabla...')sparePart(sparePartID, description ,cost)->('sp1', 'oil+bolt+...', 100)->('sp2', 'bolt, clamp...', 200)->('sp3', 'protector, cover..', 500)repair(repairID, repairJob, sparePartID, cost, workShopID)->('r001', 'overhaul', 'sp1', 100, 'w001')->('r002', 'overhaul', 'sp2', 200, 'w001')->('r003', 'gearbox', 'sp3', 500, 'w002')->('r004', 'gearbox', 'sp2', 200, 'w002')output:Is it possible to make it? |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
see199
Starting Member
21 Posts |
Posted - 2007-06-14 : 02:36:56
|
is it this code?USE NorthwindGOCREATE TABLE Pivot( Year SMALLINT, Quarter TINYINT, Amount DECIMAL(2,1) )GOINSERT INTO Pivot VALUES (1990, 1, 1.1)INSERT INTO Pivot VALUES (1990, 2, 1.2)INSERT INTO Pivot VALUES (1990, 3, 1.3)INSERT INTO Pivot VALUES (1990, 4, 1.4)INSERT INTO Pivot VALUES (1991, 1, 2.1)INSERT INTO Pivot VALUES (1991, 2, 2.2)INSERT INTO Pivot VALUES (1991, 3, 2.3)INSERT INTO Pivot VALUES (1991, 4, 2.4)GOSELECT Year, SUM(CASE Quarter WHEN 1 THEN Amount ELSE 0 END) AS Q1, SUM(CASE Quarter WHEN 2 THEN Amount ELSE 0 END) AS Q2, SUM(CASE Quarter WHEN 3 THEN Amount ELSE 0 END) AS Q3, SUM(CASE Quarter WHEN 4 THEN Amount ELSE 0 END) AS Q4FROM Northwind.dbo.PivotGROUP BY YearGOfrom my understanding, this is the static column, what if i wan to make it dynamic?which means if the user enter new sparePart, it'll automatically included in the sql. |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-06-14 : 02:40:17
|
See this: [url]http://www.sqlservercentral.com/columnists/plarsson/pivottableformicrosoftsqlserver.asp[/url]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-14 : 02:43:35
|
quote: this is the static column, what if i wan to make it dynamic?
You mean the number of columns may change at runtime ? KH |
|
|
see199
Starting Member
21 Posts |
Posted - 2007-06-14 : 02:53:47
|
quote: Originally posted by harsh_athalye See this: [url]http://www.sqlservercentral.com/columnists/plarsson/pivottableformicrosoftsqlserver.asp[/url]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED"
OMG~ i cant understand |
|
|
see199
Starting Member
21 Posts |
Posted - 2007-06-14 : 02:54:52
|
quote: Originally posted by khtan
quote: this is the static column, what if i wan to make it dynamic?
You mean the number of columns may change at runtime ? KH
yup. and i saw an example in the post b4 urs. but i hardly understand |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
|
see199
Starting Member
21 Posts |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-14 : 08:51:34
|
quote: Originally posted by see199
quote: Originally posted by harsh_athalye See this: [url]http://www.sqlservercentral.com/columnists/plarsson/pivottableformicrosoftsqlserver.asp[/url]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED"
OMG~ i cant understand
Then read about Cross-tab Reports in sql server help fileMadhivananFailing to plan is Planning to fail |
|
|
|
|
|
|
|