| Author |
Topic |
|
nirene
Yak Posting Veteran
98 Posts |
Posted - 2007-08-04 : 21:22:49
|
| Hai Everyone, I have a table with data like thisTran_no Description Debit Credit1 SW 10001 BR1 5001 BR2 2001 BR3 3002 SQ 5002 BW1 2002 BW2 2002 BW3 100 Now what I wanna do this that Tran_no Val1 Val2 Val3 Val41 1000 500 200 3002 500 200 200 100Thanks in advanceNirene |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-08-05 : 03:56:31
|
1) Search for PIVOT/CROSS TAB REPORT queries2) The SQL Server 2005 and later built-in PIVOT keyword E 12°55'05.25"N 56°04'39.16" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-08-06 : 02:26:57
|
| orRead about Cross-tab reports in sql server help fileMadhivananFailing to plan is Planning to fail |
 |
|
|
nirene
Yak Posting Veteran
98 Posts |
Posted - 2007-08-06 : 03:38:47
|
| Hai,Where to search for Cross Tab Report Query. |
 |
|
|
nirene
Yak Posting Veteran
98 Posts |
Posted - 2007-08-06 : 03:39:52
|
| Im using SQL Server 2000 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-08-06 : 04:06:16
|
quote: Originally posted by nirene Hai,Where to search for Cross Tab Report Query.
In SQL Server help fileMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
nirene
Yak Posting Veteran
98 Posts |
Posted - 2007-08-07 : 09:06:57
|
| Hai, Can you give a select query statement to do thisCos it is very urgent for my project.Nirene |
 |
|
|
sbalaji
Starting Member
48 Posts |
Posted - 2007-08-08 : 06:08:53
|
| you can try this http://www.sql.co.il/ug/16/Dynamic%20Crosstab.txt. |
 |
|
|
nirene
Yak Posting Veteran
98 Posts |
Posted - 2007-08-08 : 06:41:19
|
| Hello Balaji, The url doesnt work,could you just give the content.Nirene |
 |
|
|
sbalaji
Starting Member
48 Posts |
Posted - 2007-08-09 : 01:45:56
|
| i think u r realy having a bad time.CREATE PROC sp_CrossTab @table AS sysname, -- table to crosstab @onrows AS nvarchar(128), -- grouping key values (on rows) -- can be column or computation, -- e.g., CustomerID, MONTH(OrderDate) @onrowsalias AS sysname = NULL, -- alias for grouping column, -- NULL = use original col name @oncols AS nvarchar(128), -- destination columns (on columns), -- can be column or computation, -- e.g., EmployeeID, YEAR(OrderDate) @sumcol AS sysname = NULL -- Data cells, -- NULL = count rows, -- <value> = column to sumASDECLARE @sql AS varchar(8000), @NEWLINE AS char(1)SET @NEWLINE = CHAR(10)-- step 1: beginning of SQL string---- start generating the SQL string,-- starting with the grouping key as the first column.-- use an alias for that column, if one was providedSET @sql = 'SELECT' + @NEWLINE + ' ' + @onrows + CASE WHEN @onrowsalias IS NOT NULL THEN ' AS ' + @onrowsalias ELSE '' END-- step 2: storing keys in a temp table---- generate another SELECT statement-- which stores the unique values of the-- grouping column/ computation in a temp tableCREATE TABLE #keys(keyvalue nvarchar(100) NOT NULL PRIMARY KEY)DECLARE @keyssql AS varchar(1000)SET @keyssql = 'INSERT INTO #keys ' + 'SELECT DISTINCT CAST(' + @oncols + ' AS nvarchar(100)) ' + 'FROM ' + @tableEXEC (@keyssql)-- step 3: middle part of SQL string---- concatenate each key value as a new column-- based on the unique keys of-- the grouping column/computations which were-- stored in the temp table #keysDECLARE @key AS nvarchar(100)SELECT @key = MIN(keyvalue) FROM #keysWHILE @key IS NOT NULLBEGIN SET @sql = @sql + ',' + @NEWLINE + ' SUM(CASE CAST(' + @oncols + ' AS nvarchar(100))' + @NEWLINE + ' WHEN N''' + @key + ''' THEN ' + CASE WHEN @sumcol IS NULL THEN '1' ELSE @sumcol END + @NEWLINE + ' ELSE 0' + @NEWLINE + ' END) AS c' + @key SELECT @key = MIN(keyvalue) FROM #keys WHERE keyvalue > @keyEND-- step 4: end of SQL string---- concatenate the last part of the SQL string-- including the FROM clause-- and the grouping column/computationSET @sql = @sql + @NEWLINE + 'FROM ' + @table + @NEWLINE + 'GROUP BY ' + @onrows + @NEWLINE + 'ORDER BY ' + @onrows-- PRINT @sql + @NEWLINE -- For debugEXEC (@sql)GO--Courtesy:http://www.sql.co.il/ug/16/Dynamic%20Crosstab.txt. |
 |
|
|
|