Solved

SQL: select max group by

Posted on 2015-01-14
8
105 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 46

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
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 
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 46

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

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 …
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

920 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

12 Experts available now in Live!

Get 1:1 Help Now