Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All
I have a table like below
HTML
Rack    Cell    Qty
A1	1	50
A2	2	20
A3	3	70
B1	1	80
B2	3	90
C1	1	56
C3	3	77
C4	4	400
D1	5	55
D2	6	66

After executing the following Store Procedure

SQL
ALTER PROCEDURE [dbo].[SP_TestForAccess]
AS
BEGIN
DECLARE @query NVARCHAR(4000)
DECLARE @cols NVARCHAR(2000)
SELECT @cols = COALESCE(@cols +',['+ cell +']','['+ cell +']') FROM (SELECT DISTINCT cell FROM TestForAccess)as T
PRINT @cols
SET @query ='select rack,'+@cols+' FROM (select rack,cell,qty FROM TestForAccess)AS D pivot(sum(qty) for cell in ('+@cols+')) as P;'
--EXEC SP_EXECUTESQL @query
PRINT @query
EXECUTE(@query)
END



I am getting the following table
Here I am changing the cell value into columns
HTML
Rack    1       2       3       4       5       6   
A1	50	NULL	NULL	NULL	NULL	NULL
A2	NULL	20	NULL	NULL	NULL	NULL
A3	NULL	NULL	70	NULL	NULL	NULL
B1	80	NULL	NULL	NULL	NULL	NULL
B2	NULL	NULL	90	NULL	NULL	NULL
C1	56	NULL	NULL	NULL	NULL	NULL
C3	NULL	NULL	77	NULL	NULL	NULL
C4	NULL	NULL	NULL	400	NULL	NULL
D1	NULL	NULL	NULL	NULL	55	NULL
D2	NULL	NULL	NULL	NULL	NULL	66


Now my intention is to make group to the Rack column like below
HTML
     Rack    1       2       3       4       5       6
     A1    50  NULL    NULL    NULL    NULL    NULL
A    A2    NULL    20  NULL    NULL    NULL    NULL
     A3        NULL    NULL    70  NULL    NULL    NULL

     B1    80  NULL    NULL    NULL    NULL    NULL
B    B2    NULL    NULL    90  NULL    NULL    NULL

     C1    56  NULL    NULL    NULL    NULL    NULL
C    C3    NULL    NULL    77  NULL    NULL    NULL
     C4    NULL    NULL    NULL    400 NULL    NULL

     D1    NULL    NULL    NULL    NULL    55  NULL
D    D2    NULL    NULL    NULL    NULL    NULL    66


What should I change the above store procedure to get like the above table
Thanks to All
Posted
Updated 8-Aug-12 2:33am
v2
Comments
[no name] 8-Aug-12 8:25am    
Look up "group by"

1 solution

see example given below
SQL
select RackGrp as RackGrp, Rack as Rack from
(
select 'a1' as Rack,substring('a1',1,1) as RackGrp
union all
select 'a2' as Rack,substring('a2',1,1) as RackGrp
union all
select 'b1' as Rack,substring('b1',1,1) as RackGrp
) as a

Happy Coding!
:)
 
Share this answer
 
v2
Comments
Christian Amado 8-Aug-12 9:51am    
Nice code!
Aarti Meswania 8-Aug-12 9:52am    
thank you! :)
IndrajitDasgupat 9-Aug-12 0:40am    
Hi
I am little new in SQL if give little more information about your code then it will help me lot to solve the issue
Thanks
Aarti Meswania 9-Aug-12 0:46am    
you have column 'Rack' format is 'a1','a2'...,'b1'...
now you want group-'a','b'... and want to make 'Rack' column as subgroup.
so, using 'substring()' function of sql I have separated 'a' from 'a1',
Input='a1' -> apply function substring('a1',1,1) as b -> output ='a'
this way you will get first character as maingroup & rack-column will be subgroup of it.
IndrajitDasgupat 9-Aug-12 1:19am    
Yes it is coming like below
Rackgrp Rack
a a1
a a2
b b1

But My requirement Like below
Rackgrp Rack
a1
a a2
a3

b1
b b2

Thanks for your helping attitude

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900