Domanda

I have a table called 'Bill' which is a table for a database for a restaurant system. Sample data is below:

  Bill_Code,          Date,    Customer_ID, Order_Code, Total_Amount, Discount, Payable_Amount, Payment_Type, Staff_Name   
'B201404012000', '2014-03-21', 'S8909608D', 'O222224', '0.00', '0.00', '0.00', 'CASH', 'Dany Lim'   
'B201404012030', '2014-04-01', 'S8409608D', 'O222221', '100.00', '0.00', '100.00', 'VISA', 'Arya Stark'   
'B201404012032', '2014-04-01', 'S8609608D', 'O222222', '80.00', '0.10', '72.00', 'VISA', 'Arya Stark'   
'B201404012033', '2014-04-21', 'S8709608D', 'O222223', '0.00', '0.00', '0.00', 'NETS', 'Rob Targa'   
'B201404012039', '2014-04-01', 'S8709655D', 'O222225', '0.00', '0.00', '0.00', 'MASTERCARD', 'Dany Lim'   
'B201404012053', '2014-04-15', 'S8909608D', 'O222224', '0.00', '0.00', '0.00', 'CASH', 'Dany Lim'   
'B201404012115', '2014-04-11', 'S8909608D', 'O222226', '50.00', '0.00', '50.00', 'CASH', 'Rob Targa'   

I need to find which two dates with the same Customer_ID have a date interval of 30 days.

Meaning, I need to find out if any particular customer has had a lapse of 30 days between visits.

Appreciate your help. Thanks in advance.

È stato utile?

Soluzione

If you need to know returning customers with date between first and last order more than 30 days try to use GROUP BY:

SELECT Customer_ID,
       MIN(Date) as MinDate,
       MAX(Date) as MaxDate
FROM BILL
GROUP BY Customer_ID 
HAVING DATEDIFF(MAX(Date),MIN(Date))>=30

Altri suggerimenti

You try this way :

To convert a time string to the number of seconds, use the strftime function with the %s modifier. (A time string without a date part will be assumed to have the date 2000-01-01, but this cancels out when computing the differences.)

To compute the pause times for a specific production record, use a correlated subquery; the total aggregate is needed to cope with zero/one/multiple matching pauses.

For example :

SELECT date,
       item,
       sum(strftime('%s', end) - strftime('%s', begin) -
           (SELECT total(strftime('%s', end) - strftime('%s', begin))
            FROM pause
            WHERE pause.date   = production.date
              AND pause.begin >= production.begin
              AND pause.end   <= production.end)
          ) AS seconds
FROM production
GROUP BY date,
         item
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top