Solved

SQL Query to detect different value

Posted on 2010-11-17
6
356 Views
Last Modified: 2012-08-13
I have a table that contains a separate row for each item in an order. Each row for an order should have the same date. Unfortunately, some don't. I need to detect which orders has a row with a date that is different from the other rows for the same order.

For example:
In table 'Orders' I am searching for row like this. Row 111 has a date different from the other rows. How do I detect this in a SQL Query? I can't specify the OrderID. I need to search a large table in one query.

ID         OrderID       ShipDate
111      1001              2009-05-12 00:00:00.000
112      1001              2010-01-26 00:00:00.000
113      1001              2010-01-26 00:00:00.000
114      1001               2010-01-26 00:00:00.000

Thanks,
DT

SQL Server 2008
0
Comment
Question by:DTwined
6 Comments
 
LVL 19

Accepted Solution

by:
Bhavesh Shah earned 500 total points
ID: 34155082
Hi,

check this

select OrderID,count(distinct shipdate)shipdate
from tablename
group by orderid
having count(distinct shipdate) = 1
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34155350
Please check the attached script.

Raj
create table #orders
(
	ID			int,
	OrderID     int,
	ShipDate	datetime
)

insert into #orders
select 111,      1001,              '2009-05-12 00:00:00.000' union all
select 112,      1001,              '2010-01-26 00:00:00.000' union all
select 113,      1001,              '2010-01-26 00:00:00.000' union all
select 114,      1001,               '2010-01-26 00:00:00.000' union all
select 115,      1002,              '2010-01-27 00:00:00.000' union all
select 116,      1002,               '2010-01-27 00:00:00.000' 

select * from #orders


-- OrderIDs that are in different dates
select OrderID
from #orders
group by OrderID
having count(distinct ShipDate) > 1

drop table #orders

Open in new window

0
 
LVL 7

Expert Comment

by:rmm2001
ID: 34155358
Will you ever run into the scenario:

ID         OrderID       ShipDate
111      1001              2009-05-12 00:00:00.000
112      1001              2010-01-26 00:00:00.000
113      1001              2010-01-26 00:00:00.000
114      1001              2011-05-12 00:00:00.000
115      1001              2012-01-26 00:00:00.000
116      1001              2012-01-26 00:00:00.000
117      1001              2013-01-26 00:00:00.000

Where you have multiple different dates for the same order number and those multiple different dates have more than one entry? If so the query above would be a little stretched to work. If not the it'll work just fine
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34155371
Cont: of my comment http:#34155350

If you want to return all the rows related to those OrderIDs that are scattered in different dates, try this query.
 
select * from #orders where OrderID in
(
	select OrderID
	from #orders
	group by OrderID
	having count(distinct ShipDate) > 1
)

Open in new window


Regards
Raj
0
 
LVL 32

Expert Comment

by:awking00
ID: 34156177
Is it possible to have more than one date that is different from the others?
0
 

Author Closing Comment

by:DTwined
ID: 34157918
First one I tried and it did the trick. I changed the '=1' to '>1' to get it to do what I needed it to do. Thanks for all the feedback!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Amazon RDS migrate to SQL Server 3 33
TSQL convert date to string 4 56
How to structure query with count aggregate 4 41
Parsing this XML works but the other one doesn't 9 27
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 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