tags:

views:

76

answers:

4

I'm tring to grab all fields from the latest Cash record, and then all fields from the related TransactionInfo record. I can't quite get this to work yet:

select t.*, top 1 c.* from Cash c
inner join TransactionInfo t
on c.TransactionID = t.id
order by c.createdOn desc
+1  A: 

What's that top 1 doing there? If you only want one row then the TOP(1) must come first:

SELECT TOP(1) t.*, c.*
FROM Cash c
INNER JOIN TransactionInfo t
ON c.TransactionID = t.id
ORDER BY c.createdOn DESC
Mark Byers
top is because I want to just grab the latest entry inserted..without knowing its id
CoffeeAddict
yea I see what you're saying now. What if I just wanted the latest record, not all records. I only want to see one result and that is the very latest record inserted into Cash based on CreatedOn
CoffeeAddict
Each latest entry of Cash per Transaction?
Mark Byers
I want the latest entry of Cash. Yours lists all ordered by CreatedOn. I only want the top 1 (most recently created Cash record)
CoffeeAddict
@coffeeaddict: Does my second query do what you want? If not, can you please explain more clearly what you want, perhaps with some example input and the expected output for that input.
Mark Byers
I don't want all transactions. I want the latest Cash record along with its related Transaction record details..all one result, one line
CoffeeAddict
Expected output would be 1 record result all on one line in the query analyzer results: [field1 from Transaction Table], [field2 from Transaction Table], [field3 from Transaction Table], [field1 from Cash Table], [field1 from Cash Table], [field2 from Cash Table], [field3 from Cash Table], [field4 from Cash Table] So it's going to show this based on the latest Cash record created in the Cash table (the latest record based on its Cash.CreatedOn date).
CoffeeAddict
Your first is close but that's why I had top 1 in there on the Cash table so that I only receive the latest one, not -all- cash results sorted by Cash.CreatedOn
CoffeeAddict
Ok, I just had the top in the wrong place looks like. God I hate SQL ;)
CoffeeAddict
+2  A: 
select top 1 *
from Cash c
inner join TransactionInfo t on c.TransactionID = t.id
order by createdOn desc
Blorgbeard
but will that also grab all fields from TransactionInfo in the result set? I need to see both sets of fields.
CoffeeAddict
+1 - I think this is what the OP is after (i.e. still needs the TOP 1, which was just in the wrong place)
AdaTheDev
@coffeeaddict - have you tried running this query? From your comments, it really does sound like this is what you want. Can you try it and clarify whether it does (or doesn't) do what you want?
AdaTheDev
@coffeeaddict: And if it doesn't work, post the error or the unexpected results (and what you expected).
Mark Byers
@coffeeaddict yes, `select *` will grab all fields from all joined tables - there's no need to say `select c.*, t.*` explicitly.
Blorgbeard
A: 

select t.,c. from (Select top 1 * from Cash order by createdOn desc ) c inner join TransactionInfo t on c.TransactionID = t.id order by createdOn desc

DOn;t use select * especially with a join, it wastes server resources.

HLGEM
A: 
SELECT c.*, t.* FROM cash c, transactioninfo t
WHERE c.infoid = t.id AND c.createdOn = (SELECT max(createdOn) FROM cash WHERE infoId = t.id) ORDER BY transactiontabledate desc

You need to find the record with the latest date from the cash table for each transactionId and use that also to filter it out in your query.

zapping