Home All Groups Group Topic Archive Search About

SQL GROUP BY CLAUSE



Author
1 Dec 2008 9:26 AM
keyur joshi
hi

i have made a view and its out put will be the following

InvDt           ItmNm           Qty              Mrp
20/08/08   abc                   20              100
21/08/08   abc                   30              100


Now i want to the output like this

invDt                                        ItmNm               Qty        
  Mrp.
20/08/08,21/08/08               abc                     50            100

Is It possible in to the ms sql server 2000

if yes then reply me soon plz help me in this


with regards
keyur joshi

Author
1 Dec 2008 11:40 AM
Uri Dimant
Hi
I'd prefer doing such things on the client or upgrade to SQL Server 2005
select convert(varchar(10),t1.InvDt,101)+',' +

convert(varchar(10),#t.InvDt,101),#t.ItmNm ,

t1.Qty+#t.Qty ,#t.Mrp from #t join #t t1

on #t.InvDt<t1.InvDt





Show quoteHide quote
"keyur joshi" <keyurjo***@discussions.microsoft.com> wrote in message
news:013FC5E2-31AE-4DAD-A95B-930650477FDF@microsoft.com...
> hi
>
> i have made a view and its out put will be the following
>
> InvDt           ItmNm           Qty              Mrp
> 20/08/08   abc                   20              100
> 21/08/08   abc                   30              100
>
>
> Now i want to the output like this
>
> invDt                                        ItmNm               Qty
>  Mrp.
> 20/08/08,21/08/08               abc                     50            100
>
> Is It possible in to the ms sql server 2000
>
> if yes then reply me soon plz help me in this
>
>
> with regards
> keyur joshi
>
>
Are all your drivers up to date? click for free checkup

Author
7 Dec 2008 3:29 AM
Roshan Jayalath
Hi,

I would also recomend to use SQL2005 or application code, but I guess
sometimes we have to live with what we re given. so in case you need to do it
in SQL2000 you can use a cursor as below. You may need to modify the code
below.

-- Drop table #invsum
Select sum(qty) As total, itmnm, mrp, space(2000) as dates into #invsum from
inv group by itmnm, mrp

Declare @citmnm varchar(100), @imrp Int, @dInvDt datetime

DECLARE #invdates CURSOR
FOR Select distinct invdt, itmnm, mrp From inv

OPEN #invdates

FETCH NEXT FROM #invdates INTO @dInvDt, @citmnm, @imrp
WHILE (@@fetch_status <> -1)
BEGIN
    IF (@@fetch_status <> -2)
    BEGIN
        update #invsum set dates = LTRIM(RTRIM(dates)) + case when dates = ''
THEN '' ELSE ',' END +
            convert(CHAR(8), @dInvDt, 3 )
            WHERE itmnm = @citmnm AND mrp = @imrp
    END
    FETCH NEXT FROM #invdates INTO @dInvDt, @citmnm, @imrp
END

CLOSE #invdates
DEALLOCATE #invdates
GO

SELECT * FROM #invsum

Best Regards,
Roshan Jayalath


Show quoteHide quote
"keyur joshi" wrote:

> hi
>
> i have made a view and its out put will be the following
>
> InvDt           ItmNm           Qty              Mrp
>  20/08/08   abc                   20              100
>  21/08/08   abc                   30              100
>
>
> Now i want to the output like this
>
> invDt                                        ItmNm               Qty        
>   Mrp.
> 20/08/08,21/08/08               abc                     50            100
>
> Is It possible in to the ms sql server 2000
>
> if yes then reply me soon plz help me in this
>
>
> with regards
> keyur joshi
>
>
Author
7 Dec 2008 3:30 AM
Roshan Jayalath
Hi,

Regarding my last reply to you, please note that the code may not be optimal
as I did it quickly and you have to note that I have put 2000 character
limit. When you reach 2000 character, bang..... so you may try a text column.

Best Regards,
Roshan Jayalath

Show quoteHide quote
"keyur joshi" wrote:

> hi
>
> i have made a view and its out put will be the following
>
> InvDt           ItmNm           Qty              Mrp
>  20/08/08   abc                   20              100
>  21/08/08   abc                   30              100
>
>
> Now i want to the output like this
>
> invDt                                        ItmNm               Qty        
>   Mrp.
> 20/08/08,21/08/08               abc                     50            100
>
> Is It possible in to the ms sql server 2000
>
> if yes then reply me soon plz help me in this
>
>
> with regards
> keyur joshi
>
>

Bookmark and Share