Solved

SQL: select max group by

Posted on 2015-01-14
8
107 Views
Last Modified: 2015-01-22
Hi

Looking to select the last data row in a table for a group of customers

this gives me their last iInquiryId
SELECT     MAX(iInquiryId) AS inq
FROM         dbo.tbInquiry
WHERE     (iCustomerId IN (1,2,3,4))
GROUP BY iCustomerId

I also want vComment, dtInquiry but adding these in only gives me the last row of them all
adding a group by  iCustomerId, vComment, dtInquiry returns too much data

any ideas
0
Comment
Question by:louise_8
8 Comments
 
LVL 12

Expert Comment

by:James Elliott
ID: 40548781
Something like this, which I haven't tested:

SELECT
a.* 
FROM
(
		SELECT iInquiryID, vComment, dtInquiry, ROW_NUMBER() OVER (PARTITION BY iCustomerID ORDER BY iInquiryID DESC) as rn
		WHERE iCustomerid IN (1,2,3,4)
) a
WHERE a.rn = 1

Open in new window

0
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 40548782
You can try something like this:
SELECT  iInquiryId AS inq, vComment, dtInquiry  
 FROM         dbo.tbInquiry
 WHERE   iInquiryId = (SELECT MAX(iInquiryId) WHERE iCustomerId IN (1,2,3,4))

Open in new window

0
 
LVL 12

Expert Comment

by:James Elliott
ID: 40548786
Sorry, missing a line.

SELECT
a.* 
FROM
(
		SELECT iInquiryID, vComment, dtInquiry, ROW_NUMBER() OVER (PARTITION BY iCustomerID ORDER BY iInquiryID DESC) as rn
		FROM dbo.tbInquiry
		WHERE iCustomerid IN (1,2,3,4)
) a
WHERE a.rn = 1

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
ID: 40548795
Select A.*
From dbo.tbInquiry A
inner join
(
SELECT  iCustomerId ,   MAX(iInquiryId) AS inq
FROM         dbo.tbInquiry
WHERE     (iCustomerId IN (1,2,3,4))
GROUP BY iCustomerId ) As X
on A.iCustomerId =X.iCustomerId  and A.iInquiryId = X.inq
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40548801
please read up this article I wrote for this kind of questions:
http://www.experts-exchange.com/Database/Miscellaneous/A_3203-DISTINCT-vs-GROUP-BY-and-why-does-it-not-work-for-my-query.html
it explains all you need in regards to the needs vs syntax
0
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 40548821
Forgot the FROM in the subselect:
SELECT  iInquiryId AS inq, vComment, dtInquiry  
 FROM  dbo.tbInquiry
 WHERE   iInquiryId = (SELECT MAX(iInquiryId)  FROM  dbo.tbInquiry WHERE iCustomerId IN (1,2,3,4))

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 40549113
select inq, iCustomerId, vComment, dtInquiry from
(select iInquiryId as inq, iCustomerid, vComment, dtInquiry,
 row_number() over (partition by iCustomerId order by iInquiryId desc) rn
 from tbo.tblInquiry
 where iCustomerId in (1,2,3,4)) as x
where x.rn = 1;
0
 

Author Closing Comment

by:louise_8
ID: 40563880
thanks
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question