SQL如何链接另一个选择的最低行

时间:2019-01-12 18:42:46

标签: sql sql-server select group-by

我从一个星期开始搜索此问题。我尝试了许多代码,但无法获得正确的答案,这就是问题所在: 我有一张卖方表格,每一次有客户联系该卖方时,无论他是否出售了某物,都是如此。

表格卖方(我将只为一位卖方提供数据,以便为您提供更具体的问题)

  • SellerID-电话呼叫日期-序列-销售(布尔)
  • 100-2018-01-05-1-0
  • 100-2018-01-05-2-0
  • 100-2018-01-05-3-1
  • 100-2018-01-05-4-0
  • 100-2018-01-05-5-0
  • 100-2018-01-10-1-0
  • 100-2018-01-10-2-0
  • 100-2018-01-10-3-0
  • 100-2018-01-10-4-1
  • 100-2018-01-10-5-0
  • 100-2018-01-10-6-0
  • 100-2018-01-10-7-0
  • 100-2018-01-10-8-0
  • 100-2018-01-10-9-0
  • 100-2018-01-10-10-0

我想:两次电话之间他没有卖出多少次。 我应该在这个模型中有3个答案:

1- a sequence of 2 during  2018-01-05 sequence 1 to  2018-01-05 sequence 2
2- a sequence of 5 during  2018-01-05 sequence 4 to  2018-01-10 sequence 3 
2- a sequence of 6 during  2018-01-10 sequence 5 to  2018-01-10 sequence 10

我现在所做的是:我尝试查找边界并获取这2个边界之间的计数(我知道数据库从2018年1月5日开始),所以我将2018年1月1日作为最低日期最低边界在左侧联接中,我使用getdate()+ 1将明天日期作为最高边界:

Select '2018-01-01' as Date1, SellerID as Seller1, 0 as Sequence1  from Seller  Group by Seller
Union ALL
Select phonecalldate as Date1, SellerID as Seller1, 0 as Sequence1 from Seller  Where Selling = 'true' group by phonecalldate , SellerID , Sequence
) as Boundary1
Left JOIN (
Select * from (
Select getdate()+1 as Date2, SellerID as Seller2, 0 as Sequence2 from Seller    Group by Seller
Union ALL
Select phonecalldate as DateMatch2, SellerID as Seller2, Sequence as Sequence2 from Seller  Where Selling = 'true' ) as Boundary2 
ON Boundary1.Seller1 = Boundary2 .Seller2 and ((Boundary1.Date1 = Boundary2.Date2 and Boundary1.Sequence1 < Boundary2 .Sequence2) or (Boundary1.Date1 < Boundary2.Date2 )) 

此查询返回:

  • (第一个查询)左联接(第二个查询)
  • 2018-01-01-100-0-2018-01-05-100-3
  • 2018-01-01-100-0-2018-01-10-100-4
  • 2018-01-01-100-0-2019-01-12-100-0
  • 2018-01-05-100-3-2018-01-10-100-4
  • 2018-01-05-100-3-2019-01-12-100-0
  • 2018-01-10-100-4-2019-01-12-100-0

如何像这样每个卖方1最低价?

  • 2018-01-01-100-0-2018-01-05-100-3
  • 2018-01-05-100-3-2018-01-10-100-4
  • 2018-01-10-100-4-2019-01-12-100-0

以同样的方式如何计算两个日期之间的总数? 结果应该可以给我(此) 这就是我真正想要的!

  • 2018-01-01-100-0-2018-01-05-100-3 3 (2)
  • 2018-01-05-100-3-2018-01-10-100-4 (5)
  • 2018-01-10-100-4-2019-01-12-100-0 (6)

现在我有了界限,我想我可以找到几个:卖=假 在我的结果之间。

有人有更好的方法来查询此消息吗?

谢谢

1 个答案:

答案 0 :(得分:1)

我希望这部分由问题的作者完成。

DECLARE @Seller TABLE (
  SellerID INT,
  phonecalldate DATE,
  [Sequence] INT,
  Selling INT CHECK(Selling IN(0, 1)),
  PRIMARY KEY (SellerID, phonecalldate, [Sequence])
);

INSERT @Seller(SellerID, phonecalldate, [Sequence], Selling)
VALUES
  (100, '20180105', 1, 0),
  (100, '20180105', 2, 0),
  (100, '20180105', 3, 1),
  (100, '20180105', 4, 0),
  (100, '20180105', 5, 0),
  (100, '20180110', 1, 0),
  (100, '20180110', 2, 0),
  (100, '20180110', 3, 0),
  (100, '20180110', 4, 1),
  (100, '20180110', 5, 0),
  (100, '20180110', 6, 0),
  (100, '20180110', 7, 0),
  (100, '20180110', 8, 0),
  (100, '20180110', 9, 0),
  (100, '20180110', 10, 0);

可以使用以下查询获取获得所需结果所需的数据:

WITH
  a AS (
    SELECT
      SellerID,
      Selling,
      FORMAT(phonecalldate, 'yyyyMMdd') +
        FORMAT([Sequence], '00000000') AS dump,
      SUM(Selling) OVER
      (
        PARTITION BY SellerID
        ORDER BY phonecalldate, [Sequence]
      ) AS g
    FROM @Seller
  )
SELECT
  COUNT(*) AS PhoneCallQty,
  CAST(LEFT(MIN(dump), 8) AS DATE) AS StartDate,
  CAST(RIGHT(MIN(dump), 8) AS INT) AS StartSeq,
  CAST(LEFT(MAX(dump), 8) AS DATE) AS EndDate,
  CAST(RIGHT(MAX(dump), 8) AS INT) AS EndSeq
FROM a
WHERE Selling = 0
GROUP BY SellerID, g;

输出:

+--------------+------------+----------+------------+--------+
| PhoneCallQty | StartDate  | StartSeq |  EndDate   | EndSeq |
+--------------+------------+----------+------------+--------+
|            2 | 2018-01-05 |        1 | 2018-01-05 |      2 |
|            5 | 2018-01-05 |        4 | 2018-01-10 |      3 |
|            6 | 2018-01-10 |        5 | 2018-01-10 |     10 |
+--------------+------------+----------+------------+--------+