| Author |
Topic |
|
Vaishu
Posting Yak Master
178 Posts |
Posted - 2010-02-25 : 04:52:58
|
| HiI have a table like belowCUSID-----------COMMWALL------------Some comments hereSRAN------------Booking slect optiojdhjjhjhSRAN------------PostponedjjhjhjjhjhjSRAN------------Delivery add changedRAMM------------New cystome paidTRAK------------Based outside of the ciyExpected result isCUSID-----------COMMWALL------------Some comments hereSRAN------------Booking slect optiojdhjjhjhRAMM------------New cystome paidTRAK------------Based outside of the ciyAny 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?MadhivananFailing to plan is Planning to fail |
 |
|
|
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)---------COMMWALL--------------20020805-----------Some comments hereSRAN--------------20030805-----------Booking slect optiojdhjjhjhSRAN--------------20030915-----------PostponedjjhjhjjhjhjSRAN--------------20100211-----------Delivery add changedRAMM--------------20090205-----------New cystome paidTRAK--------------20100122------------Based outside of the ciy Expected result is and need to sort by CUSID(Unique)CUSID-----------COMMWALL------------Some comments hereSRAN------------Delivery add changedRAMM------------New cystome paidTRAK------------Based outside of the ciyquote: Originally posted by madhivanan Which is the unique key?Which version of SQL Server are you using?MadhivananFailing to plan is Planning to fail
|
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-02-25 : 05:12:36
|
| select custid,comment from your_table as twhere DATEENT=(select max(DATEENT) from your_table where custid=t.custid)MadhivananFailing to plan is Planning to fail |
 |
|
|
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,COMMfrom table_name)a where s_no=1Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
balajiie
Starting Member
7 Posts |
Posted - 2010-02-25 : 05:30:17
|
| Just U need a Row That Recently Posted Rightits simple Try ThisSelect Top 1 * from table_name order by DATEENT descAll is wellBalaji |
 |
|
|
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 Rightits simple Try ThisSelect Top 1 * from table_name order by DATEENT descAll is wellBalaji
No, You quite miss-understood, Recent record on each customer!!!Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
Vaishu
Posting Yak Master
178 Posts |
Posted - 2010-02-25 : 05:35:10
|
HiThank you very much guys.!!!!!! |
 |
|
|
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 Rightits simple Try ThisSelect Top 1 * from table_name order by DATEENT descAll is wellBalaji
See the expected result againMadhivananFailing to plan is Planning to fail |
 |
|
|
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 itAll is wellBalaji |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
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 operationAll is wellBalaji |
 |
|
|
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 operationAll is wellBalaji
No. It wont give you the expected resultMadhivananFailing to plan is Planning to fail |
 |
|
|
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] |
 |
|
|
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=139575MadhivananFailing to plan is Planning to fail |
 |
|
|
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); |
 |
|
|
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?MadhivananFailing to plan is Planning to fail |
 |
|
|
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?MadhivananFailing 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 ONSET STATISTICS TIME ON--Your Queryselect custid,comment from @t as twhere 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 OFFSET STATISTICS TIME ON |
 |
|
|
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 |
 |
|
|
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 testCan 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 t6SET STATISTICS IO ONSET STATISTICS TIME ON--Your Queryselect custid,comment from @t as twhere 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 OFFSET STATISTICS TIME ONMadhivananFailing to plan is Planning to fail |
 |
|
|
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 t6SET STATISTICS IO ONSET STATISTICS TIME ONselect custid,comment from @t as twhere 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 OFFSET STATISTICS TIME ON |
 |
|
|
Next Page
|