Solved

help needed with Microsoft SQL Server syntax

Posted on 2011-03-14
5
257 Views
Last Modified: 2012-05-11
select customerid, city, dateordered where customerid = (select customerid, city, max(dateordered)
0
Comment
Question by:Jimmy_inc
5 Comments
 
LVL 19

Assisted Solution

by:Rikin Shah
Rikin Shah earned 125 total points
ID: 35135504
Try this...

select customerid, city, dateordered from <tablename> where customerid in (select customerid from <tablename> having max(dateordered))
0
 

Author Comment

by:Jimmy_inc
ID: 35135513
Yes that looks better, I will try this
0
 
LVL 40

Accepted Solution

by:
Sharath earned 125 total points
ID: 35135551
try this.
SELECT customerid, 
       city, 
       dateordered 
  FROM your_table t1 
 WHERE t1.dateordered = (SELECT MAX(t2.dateordered) 
                           FROM your_table t2 
                          WHERE t1.customerid = t2.customerid 
                                AND t1.city = t2.city)

Open in new window

0
 
LVL 5

Assisted Solution

by:Angelgeo
Angelgeo earned 125 total points
ID: 35136300

select 
  customerid, city, max(dateordered) as dateordered
from
  tablename
group by customerid, city

-- Or 

select 
  customerid, city, dateordered
from
  tablename
where 
  dateordered in (select max(dateordered) as dateordered
from
  tablename)

Open in new window

0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 125 total points
ID: 35139240
See attached.
query.txt
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 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

21 Experts available now in Live!

Get 1:1 Help Now