you see, its web-based, where the details displayed would depend on whatever data type (display format) the users choose, be it in details, as summary, or as grand sum. when (data type = detail) is chosen, the list is over 800++. there4 users r requestin 4 the 3 columns 2b distinct so that they can b able see the available categories easily... (the categories r the 3 keys). currently im using the codes below... it works but im hopin 4 a simplier control...
---------------------------------------------------------------------
if @Action = 'ALL'
begin
CREATE TABLE #tempKey (
a varchar (20),
b varchar (100),
c varchar (3)
)
if @Data_Type = 'DETAIL'
begin
set @Data_Type = ''
end
set @str = 'insert into #tempKey(a, b, c)
select distinct a, b, c
from tablename
where a like ''%'+ @a + '%''
and b like ''%'+ @b + '%''
and c like ''%' + @c + '%''
and Data_type like ''%' + @Data_Type + '%'''
if @Data_type <> 'Grand'
begin
set @str = @str + 'and b <> ''Grand'' '
end
else
begin
set @str = @str + 'and b = ''Grand'' '
end
set @str = @str + 'order by a, b, c'
exec sp_executesql @str
select a, b, c, identity (int,1,1) KeyID
into #tempKeyID
from #tempKey
select @maxKey = max(keyid)
from #tempKeyID
if @maxKey is null
begin
select 'NG' as msg
end
else
begin
set @i = 1
select top 1 @a = a,
@b = b,
@c = c,
@Keyid = Keyid
from #tempKeyID
delete from #tempKeyID
where a = @a
and b = @b
and c = @c
and keyid = @keyid
WHILE (@maxKey >= @i)
BEGIN
print @a
print @i
select identity (int,1,1) Rowid, @keyid as keyid, *
into #tempReady
from tablename
where a = @a
and b = @b
and c = @c
and Data_type like '%' + @Data_Type + '%'
order by keyid, a desc, b, c, data_type, data_seq, buyer
update #tempReady
set a = '',
b = '',
c = ''
where rowid <> '1'
----- prepare data -----
if @i = 1
begin
select keyid, a, b, c, buyer, Data_Type, Data_Seq
into #tempResult
from #tempReady
end
else
begin
insert into #tempResult
select keyid, a, b, c, buyer, Data_Type, Data_Seq
from #tempReady
end
set @i = @i + 1
drop table #tempReady
select top 1 @a = a,
@b = b,
@c = c,
@Keyid = keyid
from #tempKeyID
delete from #tempKeyID
where a = @a
and b = @b
and c = @c
and Keyid = @keyid
END
select a, b, c, buyer, Data_Type, Data_Seq
from #tempResult
order by keyid, a desc, b, c, data_type, data_seq, buyer
end
end
------------------------------------------------------------------------------
pls advice. thx.
|