Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Query help needed

Posted on 2014-02-01
9
Medium Priority
?
148 Views
Last Modified: 2014-02-01
I need help with the logic of this query.

I have two tables - customers and orders.

Customers
--------------------------------------
custid    name
1            Joe
2            Sam
3            Rick

Orders
-------------------------------------
orderid  custid  orderdate           status
1               1        01/01/2006       posted
2               1        10/10/2013       posted
3               1        01/14/2014       in process
4               2        02/02/2008       posted

I need the query to select all customers where the orderdate =>01/01/2012 AND the status is posted.

The criteria for the expected customer results is based on the values in orders. Currently the results I am getting include duplication of customers where there are more than one order that meets the criteria.

Example:
SELECT * from Customers
INNER JOIN    -- have tried other joins also
custid on customer.custid = orders.custid
WHERE
status = 'posted' and orderdate >= 01/01/2012

This results in two duplicate records in the results for custid 1

How do I need to properly construct the query to get the desired results?
0
Comment
Question by:ckelsoe
[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
  • 5
  • 3
9 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 39826643
What database platform are you using?

Access?  SQL Server?  ...?  (this makes a difference)

Try delimiting your dates:


status = 'posted' and orderdate >= #01/01/2012#       (MS Access)

status = 'posted' and orderdate >= '01/01/2012'       (SQL Server and others)


(Without the delimiters, 01/01/2012 is 1 divided by 1 divided by 2012)
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 total points
ID: 39826659
Hi ckelsoe,

Probably the easiest to select the most recent when there are duplicates.

SELECT * 
FROM customers c
INNER JOIN order o
  ON c.customer_id = o.customer_id
WHERE order_date >= '01/'1/2-13'
  AND order_status = 'posted'
  AND order_id = (select max(order_id) from order o1 where o1.customer_id = o.customer_id and o1.order_status = 'posted')

Open in new window



That should work fine in SQL Server.


Good Luck,
Kent
0
 

Author Closing Comment

by:ckelsoe
ID: 39826701
Thanks - I think that should work perfectly for what I need.
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 

Author Comment

by:ckelsoe
ID: 39827107
Ok I now have this issue. The MAX(OrderID) does not work as there is a case where the highest order id does not have the date within the scope. An earlier orderid does have a date within scope. The code as written above drops that customer when it should be included in the results. I tried changing it to max(order_date) but that returns no results at all. Ideas?
0
 

Author Comment

by:ckelsoe
ID: 39827123
So this works:
SELECT
	MAX (PostedDate)
FROM
	ERCHistory
WHERE
	customerid = 394
AND ERCStatus = 'posted'

Open in new window


But this does not:
SELECT
	c.CustomerID,
	c.Business,
	c.BusName,
	c.NameLast,
	c.NameFirst,
	c.Address1,
	c.City,
	c.State,
	c.Zip,
	c.Phone,
	c.BusPhone,
	c.Cell,
	c.Email,
	c.Pager,
	c.ResaleNumber,
	c.TaxExemptParts,
	c.TaxExemptLabor,
	c.[TimeStamp]
FROM
	dbo.Customer c
INNER JOIN dbo.ERCHistory o ON c.CustomerID = o.CustomerID
WHERE
	o.ERCStatus = 'Posted'
AND o.PostedDate >= '01/01/2006'
AND o.ERCID = (
	SELECT
		MAX (PostedDate)
	FROM
		ERCHistory o1
	WHERE
		o1.CustomerID = o.CustomerID
	AND o1.ERCStatus = 'posted'
)
ORDER BY c.CustomerID ASC

Open in new window


The initial code at MAX (ERCID) which did work.

Here is the data that is causing the issue:
ERCID      CustomerID      ERCStatus      PostedDate
913              394      Posted      2004-09-15-00:00:00.000
1064      394      Posted      2005-03-09-00:00:00.000
1103      394      Posted      2005-04-26-00:00:00.000
1281      394      Posted      2006-01-10-00:00:00.000
1282      394      Posted      2005-12-30-00:00:00.000
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 39827162
No problem.  With just need to include the data check in the inner filter.  I had assumed that the dates would increment as the ID value did...

SELECT *
FROM customers c
INNER JOIN order o
  ON c.customer_id = o.customer_id
WHERE order_date >= '01/01/2013'
  AND order_status = 'posted'
  AND order_id = (select max(order_id) from order o1 where o1.customer_id = o.customer_id and o1.order_status = 'posted' and order_date >= '01/01/2013')
                                           

Try that one.  :)
0
 

Author Comment

by:ckelsoe
ID: 39827170
Ok It is still not picking up the cases where the highest ercid does not have the latest posteddate. Here is the modified code looks like this:
SELECT
	c.CustomerID,
	c.Business,
	c.BusName,
	c.NameLast,
	c.NameFirst,
	c.Address1,
	c.City,
	c.State,
	c.Zip,
	c.Phone,
	c.BusPhone,
	c.Cell,
	c.Email,
	c.Pager,
	c.ResaleNumber,
	c.TaxExemptParts,
	c.TaxExemptLabor,
	c.[TimeStamp]
FROM
	dbo.Customer c
INNER JOIN dbo.ERCHistory o ON c.CustomerID = o.CustomerID
WHERE
	o.ERCStatus = 'Posted'
AND o.PostedDate >= '01/01/2006'
AND o.ERCID = (
	SELECT
		MAX (ERCID)
	FROM
		ERCHistory o1
	WHERE
		o1.CustomerID = o.CustomerID
	AND o1.ERCStatus = 'posted'
  AND o.PostedDate >= '01/01/2006'
)
ORDER BY c.CustomerID ASC

Open in new window

0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 39827184
Line 34.

It should read,

  AND o1.PostedDate >= '01/01/2006'
0
 

Author Comment

by:ckelsoe
ID: 39827212
Missed that - THANK YOU!!!!
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

715 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