Solved

Finding Not Exists in Previous Month/Period

Posted on 2014-03-12
6
349 Views
Last Modified: 2014-03-12
Hello experts,
I need help extracting data from my database where the customer was previously in my database and have transactions.  I want to find which customers have sales in the current month say February 2014, but no transactions in the previous 2 months (say Dec 2013 and Jan 2014)
I have 2 tables
Table 1:
company      cust_num      registerd_dt
100      2324      8/28/2009
100      2326      2/24/2013
100      2341      8/25/2013

Table 2:
company      cust_num      sales      month
100      2324      6      9/1/2013
100      2324      0      10/1/2013
100      2324      0      11/1/2013
100      2324      0      12/1/2013
100      2324      0      1/1/2014
100      2324      0      2/1/2014
100      2326      3      2/1/2014
100      2326      5      9/1/2013
100      2326      6      10/1/2013
100      2326      0      11/1/2013
100      2326      0      12/1/2013
100      2326      0      1/1/2014
100      2326      15      2/1/2014
100      2341      7      9/1/2013
100      2341      0      10/1/2013
100      2341      0      11/1/2013
100      2341      0      12/1/2013
100      2341      0      1/1/2014
100      2341      10      2/1/2014


Expected result:
company      cust_num      sales      month      registerd_dt
100      2326      15      2/1/2014      2/24/2013
100      2341      10      2/1/2014      8/25/2013
0
Comment
Question by:fb1990
6 Comments
 
LVL 34

Expert Comment

by:PatHartman
ID: 39924322
The simplest solution (and the most easily tested) is to create three queries.  The first for period 1, the second for period 2, and the third to combine them.

The first two queries should select only rows with > 0 sales for the period (having rows with zeros isn't helping this process so you have to get rid of them).

The third query Joins the customer table qry1 and a second time to qry2. Rows will only be returned if both queries have returned rows.  Select the columns you need from the customer table and from qry2.  Do not select any columns from qry1.  If multiple months are selected for either query, query3 will need to use Select Distinct or a group by so that it only returns a single row for the customer.

You could do this with subqueries and the Exists predicate but for most cases, I prefer this separate approach since it allows me to test each query independently.
0
 
LVL 1

Author Comment

by:fb1990
ID: 39924338
Hello PatHartman,

Do you have an example that you can share with me?  Like actual SQL code.

Thanks..
0
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39924381
select	b.company
,	b.cust_num
,	b.sales
,	b.[month]
,	a.registerd_dt
from	table1	a
join	table2	b	on	a.company = b.company
			and	a.cust_num = b.cust_num
			and	b.[month] >= 'February 1, 2014'
			and	b.[month] <  'March 1, 2014'
left
join	table2	c	on	b.company = c.company
			and	b.cust_num = c.cust_num
			and	c.[month] >= 'December 1, 2014'
			and	c.[month] <  'February 1, 2014
where	c.company IS NULL

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:fb1990
ID: 39924766
hello John_Vidmar,

This does not.  It created a bunch of duplication and give sales for Dec and Jan
0
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 500 total points
ID: 39924784
Try something like this:
SQL> WITH Table1 (Company, Cust_Num, Registerd_Dt)
  2    AS (
  3       SELECT 100, 2324, TO_DATE ( '8/28/2009', 'mm/dd/yyyy') FROM DUAL         UNION ALL
  4       SELECT 100, 2326, TO_DATE ( '2/24/2013', 'mm/dd/yyyy') FROM DUAL         UNION ALL
  5       SELECT 100, 2341, TO_DATE ( '8/25/2013', 'mm/dd/yyyy') FROM DUAL)
  6  , Table2 (Company, Cust_Num, Sales, Month_Dt)
  7    AS (
  8       SELECT 100, 2324, 6, TO_DATE ( '9/1/2013', 'mm/dd/yyyy') FROM DUAL         UNION ALL
  9       SELECT 100, 2324, 0, TO_DATE ( '10/1/2013', 'mm/dd/yyyy') FROM DUAL         UNION ALL
 10       SELECT 100, 2324, 0, TO_DATE ( '11/1/2013', 'mm/dd/yyyy') FROM DUAL         UNION ALL
 11       SELECT 100, 2324, 0, TO_DATE ( '12/1/2013', 'mm/dd/yyyy') FROM DUAL         UNION ALL
 12       SELECT 100, 2324, 0, TO_DATE ( '1/1/2014', 'mm/dd/yyyy') FROM DUAL         UNION ALL
 13       SELECT 100, 2324, 0, TO_DATE ( '2/1/2014', 'mm/dd/yyyy') FROM DUAL         UNION ALL
 14       SELECT 100, 2326, 3, TO_DATE ( '2/1/2014', 'mm/dd/yyyy') FROM DUAL         UNION ALL
 15       SELECT 100, 2326, 5, TO_DATE ( '9/1/2013', 'mm/dd/yyyy') FROM DUAL         UNION ALL
 16       SELECT 100, 2326, 6, TO_DATE ( '10/1/2013', 'mm/dd/yyyy') FROM DUAL         UNION ALL
 17       SELECT 100, 2326, 0, TO_DATE ( '11/1/2013', 'mm/dd/yyyy') FROM DUAL         UNION ALL
 18       SELECT 100, 2326, 0, TO_DATE ( '12/1/2013', 'mm/dd/yyyy') FROM DUAL         UNION ALL
 19       SELECT 100, 2326, 0, TO_DATE ( '1/1/2014', 'mm/dd/yyyy') FROM DUAL         UNION ALL
 20       SELECT 100, 2326, 15, TO_DATE ( ' 2/1/2014', 'mm/dd/yyyy') FROM DUAL         UNION ALL
 21       SELECT 100, 2341, 7, TO_DATE ( '9/1/2013', 'mm/dd/yyyy') FROM DUAL         UNION ALL
 22       SELECT 100, 2341, 0, TO_DATE ( '10/1/2013', 'mm/dd/yyyy') FROM DUAL         UNION ALL
 23       SELECT 100, 2341, 0, TO_DATE ( '11/1/2013', 'mm/dd/yyyy') FROM DUAL         UNION ALL
 24       SELECT 100, 2341, 0, TO_DATE ( '12/1/2013', 'mm/dd/yyyy') FROM DUAL         UNION ALL
 25       SELECT 100, 2341, 0, TO_DATE ( '1/1/2014', 'mm/dd/yyyy') FROM DUAL         UNION ALL
 26       SELECT 100, 2341, 10, TO_DATE ( ' 2/1/2014', 'mm/dd/yyyy') FROM DUAL)
 27  --
 28  SELECT   Company
 29         , Cust_Num
 30         , Month_Dt
 31         , Sales
 32         , Registerd_Dt
 33    FROM (
 34  SELECT   T1.Company
 35         , T1.Cust_Num
 36         , T1.Registerd_Dt
 37         , MAX( Month_Dt) Month_Dt
 38         , SUM ( DECODE (MONTHS_BETWEEN ( TO_DATE ( '2/1/2014', 'mm/dd/yyyy'), Month_Dt), 0, Sales, 0)) Sales
 39         , SUM ( DECODE (MONTHS_BETWEEN ( TO_DATE ( '2/1/2014', 'mm/dd/yyyy'), Month_Dt), 1, Sales, 0)) M1
 40         , SUM ( DECODE (MONTHS_BETWEEN ( TO_DATE ( '2/1/2014', 'mm/dd/yyyy'), Month_Dt), 2, Sales, 0)) M2
 41      FROM Table1 T1, Table2 T2
 42     WHERE T1.Company = T2.Company
 43       AND T1.Cust_Num = T2.Cust_Num
 44       AND MONTHS_BETWEEN ( TO_DATE ( '2/1/2014', 'mm/dd/yyyy'), Month_Dt) < 3
 45  GROUP BY t1.Company, t1.Cust_Num, t1.Registerd_Dt)
 46  WHERE Sales > 0 AND m1=0 AND m2=0
 47  ORDER BY 1,2,3
 48  /

   COMPANY   CUST_NUM MONTH_DT        SALES REGISTERD_
---------- ---------- ---------- ---------- ----------
       100       2326 02/01/2014         18 02/24/2013
       100       2341 02/01/2014         10 08/25/2013

SQL>

Open in new window

0
 
LVL 1

Author Closing Comment

by:fb1990
ID: 39925479
Sweet.  Worked like a charm.  Thank you!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
'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 …
This video shows how to recover a database from a user managed backup
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

707 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

13 Experts available now in Live!

Get 1:1 Help Now