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 |
|
ms
Starting Member
11 Posts |
Posted - 2008-10-20 : 05:40:17
|
| Could someone please help me with this. I'm currently using hash temp table to do some select and doing some computation with it. Is there any way i could transfer this data into a view so that I can do pivot reports in excel. I'm trying to see if i can avoid using a db table for the same.Thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-20 : 05:42:37
|
| you can. just use population query for your hash table as the view definition. for more details post query used for populating hash table. |
 |
|
|
ms
Starting Member
11 Posts |
Posted - 2008-10-20 : 06:19:10
|
| Here's the codeDROP TABLE #temp1SELECT [Code1] as [Prov], [Code2] as [Spec],Prov=' ',Spec=' ', COUNT(CASE month([Start Date]) WHEN 4 THEN [NN_NO] ELSE NULL END) AS April, COUNT(CASE month([Start Date]) WHEN 5 THEN [NN_NO] ELSE NULL END) AS May, COUNT(CASE month([Start Date]) WHEN 6 THEN [NN_NO] ELSE NULL END) AS Jun, COUNT(CASE month([Start Date]) WHEN 7 THEN [NN_NO] ELSE NULL END) AS Jul, COUNT(CASE month([Start Date]) WHEN 8 THEN [NN_NO] ELSE NULL END) AS Aug, COUNT(CASE month([Start Date]) WHEN 9 THEN [NN_NO] ELSE NULL END) AS Sep, COUNT(CASE month([Start Date]) WHEN 10 THEN [NN_NO] ELSE NULL END) AS Oct, COUNT(CASE month([Start Date]) WHEN 11 THEN [NN_NO] ELSE NULL END) AS Nov, COUNT(CASE month([Start Date]) WHEN 12 THEN [NN_NO] ELSE NULL END) AS Dec, COUNT(CASE month([Start Date]) WHEN 1 THEN [NN_NO] ELSE NULL END) AS Jan, COUNT(CASE month([Start Date]) WHEN 2 THEN [NN_NO] ELSE NULL END) AS Feb, COUNT(CASE month([Start Date]) WHEN 3 THEN [NN_NO] ELSE NULL END) AS Mar, COUNT(dbo.In_LIVE.[NN_NO]) AS Total,To_NN=0,INTO #temp1FROM dbo.In_LIVEWHERE (LEN(dbo.In_LIVE.[NN_NO]) <> 0)GROUP BY [Prov], [Spec]UPDATE #temp1set Prov= B.TrFrom #temp1 A, Prov BWhere A.[Code1] = B.IDUPDATE #temp1Set Spec= B.SpFrom #temp1 A, Sc BWhere A.[Code2] = B.IdDECLARE @To_NN intSET @To_NN = 0UPDATE #temp1Set @To_NN = To_NN= (SELECT count([NN_NO]) FROM dbo.In_LIVE B WHERE B.[Code1] =#temp1.[Prov] AND B.[Code2] =#temp1.[Spec])SELECT * FROM #temp1ORDER BY [Prov Code],[Spec Code])THanks |
 |
|
|
darkdusky
Aged Yak Warrior
591 Posts |
Posted - 2008-10-20 : 06:58:49
|
| You could do it as a function - you can query like the VIEW you asked for but usually run faster:CREATE FUNCTION [dbo].[fnName] ()RETURNS TableAS Return (SELECT [Code1] as [Prov],[Code2] as [Spec],Prov=' ',Spec=' ',COUNT(CASE month([Start Date]) WHEN 4 THEN [NN_NO] ELSE NULL END) AS April, COUNT(CASE month([Start Date]) WHEN 5 THEN [NN_NO] ELSE NULL END) AS May, COUNT(CASE month([Start Date]) WHEN 6 THEN [NN_NO] ELSE NULL END) AS Jun, COUNT(CASE month([Start Date]) WHEN 7 THEN [NN_NO] ELSE NULL END) AS Jul, COUNT(CASE month([Start Date]) WHEN 8 THEN [NN_NO] ELSE NULL END) AS Aug, COUNT(CASE month([Start Date]) WHEN 9 THEN [NN_NO] ELSE NULL END) AS Sep, COUNT(CASE month([Start Date]) WHEN 10 THEN [NN_NO] ELSE NULL END) AS Oct, COUNT(CASE month([Start Date]) WHEN 11 THEN [NN_NO] ELSE NULL END) AS Nov, COUNT(CASE month([Start Date]) WHEN 12 THEN [NN_NO] ELSE NULL END) AS Dec, COUNT(CASE month([Start Date]) WHEN 1 THEN [NN_NO] ELSE NULL END) AS Jan, COUNT(CASE month([Start Date]) WHEN 2 THEN [NN_NO] ELSE NULL END) AS Feb, COUNT(CASE month([Start Date]) WHEN 3 THEN [NN_NO] ELSE NULL END) AS Mar, COUNT(dbo.In_LIVE.[NN_NO]) AS Total,To_NN=0FROM dbo.In_LIVEWHERE (LEN(dbo.In_LIVE.[NN_NO]) <> 0)GROUP BY [Prov],[Spec])The updates of temp table - use CASE statements instead. |
 |
|
|
|
|
|
|
|