Solved

Finding Not Exists in Previous Month/Period

Posted on 2014-03-12
6
379 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 36

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
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 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

Industry Leaders: 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!

Question has a verified solution.

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

Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
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…

685 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