Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to select first row ?

Author  Topic 

Vaishu
Posting Yak Master

178 Posts

Posted - 2010-02-25 : 04:52:58
Hi
I have a table like below

CUSID-----------COMM
WALL------------Some comments here
SRAN------------Booking slect optiojdhjjhjh
SRAN------------Postponedjjhjhjjhjhj
SRAN------------Delivery add changed
RAMM------------New cystome paid
TRAK------------Based outside of the ciy

Expected result is

CUSID-----------COMM
WALL------------Some comments here
SRAN------------Booking slect optiojdhjjhjh
RAMM------------New cystome paid
TRAK------------Based outside of the ciy

Any help please?






madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-25 : 04:56:54
Which is the unique key?
Which version of SQL Server are you using?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Vaishu
Posting Yak Master

178 Posts

Posted - 2010-02-25 : 05:09:57
Hi

I am using SQL 2008 standard.

The table have customer Id (CUSID), Date entered and Comments. One customer have many comments. I need to pick the last comments entered by date.(I forgot to mention the date)

CUSID-----------DATEENT(desimal(9,0)---------COMM
WALL--------------20020805-----------Some comments here
SRAN--------------20030805-----------Booking slect optiojdhjjhjh
SRAN--------------20030915-----------Postponedjjhjhjjhjhj
SRAN--------------20100211-----------Delivery add changed
RAMM--------------20090205-----------New cystome paid
TRAK--------------20100122------------Based outside of the ciy

Expected result is and need to sort by CUSID(Unique)

CUSID-----------COMM

WALL------------Some comments here
SRAN------------Delivery add changed
RAMM------------New cystome paid
TRAK------------Based outside of the ciy



quote:
Originally posted by madhivanan

Which is the unique key?
Which version of SQL Server are you using?

Madhivanan

Failing to plan is Planning to fail

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-25 : 05:12:36
select custid,comment from your_table as t
where DATEENT=(select max(DATEENT) from your_table where custid=t.custid)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2010-02-25 : 05:14:45
select CUSID,COMM from
(select row_number() over(partition by CUSID order by DATEENT desc) as s_no, CUSID,COMM
from table_name)a where s_no=1

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

balajiie
Starting Member

7 Posts

Posted - 2010-02-25 : 05:30:17
Just U need a Row That Recently Posted Right

its simple Try This

Select Top 1 * from table_name order by DATEENT desc

All is well
Balaji
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2010-02-25 : 05:33:00
quote:
Originally posted by balajiie

Just U need a Row That Recently Posted Right

its simple Try This

Select Top 1 * from table_name order by DATEENT desc

All is well
Balaji



No, You quite miss-understood, Recent record on each customer!!!

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

Vaishu
Posting Yak Master

178 Posts

Posted - 2010-02-25 : 05:35:10
Hi
Thank you very much guys.!!!!!!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-25 : 06:24:20
quote:
Originally posted by balajiie

Just U need a Row That Recently Posted Right

its simple Try This

Select Top 1 * from table_name order by DATEENT desc

All is well
Balaji


See the expected result again

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

balajiie
Starting Member

7 Posts

Posted - 2010-02-25 : 06:34:07
hi senthil sorry for the interference .. i am a beginner i am not able to understand your query..please explain it

All is well
Balaji
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-25 : 06:40:07
quote:
Originally posted by balajiie

hi senthil sorry for the interference .. i am a beginner i am not able to understand your query..please explain it

All is well
Balaji


Refer this
http://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/multipurpose-row-number-function.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

balajiie
Starting Member

7 Posts

Posted - 2010-02-25 : 06:52:30
Thank U so Much Madhi...only one doubt ..shall v use distinct keyword for this operation

All is well
Balaji
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-25 : 07:21:08
quote:
Originally posted by balajiie

Thank U so Much Madhi...only one doubt ..shall v use distinct keyword for this operation

All is well
Balaji


No. It wont give you the expected result

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-03-01 : 05:05:14
[code]SELECT t1.custid, t2.comment
FROM (SELECT DISTINCT custid
FROM your_table) t1(c)
CROSS APPLY (SELECT TOP 1 comment
FROM your_table t
WHERE t.c = t1.custid
ORDER BY DATEENT DESC) t2;
[/code]
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-01 : 05:09:33
quote:
Originally posted by ms65g

SELECT t1.custid, t2.comment 
FROM (SELECT DISTINCT custid
FROM your_table) t1(c)
CROSS APPLY (SELECT TOP 1 comment
FROM your_table t
WHERE t.c = t1.custid
ORDER BY DATEENT DESC) t2;



How did you forget your own advice?
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=139575

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-03-01 : 05:58:09
--> How did you forget your own advice? <--
Would you like so standard form?

SELECT *
FROM table_name T1
WHERE NOT EXISTS
(SELECT *
FROM table_name T2
WHERE T1.DATEENT < T2.DATEENT
AND T2.custid = T1.custid);

SELECT *
FROM table_name T1
WHERE DATEENT >= ALL (SELECT DATEENT
FROM table_name T2
WHERE T1.custid = T2.custid);

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-01 : 07:14:35
quote:
Originally posted by ms65g

--> How did you forget your own advice? <--
Would you like so standard form?

SELECT *
FROM table_name T1
WHERE NOT EXISTS
(SELECT *
FROM table_name T2
WHERE T1.DATEENT < T2.DATEENT
AND T2.custid = T1.custid);

SELECT *
FROM table_name T1
WHERE DATEENT >= ALL (SELECT DATEENT
FROM table_name T2
WHERE T1.custid = T2.custid);




Can you post performance results based on various methods?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-03-01 : 08:28:44
quote:
Originally posted by madhivanan

quote:
Originally posted by ms65g

--> How did you forget your own advice? <--
Would you like so standard form?

SELECT *
FROM table_name T1
WHERE NOT EXISTS
(SELECT *
FROM table_name T2
WHERE T1.DATEENT < T2.DATEENT
AND T2.custid = T1.custid);

SELECT *
FROM table_name T1
WHERE DATEENT >= ALL (SELECT DATEENT
FROM table_name T2
WHERE T1.custid = T2.custid);




Can you post performance results based on various methods?

Madhivanan

Failing to plan is Planning to fail




SET NOCOUNT ON;
DECLARE @t TABLE (CUSTID CHAR(4), DATEENT DECIMAL(9,0), COMMENT VARCHAR(50));
INSERT @t VALUES
('WALL',20020805,'Some comments here'),
('SRAN',20030805,'Booking slect optiojdhjjhjh'),
('SRAN',20030915,'Postponedjjhjhjjhjhj'),
('SRAN',20100211,'Delivery add changed'),
('RAMM',20090205,'New cystome paid'),
('TRAK',20100122,'Based outside of the ciy');

SET STATISTICS IO ON
SET STATISTICS TIME ON

--Your Query
select custid,comment from @t as t
where DATEENT=(select max(DATEENT) from @t where custid=t.custid)
/*

Batch Cost: 47%
Table '#76818E95'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.


SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 35 ms.
*/
--My Query
SELECT custid, comment
FROM @t T1
WHERE NOT EXISTS
(SELECT *
FROM @t T2
WHERE T1.DATEENT < T2.DATEENT
AND T2.custid = T1.custid);
/*
Batch Cost: 21%

Table '#70C8B53F'. Scan count 2, logical reads 7, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.


SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 28 ms.

*/
SET STATISTICS IO OFF
SET STATISTICS TIME ON


Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-03-01 : 08:37:36
So ur query is much resource intensive because urs is taking 7 times more logical reads than madhis one.

PBUH
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-01 : 08:38:59
Dont test on the small set of data. Have atleast 10K rows and test

Can you post the result of this?


SET NOCOUNT ON;
DECLARE @t TABLE (CUSTID CHAR(4), DATEENT DECIMAL(9,0), COMMENT VARCHAR(50));

INSERT @t VALUES
('WALL',20020805,'Some comments here'),
('SRAN',20030805,'Booking slect optiojdhjjhjh'),
('SRAN',20030915,'Postponedjjhjhjjhjhj'),
('SRAN',20100211,'Delivery add changed'),
('RAMM',20090205,'New cystome paid'),
('TRAK',20100122,'Based outside of the ciy');
INSERT @t
SELECT t1.* from @t as t1 cross join @t as t2 cross join @t as t3 cross join @t as t4 cross join @t as t5 cross join @t as t6


SET STATISTICS IO ON
SET STATISTICS TIME ON

--Your Query
select custid,comment from @t as t
where DATEENT=(select max(DATEENT) from @t where custid=t.custid)

SELECT custid, comment
FROM @t T1
WHERE NOT EXISTS
(SELECT *
FROM @t T2
WHERE T1.DATEENT < T2.DATEENT
AND T2.custid = T1.custid);
SET STATISTICS IO OFF
SET STATISTICS TIME ON



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-03-01 : 08:59:20
My query is not useful BUT IT IS A SOLUTION!

SET NOCOUNT ON;
DECLARE @t TABLE (CUSTID CHAR(4), DATEENT DECIMAL(9,0), COMMENT VARCHAR(50));

INSERT @t VALUES
('WALL',20020805,'Some comments here'),
('SRAN',20030805,'Booking slect optiojdhjjhjh'),
('SRAN',20030915,'Postponedjjhjhjjhjhj'),
('SRAN',20100211,'Delivery add changed'),
('RAMM',20090205,'New cystome paid'),
('TRAK',20100122,'Based outside of the ciy');
INSERT @t
SELECT t1.* from @t as t1 cross join @t as t2 cross join @t as t3 cross join @t as t4 cross join @t as t5 cross join @t as t6


SET STATISTICS IO ON
SET STATISTICS TIME ON

select custid,comment from @t as t
where DATEENT=(select max(DATEENT) from @t where custid=t.custid)
/*
Table '#290D0E62'. Scan count 1, logical reads 248, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

SQL Server Execution Times:
CPU time = 141 ms, elapsed time = 291 ms.
*/

SELECT custid, comment
FROM @t T1
WHERE NOT EXISTS
(SELECT *
FROM @t T2
WHERE T1.DATEENT < T2.DATEENT
AND T2.custid = T1.custid);
/*
Table '#290D0E62'. Scan count 2, logical reads 7730586, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

SQL Server Execution Times:
CPU time = 385281 ms, elapsed time = 389001 ms.
*/

SET STATISTICS IO OFF
SET STATISTICS TIME ON
Go to Top of Page
    Next Page

- Advertisement -