SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Filter out records from table


Filter out records from table

Author
Message
PSB
PSB
SSC Eights!
SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)

Group: General Forum Members
Points: 993 Visits: 1544
Hi,

I have a table where I want to filter out some records.

I want to get maximum OF enddate BY USER AND HName WHERE description = Client
FOR USER 'dk' I want to return the record WHERE EndDate ='2015-06-27 23:59:59.000' AND Description = 'Client' AND HName = 'CR-GINGA'
FOR USER 'dk' I want to return the record WHERE EndDate ='2015-06-27 23:59:59.000' AND Description = 'Client' AND HName = 'CR-ABCED'
FOR USER 'ff' I want to return the record WHERE EndDate ='2016-01-14 23:59:59.000' AND Description = 'Client' AND HName = 'HN-FRNNN'

CREATE TABLE #Info

(
HName VARCHAR(100),
USERName VARCHAR(10),
StartDate DATETIME,
ENdDAte DATETIME,
DESCRIPTION VARCHAR(100),
STag VARCHAR(10)

)

INSERT INTO #Info
( HName ,
USERName ,
StartDate ,
ENdDAte ,
DESCRIPTION ,
STag
)
VALUES ( 'CR-GINGA' , -- HName - varchar(100)
'dk' , -- USERName - varchar(10)
'2012-06-27 00:00:00.000' , -- StartDate - datetime
'2013-06-27 23:59:59.000' , -- ENdDAte - datetime
'Client' , -- DESCRIPTION - varchar(100)
'ABX123' -- STag - varchar(10)
)

INSERT INTO #Info
( HName ,
USERName ,
StartDate ,
ENdDAte ,
DESCRIPTION ,
STag
)
VALUES ( 'CR-GINGA' , -- HName - varchar(100)
'dk' , -- USERName - varchar(10)
'2013-06-28 00:00:00.000' , -- StartDate - datetime
'2015-06-27 23:59:59.000' , -- ENdDAte - datetime
'Client' , -- DESCRIPTION - varchar(100)
'ABX123' -- STag - varchar(10)
)

INSERT INTO #Info
( HName ,
USERName ,
StartDate ,
ENdDAte ,
DESCRIPTION ,
STag
)
VALUES ( 'CR-GINGA' , -- HName - varchar(100)
'dk' , -- USERName - varchar(10)
'2012-06-27 00:00:00.000' , -- StartDate - datetime
'2015-06-27 23:59:59.000' , -- ENdDAte - datetime
'Comp Care' , -- DESCRIPTION - varchar(100)
'ABX123' -- STag - varchar(10)
)

INSERT INTO #Info
( HName ,
USERName ,
StartDate ,
ENdDAte ,
DESCRIPTION ,
STag
)
VALUES ( 'CR-GINGA' , -- HName - varchar(100)
'dk' , -- USERName - varchar(10)
'2012-06-27 00:00:00.000' , -- StartDate - datetime
'2013-06-27 23:59:59.000' , -- ENdDAte - datetime
'Next business' , -- DESCRIPTION - varchar(100)
'ABX123' -- STag - varchar(10)
)
INSERT INTO #Info
( HName ,
USERName ,
StartDate ,
ENdDAte ,
DESCRIPTION ,
STag
)
VALUES ( 'CR-GINGA' , -- HName - varchar(100)
'dk' , -- USERName - varchar(10)
'2013-06-28 00:00:00.000' , -- StartDate - datetime
'2015-06-27 23:59:59.000' , -- ENdDAte - datetime
'Next businessNext business' , -- DESCRIPTION - varchar(100)
'ABX123' -- STag - varchar(10)
)



INSERT INTO #Info
( HName ,
USERName ,
StartDate ,
ENdDAte ,
DESCRIPTION ,
STag
)
VALUES ( 'CR-ABCED' , -- HName - varchar(100)
'dk' , -- USERName - varchar(10)
'2012-06-27 00:00:00.000' , -- StartDate - datetime
'2013-06-27 23:59:59.000' , -- ENdDAte - datetime
'Client' , -- DESCRIPTION - varchar(100)
'ABX123' -- STag - varchar(10)
)


INSERT INTO #Info
( HName ,
USERName ,
StartDate ,
ENdDAte ,
DESCRIPTION ,
STag
)
VALUES ( 'CR-ABCED' , -- HName - varchar(100)
'dk' , -- USERName - varchar(10)
'2013-06-28 00:00:00.000' , -- StartDate - datetime
'2015-06-27 23:59:59.000' , -- ENdDAte - datetime
'Client' , -- DESCRIPTION - varchar(100)
'ABX123' -- STag - varchar(10)
)


INSERT INTO #Info
( HName ,
USERName ,
StartDate ,
ENdDAte ,
DESCRIPTION ,
STag
)
VALUES ( 'CR-ABCED' , -- HName - varchar(100)
'dk' , -- USERName - varchar(10)
'2013-06-27 00:00:00.000' , -- StartDate - datetime
'2015-06-27 23:59:59.000' , -- ENdDAte - datetime
'Comp Care' , -- DESCRIPTION - varchar(100)
'ABX123' -- STag - varchar(10)
)



INSERT INTO #Info
( HName ,
USERName ,
StartDate ,
ENdDAte ,
DESCRIPTION ,
STag
)
VALUES ( 'HN-FRNNN' , -- HName - varchar(100)
'ff' , -- USERName - varchar(10)
'2014-01-15 00:00:00.000' , -- StartDate - datetime
'2016-01-14 23:59:59.000' , -- ENdDAte - datetime
'Client' , -- DESCRIPTION - varchar(100)
'DCFTT1' -- STag - varchar(10)
)


INSERT INTO #Info
( HName ,
USERName ,
StartDate ,
ENdDAte ,
DESCRIPTION ,
STag
)
VALUES ( 'HN-FRNNN' , -- HName - varchar(100)
'ff' , -- USERName - varchar(10)
'2013-01-14 00:00:00.000' , -- StartDate - datetime
'2014-01-14 23:59:59.000' , -- ENdDAte - datetime
'Client' , -- DESCRIPTION - varchar(100)
'DCFTT1' -- STag - varchar(10)
)


INSERT INTO #Info
( HName ,
USERName ,
StartDate ,
ENdDAte ,
DESCRIPTION ,
STag
)
VALUES ( 'HN-FRNNN' , -- HName - varchar(100)
'ff' , -- USERName - varchar(10)
'2013-01-14 00:00:00.000' , -- StartDate - datetime
'2016-01-14 23:59:59.000' , -- ENdDAte - datetime
'Comp Care' , -- DESCRIPTION - varchar(100)
'DCFTT1' -- STag - varchar(10)
)


INSERT INTO #Info
( HName ,
USERName ,
StartDate ,
ENdDAte ,
DESCRIPTION ,
STag
)
VALUES ( 'HN-FRNNN' , -- HName - varchar(100)
'ff' , -- USERName - varchar(10)
'2013-01-14 00:00:00.000' , -- StartDate - datetime
'2014-01-14 23:59:59.000' , -- ENdDAte - datetime
'Next Business' , -- DESCRIPTION - varchar(100)
'DCFTT1' -- STag - varchar(10)
)


INSERT INTO #Info
( HName ,
USERName ,
StartDate ,
ENdDAte ,
DESCRIPTION ,
STag
)
VALUES ( 'HN-FRNNN' , -- HName - varchar(100)
'ff' , -- USERName - varchar(10)
'2014-01-15 00:00:00.000' , -- StartDate - datetime
'2016-01-14 23:59:59.000' , -- ENdDAte - datetime
'Next Business' , -- DESCRIPTION - varchar(100)
'DCFTT1' -- STag - varchar(10)
)





SELECT * FROM #Info

DROP TABLE #Info

Thanks,
PSB
HanShi
HanShi
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4422 Visits: 3672
select HName, USERname, max(EndDate) as max_enddate
from #Info
where Description = 'Client'
group by HName, USERname



** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search