Discussion Locked

What's the right SQL Statement

0 helpful
1 reply
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I got one(1) table with the following data:

Table Name: MyTable

ID Date Name
==============================
1 2008-08-31 James
2 2008-08-31 Jack
3 2008-08-31 Gregory
4 2008-08-29 James
5 2008-08-29 Jack
==============================

Now, how can I make it look like this:

Name Date1 Date2
=================================
James 2008-08-31 2008-08-29
Jack 2008-08-31 2008-08-29
Gregory 2008-08-31
=================================

Basically, I'm grouping it by the field Name.

1 reply

by praspam

alter FUNCTION dbo.ufn_FindReports (@sName varchar(50))
RETURNS @retFindReports TABLE
(
sdate varchar(255)
)
AS
BEGIN
declare @sdate varchar(255);
WITH DateList(sdate) AS
(
SELECT cast(date as varchar(12)) FROM mytable where sname=@sName
)
INSERT @retFindReports
SELECT sdate fROM DateList
RETURN
END
GO

declare @sdate varchar(255)
SELECT @sdate = COALESCE(@sdate + ', ', '') +
cast(sdate as varchar(12))
FROM dbo.ufn_FindReports('James')

select distinct (select @sdate),sname from mytable where sname='James'

This discussion is closed to new replies.