Author |
Topic |
cobby1812
Starting Member
16 Posts |
Posted - 2012-01-24 : 12:25:18
|
Help I am stuck....bet you havent heard that before...anyway to the issue.I am trying to retrieve a row per case out of my database.Basicially I am trying to find the first event in a table....but no matter what i do I always end showing three rows.select CASEHEADER.CASEHEADER_RSN, CASEHISTORY.CODE, CASEHISTORY.SYSTEMDATEfrom caseheaderinner join casehistoryon caseheader.caseheader_rsn = casehistory.caseheader_rsninner join caseeventon casehistory.caseheader_rsn = caseevent.caseheader_rsnWHERE CODE IN ('2010', '2004', '2014', '2017', '1458')AND CASEHEADER.CASEHEADER_RSN = 1777THE RESULTS SHOW AS 1777 2014 2009-07-30 11:37:10.7631777 2010 2010-11-02 14:45:31.9731777 1458 2010-12-22 12:35:26.357sO I WANT THE QUERY TO RETURN1777 2014, 2009-07-30 11:37:10.763hOW ON EARTH DO i DO THATOpps caps locks on now......... |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-01-24 : 12:31:42
|
SELECT TOP 1....ORDER BY <CONDITION>Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2012-01-24 : 12:32:24
|
SELECT TOP (1) .... ORDER BY SystemDate ??--Gail ShawSQL Server MVP |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-01-24 : 12:33:23
|
like:BEGIN TRANSELECT TOP 1 CASEHEADER.CASEHEADER_RSN, CASEHISTORY.CODE, CASEHISTORY.SYSTEMDATEFROM caseheader inner join casehistory ON caseheader.caseheader_rsn = casehistory.caseheader_rsn inner join caseevent on casehistory.caseheader_rsn = caseevent.caseheader_rsnWHERE CODE IN ('2010', '2004', '2014', '2017', '1458') AND CASEHEADER.CASEHEADER_RSN = 1777ORDER BY CASEHISTORY.SYSTEMDATE ASCROLLBACK Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
cobby1812
Starting Member
16 Posts |
Posted - 2012-01-24 : 12:39:31
|
Awesome of the most awesomness...thank you kindly. |
 |
|
cobby1812
Starting Member
16 Posts |
Posted - 2012-01-24 : 12:48:18
|
Sorry...what if I want to return multiple rows................. |
 |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2012-01-24 : 13:03:04
|
Multiple rows as in?????--Gail ShawSQL Server MVP |
 |
|
cobby1812
Starting Member
16 Posts |
Posted - 2012-01-25 : 03:25:56
|
As in the select Top 1 will return one row. What I want to do is only select the first code from each instance of a caseheader_rsn. ie.1234, 2014, 010120122345, 2010, 01012012etc |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-01-25 : 04:16:04
|
use a ranking function like ROW_NUMBER()SELECT dt.[CASEHEADER_RSN] , dt.[CODE] , dt.[SYSTEMDATE]FROM ( SELECT cHead.[CASEHEADER_RSN] AS [CASEHEADER_RSN] , cHist.[CODE] AS [CODE] , cHist.[SYSTEMDATE] AS [SYSTEMDATE] , ROW_NUMBER() OVER (PARTITION BY cHead.[CASEHEADER_RSN] ORDER BY cHist.[SYSTEMDATE] ASC) AS [rnk] FROM caseheader AS cHead JOIN casehistory AS cHist ON cHead.[caseheader_rsn] = cHist.[caseheader_rsn] JOIN caseevent AS cEvent ON cHist.[caseheader_rsn] = cEvent.[caseheader_rsn] WHERE cHist.[CODE] IN ('2010', '2004', '2014', '2017', '1458') -- AND cHead.[CASEHEADER_RSN] = 1777 ) AS dt WHERE dt.[rnk] = 1 Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|