Solved

SQL: select max group by

Posted on 2015-01-14
8
117 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 50

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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 143

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 50

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

734 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