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
 select max int column and other coumns

Author  Topic 

chedderslam
Posting Yak Master

223 Posts

Posted - 2015-02-27 : 14:53:09
I have three records, with one column that is an identity/PK. I want the last record inserted and the whole record's information from the query, using a where clause to limit the record returned from the table.

I get an error if I do not have the group by added. However, with this query, I still get three records returned. Can someone please help?

Thank you.

SELECT max([tablePK])
,[orderGUID]
,[tblPeopleFK]
,[tbl_shippingAddressFK]
,[tblBillingMethodFK]
,[shippingMethod]
,[shippingCost]
,[shippingWeight]
,[billingtype]
,[insuranceamount]
,[paypal_token]
,[paypal_payer_id]
,[cfuserid]
FROM [tblOrderInfoBuffer]
where cfuserid = '69233411022258297'
GROUP BY [orderGUID]
,[tblPeopleFK]
,[tbl_shippingAddressFK]
,[tblBillingMethodFK]
,[shippingMethod]
,[shippingCost]
,[shippingWeight]
,[billingtype]
,[insuranceamount]
,[paypal_token]
,[paypal_payer_id]
,[cfuserid]

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2015-02-27 : 14:57:38
Assuming your identity PK has a positive increment
SELECT TOP (1)
[tablePK],
.....
FROM [tblOrderInfoBuffer]
where cfuserid = '69233411022258297'
ORDER BY [tablePK] DESC;
Go to Top of Page
   

- Advertisement -