所以基本上我想知道如果日期不同,如何在日期上联接两个表。第一个表是我的主表,其中包括我所有已购买商品的客户。购买日期是过去的一点:
Customers
+--------+----------+-------+------------+
| custid | Quantity | Price | ReportDate |
+--------+----------+-------+------------+
| 371965 | 12 | 2 | 9/1/2016 |
| 371965 | 2 | 5 | 2/25/2018 |
| 377958 | 45 | 3 | 9/1/2016 |
| 270723 | 12 | 1.25 | 5/1/2014 |
| 270723 | 10.86 | 1.25 | 6/1/2014 |
| 270723 | 12.29 | 1.3 | 7/1/2014 |
| 270723 | 12.29 | 1.4 | 9/15/2016 |
+--------+----------+-------+------------+
所以我想加入折扣表中的我的客户表。折扣生效的时间基本上是整个时间,直到发出新折扣为止:
Discounts
+----+-----------+----------+
| id | startdate | discount |
+----+-----------+----------+
| 1 | 7/18/2013 | 0.1 |
| 2 | 1/10/2014 | 0.25 |
| 3 | 7/11/2016 | 0.11 |
| 4 | 9/14/2016 | 0.12 |
| 5 | 1/12/2017 | 0.15 |
| 6 | 2/6/2017 | 0.22 |
| 7 | 6/28/2017 | 0.09 |
+----+-----------+----------+
所以我的目标是链接两个表,并查看哪个购买日期在折扣的适当间隔内。这就是我的目标:
+--------+----------+-------+------------+----+-----------+----------+
| custid | Quantity | Price | ReportDate | id | startdate | discount |
+--------+----------+-------+------------+----+-----------+----------+
| 371965 | 12 | 2 | 9/1/2016 | 3 | 7/11/2016 | 0.11 |
| 371965 | 2 | 5 | 2/25/2018 | 7 | 6/28/2017 | 0.09 |
| 377958 | 45 | 3 | 9/1/2016 | 3 | 7/11/2016 | 0.11 |
| 270723 | 12 | 1.25 | 5/1/2014 | 2 | 41649 | 0.25 |
| 270723 | 10.86 | 1.25 | 6/1/2014 | 2 | 1/10/2014 | 0.25 |
| 270723 | 12.29 | 1.3 | 7/1/2014 | 2 | 1/10/2014 | 0.25 |
| 270723 | 12.29 | 1.4 | 9/15/2016 | 4 | 9/14/2016 | 0.12 |
+--------+----------+-------+------------+----+-----------+----------+
感谢您的帮助!!!!!!!
答案 0 :(得分:3)
您可以使用OUTER APPLY选择报告日期之前存在的TOP 1折扣,按日期降序排列以获取最新的运行时间
SELECT C1.*,DQ.* FROM CUSTOMERS C1 OUTER APPLY
(SELECT TOP 1 D.* FROM Discounts D WHERE C1.ReportDate >= D.startDate
ORDER BY D.StartDate DESC) DQ
答案 1 :(得分:2)
您可以这样加入:
switch (mimeType) {
case "application/vnd.google-apps.spreadsheet": {
mimeType = "text/csv"; //CSV: WORKING
//mimeType = "application/x-vnd.oasis.opendocument.spreadsheet"; //OPEN OFFICE: NOT WORKING (GARBLED)
//mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; //MS EXCEL: NOT WORKING (GARBLED)
break;
}
case "application/vnd.google-apps.document": {
//mimeType = "text/plain"; //PLAIN TEXT: WORKING (DOUBLE TEXT IN THE FILE, BUT NOT THE API EXPLORER)
mimeType = "application/rtf"; //RICH TEXT: WORKING
//mimeType = "application/vnd.oasis.opendocument.text"; //OPEN OFFICE DOC: NOT WORKING (GARBLED)
//mimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; //MS WORD DOCUMENT (GARBLED)
break;
}
case "application/vnd.google-apps.presentation": {
mimeType = "application/pdf"; //PLAIN TEXT: WORKING
//mimeType = "application/vnd.openxmlformats- officedocument.presentationml.presentation"; //MS POWER POINT (GARBLED)
//mimeType = "application/vnd.oasis.opendocument.presentation"; //OPEN OFFICE (GARBLED)
break;
}
case "application/vnd.google-apps.drawing": {
mimeType = "image/png"; //PNG: WORKING
//mimeType = "image/jpeg"; //JPEG: WORKING
//mimeType = "image/pdf"; //NOT WORKING (INVALID FILE: ACROBAT DOESN'T LIKE IT)
break;
}
default: {
googleDoc = false;
break;
}
}
请参见demo
答案 2 :(得分:2)
另一个选择
;with cte as (
Select A.*
,B.*
,RN = row_number() over (Partition by custid,reportdate order by startdate desc)
From Customers A
Join Discounts B on B.startdate<=A.ReportDate
)
Select *
From cte
Where RN=1