Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 27th, 2004, 01:45 AM
Registered User
 
Join Date: Apr 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default eliminate duplicates in selected columns

here's the table

w x y z
-------------------
a a a 1
a a a 2
a b c 3
a b c 4

the output im hopin 4 looks like this

w x y z
-------------------
a a a 1
                  2
a b c 3
                  4


where the duplicates in the w, x, y columns are displayed only once...

pls help!
 
Old April 27th, 2004, 05:40 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

There is, IMO, no good way to do this.

SQL is a data retrieval language, where the rows of the result represent a set. Each element (row) of a set contains the same number of attributes (columns). Your desired result does not conform to this.

Yours is really a presentation issue and should be handled in your host process on your client.


Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old April 27th, 2004, 08:13 PM
Registered User
 
Join Date: Apr 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old April 29th, 2004, 10:57 PM
Authorized User
 
Join Date: Jun 2003
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If the z column has unique values (maybe even an unique constraint / index, or primary key), and the table name is tb, the following code will do it:

select case when s.zMin is null then '' else t2.w end,
    case when s.zMin is null then '' else t2.x end,
    case when s.zMin is null then '' else t2.y end,
    t2.z
from tb t2
    left join (
        select min(z) as zMin
        from tb t
        group by t.w, t.x, t.y
    ) s on t2.z = s.zMin
order by t2.w, t2.x, t2.y

If you have another column with unique values (PK / UC) or a combination of columns, i think with some modifications the above code will also work.

If you have no column or column combination with uniwue values then only an "ugly" solution comes to my mind: using a cursor that browses through the data, and depending on the (w, x, y) values on the previous row, it inserts some data in a temporary table.





Similar Threads
Thread Thread Starter Forum Replies Last Post
sending email with selected columns frm excel Kaustav Excel VBA 0 December 11th, 2006 04:14 AM
How to eliminate all the extra zero? kuku SQL Server 2000 3 June 29th, 2005 09:47 PM
How find duplicates when I have several columns? boson SQL Language 1 October 15th, 2004 08:44 AM
syntax error on <option selected="selected"> hamid HTML Code Clinic 1 October 13th, 2004 09:20 AM
eliminate duplicates where attribute names differ dasdrs XSLT 2 August 9th, 2004 09:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.