两个客户订单之间的持续时

时间:2017-06-22 09:31:13

标签: sql sql-server

我在使用SQL查询时遇到了一些困难。表名为Customer_Orders,包含以下列

Order_ID INT -Primary Key  
Customer_ID INT - Customer who made the transaction  
Order_Time_Stamp DATE - Date and time of transaction
Item STRING - Description of item purchased
Transaction_Status STRING - Status can be "Success" and "Failure"

如果第一个订单成功的客户,我们怎样才能在30天内获得每个客户的第一个订单和第二个订单之间的持续时间?

我的查询逻辑类似于:

select Customer_ID, Duration_Between_Order_In_Days
from Customer_Orders
where DATEDIFF(DAYS, First_Order_Date, Second_Order_Date) < 30
and First_Order_Status='Success'

我尝试过使用DATEDIFF()函数和自联接和子查询,但我不知道如何为每个客户找到第二个订单。任何想法将不胜感激!

2 个答案:

答案 0 :(得分:0)

select Customer_ID, Duration_Between_Order_In_Days

    from Customer_Orders

    where DATEDIFF(First_Order_Date, Second_Order_Date) < 30

and First_Order_Status='Success'

如上面的查询所示,试试这个。

只是为了关心您的First_Order_Date, Second_Order_Date必须采用'YYYY-MM-DD'的形式。

希望这会对你有所帮助。

答案 1 :(得分:0)

使用adventureworks2012 [Sales]。[SalesOrderHeader]前10条记录如下(在将客户11000的第一个salesorderid的状态重置为0之后),其中5表示成功

+--------------+---------------------+------------+--------+
| salesorderid | orderdate           | customerid | status |
+--------------+---------------------+------------+--------+
|            1 | 2009-10-13 00:00:00 |          0 |      0 |
|        43793 | 2005-07-22 00:00:00 |      11000 |      0 |
|        51522 | 2007-07-22 00:00:00 |      11000 |      5 |
|        57418 | 2007-11-04 00:00:00 |      11000 |      5 |
|        43767 | 2005-07-18 00:00:00 |      11001 |      5 |
|        51493 | 2007-07-20 00:00:00 |      11001 |      5 |
|        72773 | 2008-06-12 00:00:00 |      11001 |      5 |
|        43736 | 2005-07-10 00:00:00 |      11002 |      5 |
|        51238 | 2007-07-04 00:00:00 |      11002 |      5 |
|        53237 | 2007-08-27 00:00:00 |      11002 |      5 |
+--------------+---------------------+------------+--------+
10 rows in set (0.18 sec)

Mysql的

select f.customerid,f.firstorder,o2.`Status`,s.secondorder,o3.`status`,
        o2.OrderDate, o3.OrderDate, datediff(o3.OrderDate, o2.OrderDate) daysbetweenorders
from
(
select customerid,min(salesorderid) firstorder
from orders 
where #customerid = 11000   and 
        status = 5
group by customerid
) f
join
(
select customerid,min(o.salesorderid) secondorder
from orders o
where #o.customerid = 11000 and 
        o.salesorderid > (select min(salesorderid) from orders o1 where o1.customerid = o.customerid and o1.Status = 5)
group by customerid
) s on s.customerid = f.customerid
join orders o2 on o2.SalesOrderID = f.firstorder
join orders o3 on o3.SalesOrderID = s.secondorder

limit 10

结果

+------------+------------+--------+-------------+--------+---------------------+---------------------+-------------------+
| customerid | firstorder | Status | secondorder | status | OrderDate           | OrderDate           | daysbetweenorders |
+------------+------------+--------+-------------+--------+---------------------+---------------------+-------------------+
|      11000 |      51522 |      5 |       57418 |      5 | 2007-07-22 00:00:00 | 2007-11-04 00:00:00 |               105 |
|      11001 |      43767 |      5 |       51493 |      5 | 2005-07-18 00:00:00 | 2007-07-20 00:00:00 |               732 |
|      11002 |      43736 |      5 |       51238 |      5 | 2005-07-10 00:00:00 | 2007-07-04 00:00:00 |               724 |
|      11003 |      43701 |      5 |       51315 |      5 | 2005-07-01 00:00:00 | 2007-07-09 00:00:00 |               738 |
|      11004 |      43810 |      5 |       51595 |      5 | 2005-07-26 00:00:00 | 2007-07-26 00:00:00 |               730 |
|      11005 |      43704 |      5 |       51612 |      5 | 2005-07-02 00:00:00 | 2007-07-27 00:00:00 |               755 |
|      11006 |      43819 |      5 |       51198 |      5 | 2005-07-27 00:00:00 | 2007-07-02 00:00:00 |               705 |
|      11007 |      43743 |      5 |       51581 |      5 | 2005-07-12 00:00:00 | 2007-07-25 00:00:00 |               743 |
|      11008 |      43826 |      5 |       51282 |      5 | 2005-07-28 00:00:00 | 2007-07-07 00:00:00 |               709 |
|      11009 |      43837 |      5 |       51562 |      5 | 2005-07-30 00:00:00 | 2007-07-24 00:00:00 |               724 |
+------------+------------+--------+-------------+--------+---------------------+---------------------+-------------------+
10 rows in set (0.77 sec)

Sql server 非常相似,但状态需要转义,datediff语法不同,top替换限制

select top 10 f.customerid,f.firstorder,o2.[Status],s.secondorder,o3.[status],
        o2.OrderDate, o3.OrderDate, datediff(d,o2.OrderDate, o3.OrderDate) daysbetweenorders
from
(
select customerid,min(salesorderid) firstorder
from [Sales].[SalesOrderHeader] 
where   --customerid = 11000    and 
        status = 5
group by customerid
) f
join
(
select customerid,min(o.salesorderid) secondorder
from [Sales].[SalesOrderHeader] o
where   --o.customerid = 11000 and 
        o.salesorderid > (select min(salesorderid) from [Sales].[SalesOrderHeader] o1 where o1.customerid = o.customerid and o1.Status = 5)
group by customerid
) s on s.customerid = f.customerid
join [Sales].[SalesOrderHeader] o2 on o2.SalesOrderID = f.firstorder
join [Sales].[SalesOrderHeader] o3 on o3.SalesOrderID = s.secondorder
order by f.customerid

注意: - 确保你有合适的密钥 - 我在下面看到了mysql(不能保证这些是你的应用程序)。

CREATE TABLE `orders` (
  `SalesOrderID` int(11) NOT NULL,
  `RevisionNumber` int(11) NOT NULL,
  `OrderDate` varchar(50) NOT NULL,
  `DueDate` varchar(50) NOT NULL,
  `ShipDate` varchar(50) DEFAULT NULL,
  `Status` int(11) NOT NULL,
  `OnlineOrderFlag` int(11) NOT NULL,
  `SalesOrderNumber` int(11) DEFAULT NULL,
  `PurchaseOrderNumber` int(11) DEFAULT NULL,
  `AccountNumber` int(11) DEFAULT NULL,
  `CustomerID` int(11) NOT NULL,
  `SalesPersonID` int(11) DEFAULT NULL,
  `TerritoryID` int(11) DEFAULT NULL,
  `BillToAddressID` int(11) NOT NULL,
  `ShipToAddressID` int(11) NOT NULL,
  `ShipMethodID` int(11) NOT NULL,
  `CreditCardID` int(11) DEFAULT NULL,
  `CreditCardApprovalCode` varchar(15) DEFAULT NULL,
  `CurrencyRateID` int(11) DEFAULT NULL,
  `SubTotal` decimal(10,2) NOT NULL,
  `TaxAmt` decimal(10,2) NOT NULL,
  `Freight` decimal(10,2) NOT NULL,
  `TotalDue` decimal(10,2) DEFAULT NULL,
  `Comment` varchar(128) DEFAULT NULL,
  `rowguid` varchar(50) DEFAULT NULL,
  `ModifiedDate` varchar(50) NOT NULL,
  KEY `k001` (`OrderDate`,`SalesOrderID`),
  KEY `k1` (`CustomerID`,`SalesOrderID`),
  KEY `k2` (`SalesOrderID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1