There is a table with the following fields:
id,year,UserCode,A2,A3,B2,B3
Usercode is a varchar fields whereas the other fields are integrers.
Let's say this is what the table contains at present:
id year Usercode A2 A3 B2 B3
1 2005 2BM 12 7 9 11
2 2002 BB1 87 98 4 32
3 1999 3NS 45 32 88 12
...
The question is, how can I produce something like the following table using the above table:
The idea is to show the final table (at the very bottom) on a web page. So I am thinking if I get the table right in sql server then the rest is fine.
2BM BB1 3NS
A2 12 87 45
A3 7 98 32
B2 9 4 88
B3 11 32 12
Since I am using sql server 2000, I do not think I can use something like the pivot sql functionality as sql 2005 allows.
In addition to the above I would like to go one step further to have a sql to produce the following result (Notice the extra texts):
2BM BB1 3NS
user info
positive nums
A2 12 87 45
yesterdays info
negative values
A3 7 98 32
contact manage
B2 9 4 88
contact method
B3 11 32 12
Thanks

sql 2000 table format
Asuranceturix
Here is the solution for the first part:
CREATE TABLE test
(
id int PRIMARY key,
year char(4),
usercode char(3),
a2 int,
a3 int,
b2 int,
b3 int
)
INSERT INTO test
SELECT 1, '2005', '2BM', 12, 7, 9, 11
UNION ALL
SELECT 2, '2002', 'BB1', 87, 98, 4, 32
UNION ALL
SELECT 3, '1999', '3NS', 45, 32, 88, 12
go
SELECT code,
max( CASE WHEN usercode = '2BM' THEN value ELSE -1 end) AS '2BM',
max( CASE WHEN usercode = 'BB1' THEN value ELSE -1 end) AS 'BB1',
max( CASE WHEN usercode = '3NS' THEN value ELSE -1 end) AS '3NS'
FROM (SELECT usercode, 'A2' AS code, a2 AS value
FROM test
UNION all
SELECT usercode, 'A3' AS code, a3
FROM test
UNION all
SELECT usercode, 'B2' AS code, b2
FROM test
UNION all
SELECT usercode, 'B3' AS code, b3
FROM test) AS pivoted
GROUP BY code
For the second part, if you really want to do stuff like that, use a temp table and add a sorting column. Then add rows for the text in the code column and set values so it will sort right. I wouldn't suggest this as a great idea, though I do something like it when producing scripts.
Jeff Maxton
Summarizing Data Using ROLLUP
The ROLLUP operator is useful in generating reports that contain subtotals and totals. The ROLLUP operator generates a result set that is similar to the result sets generated by the CUBE operator. For more information, see Summarizing Data Using CUBE.
The differences between CUBE and ROLLUP are:
For example, a simple table Inventory contains:
This query generates a subtotal report:
If the ROLLUP keyword in the query is changed to CUBE, the CUBE result set is the same, except these two additional rows are returned at the end:
The CUBE operation generated rows for possible combinations of values from both Item and Color. For example, not only does CUBE report all possible combinations of Color values combined with the Item value Chair (Red, Blue, and Red + Blue), it also reports all possible combinations of Item values combined with the Color value Red (Chair, Table, and Chair + Table).
For each value in the columns on the right in the GROUP BY clause, the ROLLUP operation does not report all possible combinations of values from the column (or columns) on the left. For example, ROLLUP does not report all the possible combinations of Item values for each Color value.
The result set of a ROLLUP operation has functionality similar to that returned by a COMPUTE BY; however, ROLLUP has these advantages:
MPAZ
Solved.
Thanks
JackNet
try using "with cube" and " rollup".
Summarizing Data Using CUBE
The CUBE operator generates a result set that is a multidimensional cube. A multidimensional cube is an expansion of fact data, or data that records individual events. The expansion is based on columns that the user wants to analyze. These columns are called dimensions. The cube is a result set containing a cross tabulation of all the possible combinations of the dimensions.
The CUBE operator is specified in the GROUP BY clause of a SELECT statement. The select list contains the dimension columns and aggregate function expressions. The GROUP BY specifies the dimension columns and the keywords WITH CUBE. The result set contains all possible combinations of the values in the dimension columns, along with the aggregate values from the underlying rows that match that combination of dimension values.
For example, a simple table Inventory contains:
This query returns a result set that contains the Quantity subtotal for all possible combinations of Item and Color:
Here is the result set:
The following rows from the result set are of special interest:
This row reports a subtotal for all rows having the value Chair in the Item dimension. The value NULL is returned for the Color dimension to show that aggregate reported by the row includes rows with any value of the Color dimension.
This row is similar, but reports the subtotal for all rows having Table in the Item dimension.
This row reports the grand total for the cube. Both the Item and Color dimensions have the value NULL showing that all values of both dimensions are summarized in the row.
These two rows report the subtotals for the Color dimension. Both have NULL in the Item dimension to show that the aggregate data came from rows having any value for the Item dimension.
Using GROUPING to Distinguish Null Values
The null values generated by the CUBE operation present a problem: How can a NULL generated by the CUBE operation be distinguished from a NULL returned in the actual data This is achieved using the GROUPING function. The GROUPING function returns 0, if the column value came from the fact data, and 1 if the column value is a NULL generated by the CUBE operation. In a CUBE operation, a generated NULL represents all values. The SELECT statement can be written to use the GROUPING function to substitute the string ALL in place of any generated NULL. Because a NULL from the fact data indicates the data value is unknown, the SELECT can also be coded to return the string UNKNOWN in place of any NULL from the fact data. For example:
Multidimensional Cubes
The CUBE operator can be used to generate n-dimensional cubes, or cubes with any number of dimensions. A single dimension cube can be used to generate a total, for example:
This SELECT statement returns a result set showing both the subtotals for each value of Item and the grand total for all values of Item:
SELECT statements that contain a CUBE with many dimensions can generate large result sets, because these statements generate rows for all combinations of the values in all the dimensions. These large result sets may contain too much data to be easily read and understood. One solution to this problem is to put the SELECT statement into a view:
The view can then be used to query only the dimension values of interest: