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 |
cidr2
Starting Member
28 Posts |
Posted - 2009-02-27 : 05:56:50
|
Hi there,OK, I believe there probably wont be a way to do this. I have a database with a table and two of the columns have only a set amount of data. one row has 4 consistant values (for Brevity, I'll just make them fruit - Oranges, Apples, Pears and Bananas) the other column, again, has a consistant amont of values (SuperMart, JollyGrocers, FruitMans)There is another column that holds costs so the table would look like this in the dbFruit-----------Market---------CostApples--------SuperMart---------$1Bananas-----SuperMart---------$2Oranges------SuperMart---------$3Pears----------SuperMart---------$1Apples--------JollyGrocers------$3Bananas-----JollyGrocers------$2Oranges-----JollyGrocers------$2I'd like to be able to produce the results like below. The reason for this is so that it facilitates the frontend design(which is InfoPath). Below is how I need it to be laid out in the forms. The frontend forms have to be able to submit values to the database, therefore it has to be updatable in anywhich way. :Fruit------SuperMart-----JollyGrocers-----FruitMansApples--------$1----------------$2-----------------$3Banans-------$2----------------$3-----------------$3Oranges------$3----------------$1-----------------$7Pears----------$1----------------$3-----------------£2The fruit and the Market values are consistant and will hardly(if ever) expand. So really it's the cost column values that are always being added to.I've been advised that SQL analysis services could do this? Is this possible? is an updatable Pivot table possible?If anyone has a solution, I'd be gratful.Cheers |
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-27 : 07:20:07
|
see this sample exampleCREATE TABLE Pivot_SampleEx( YEAR SMALLINT, QUARTER TINYINT, AMOUNT DECIMAL(2,1))INSERT INTO Pivot_SampleEx VALUES (1990,1,1.1)INSERT INTO Pivot_SampleEx VALUES (1990,2,1.2)INSERT INTO Pivot_SampleEx VALUES (1990,3,1.3)INSERT INTO Pivot_SampleEx VALUES (1990,4,1.4)INSERT INTO Pivot_SampleEx VALUES (1991,1,2.1)INSERT INTO Pivot_SampleEx VALUES (1991,2,2.2)INSERT INTO Pivot_SampleEx VALUES (1991,3,2.3)INSERT INTO Pivot_SampleEx VALUES (1991,4,2.4)INSERT INTO PIVOT_SampleEx VALUES (2002,3,5.2)INSERT INTO PIVOT_SampleEx VALUES (2002,4,5.4)-- using pivot functionSELECT YEAR,[1] AS Q1,[2] AS Q2,[3] AS Q3,[4] AS Q4FROM (SELECT * FROM Pivot_SampleEx) AS PPIVOT ( SUM(AMOUNT) FOR QUARTER IN ([1],[2],[3],[4]))AS PVT--dynamic cross tabSELECT 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 Pivot_SampleExGROUP BY YEAR |
 |
|
Mangal Pardeshi
Posting Yak Master
110 Posts |
Posted - 2009-02-27 : 07:44:41
|
Try this--Create sampletableCREATE TABLE Fruites (Fruit varchar(100),Market varchar(100),Cost int)-- Load sample dataINSERT INTO Fruites SELECT'Apples', 'SuperMart', 1 UNION ALL SELECT 'Bananas', 'SuperMart',2UNION ALL SELECT'Oranges', 'SuperMart',3UNION ALL SELECT'Pears', 'SuperMart', 1UNION ALL SELECT'Apples', 'JollyGrocers',3UNION ALL SELECT'Bananas', 'JollyGrocers',2UNION ALL SELECT'Oranges', 'JollyGrocers',2-- QuerySELECT Fruit, [SuperMart],[JollyGrocers], [FruitMans]FROM FruitesPIVOT(MAX(COST)FOR Market IN([SuperMart],[JollyGrocers], [FruitMans]))PVT Mangal Pardeshihttp://mangalpardeshi.blogspot.com |
 |
|
cidr2
Starting Member
28 Posts |
Posted - 2009-02-27 : 10:12:42
|
Thanks both for your input on this.When I creat a data connection from the frontend to the database, and, subsequently the Pivot table, I wont be able to update them. Even in directly from SQL this doesn't seeem to be possible.Do you know if there's any way of making the Pivot or Cross Tab updatable?Thanks again |
 |
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2009-02-27 : 11:05:08
|
You should really use a stored procedure to update the base table.If you still want to do this then create a view and use an INSTEAD OF trigger.CREATE TABLE Fruits( Fruit varchar(20) NOT NULL ,Market varchar(20) NOT NULL ,Cost money NOT NULL)INSERT INTO FruitsSELECT 'Apples', 'SuperMart', 1 UNION ALLSELECT 'Apples', 'JollyGrocers', 2 UNION ALLSELECT 'Apples', 'FruitMans', 3 UNION ALLSELECT 'Bananas', 'SuperMart', 2 UNION ALLSELECT 'Bananas', 'JollyGrocers', 3 UNION ALLSELECT 'Bananas', 'FruitMans', 3 UNION ALLSELECT 'Oranges', 'SuperMart', 3 UNION ALLSELECT 'Oranges', 'JollyGrocers', 1 UNION ALLSELECT 'Oranges', 'FruitMans', 7 UNION ALLSELECT 'Pears', 'SuperMart', 1 UNION ALLSELECT 'Pears', 'JollyGrocers', 3 UNION ALLSELECT 'Pears', 'FruitMans', 2GOCREATE VIEW vFruitsASSELECT Fruit, SuperMart, JollyGrocers, FruitMansFROM Fruits PIVOT ( MAX(COST) FOR Market IN(SuperMart, JollyGrocers, FruitMans) ) PGOCREATE TRIGGER vFruits_tuON vFruitsINSTEAD OF UPDATEASSET NOCOUNT ONUPDATE FSET Cost = X.CostFROM Fruits F JOIN ( SELECT I.Fruit ,CASE N.N WHEN 1 THEN 'SuperMart' WHEN 2 THEN 'JollyGrocers' WHEN 3 THEN 'FruitMans' END AS Market ,CASE N.N WHEN 1 THEN I.SuperMart WHEN 2 THEN I.JollyGrocers WHEN 3 THEN I.FruitMans END AS Cost FROM inserted I JOIN deleted D ON I.Fruit = D.Fruit CROSS JOIN ( SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 ) N(N) ) X ON F.Fruit = X.Fruit AND F.Market = X.MarketWHERE F.Cost <> X.CostGOSELECT *FROM vFruitsUPDATE vFruitsSET SuperMart = 5WHERE Fruit = 'Apples'SELECT *FROM vFruits |
 |
|
|
|
|
|
|