Solved

SQL: select max group by

Posted on 2015-01-14
8
104 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 45

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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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 45

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 31

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now