它的关键......可能有人分享你的专业知识。
DDL: -
CREATE TABLE mytable
(
SUPERID INT,
DATE_RECORDED DATETIME2,
SUPPLIER_SKU VARCHAR (50),
PRICEINCVAT DECIMAL (14,4),
[DESC] VARCHAR (100),
FROM_DATE DATETIME2,
ACTIVE BIT
)
INSERT mytable
SELECT 1000001, '2015-12-18 06:45:00.0000000', '0 986 490 640', '29.5400', 'CAPACITOR', '2015-12-18 06:45:00.0000000',0 UNION ALL
SELECT 1000001, '2015-12-18 06:45:00.0000000', '2347402', '32.6900', 'CAPACITOR', '2015-12-18 06:45:00.0000000', 0 UNION ALL
SELECT 1000001, '2015-12-18 06:45:00.0000000', 'FDB779', '30.9200', 'CAPACITOR', '2015-12-18 06:45:00.0000000',0 UNION ALL
SELECT 1000001, '2015-12-18 06:45:00.0000000', 'GDB1577', '29.3600', 'CAPACITOR', '2015-12-18 06:45:00.0000000',0UNION ALL
SELECT 1000001, '2016-03-03 02:22:00.0000000', '0 986 490 640', '29.5400','CAPACITOR','2016-03-03 02:22:00.0000000' ,1UNION ALL
SELECT 1000001, '2016-03-03 02:22:00.0000000', '2347402', '32.6900', 'CAPACITOR', '2016-03-03 02:22:00.0000000',1UNION ALL
SELECT 1000001, '2016-03-03 02:22:00.0000000', 'FDB779', '30.1800', 'CAPACITOR', '2016-03-03 02:22:00.0000000',1UNION ALL
SELECT 1000001, '2016-03-03 02:22:00.0000000', 'GDB1577','29.3600', 'CAPACITOR', '2016-03-03 02:22:00.0000000',1 UNION ALL
SELECT 1000017, '2015-12-18 06:47:00.0000000', 'GDB1496', '29.5400', 'CAPACITOR', '2015-12-18 06:47:00.0000000', 0 UNION ALL
SELECT 1000017, '2016-03-03 02:24:00.0000000', 'GDB1496', '29.5400', 'CAPACITOR', '2016-03-03 02:24:00.0000000', 1 UNION ALL
SELECT 1000653, '3/8/2016 11:04:00 PM', 'FDB1751', '29.5400', 'CAPACITOR', '3/2/2016 11:04:00 PM', 1 UNION ALL
SELECT 1000653, '12/12/2015 7:12:00 AM', 'FDB1751', '29.5400', 'CAPACITOR', '12/18/2015 7:12:00 AM', 0 UNION ALL
SELECT 1000653, '2015-12-18 07:12:00.0000000','0 986 494 096','29.5400', 'CAPACITOR', '2015-12-18 07:12:00.0000000',0 UNION ALL
SELECT 1000653, '2016-03-02 23:04:00.0000000', '0 986 494 096','29.5400', 'CAPACITOR', '2016-03-02 23:04:00.0000000',1 UNION ALL
SELECT 1000653, '2016-03-02 23:04:00.0000000', '16531','29.5400', 'CAPACITOR', '2016-03-02 23:04:00.0000000',1 UNION ALL
SELECT 1000653, '2015-12-18 07:12:00.0000000','2393501','29.5400', 'CAPACITOR','2015-12-18 07:12:00.0000000',0 UNION ALL
SELECT 1000653, '2016-03-02 23:04:00.0000000', '2393501','29.5400', 'CAPACITOR','2016-03-02 23:04:00.0000000',1 UNION ALL
SELECT 1000653, '2015-12-18 07:12:00.0000000','FDB1751','29.5400', 'CAPACITOR','2015-12-18 07:12:00.0000000',0 UNION ALL
SELECT 1000653, '2016-03-02 23:04:00.0000000','FDB1751','29.5400', 'CAPACITOR','2016-03-02 23:04:00.0000000',1 UNION ALL
SELECT 1000653, '2015-12-18 07:12:00.0000000','GDB1625','29.5400', 'CAPACITOR', '2015-12-18 07:12:00.0000000',0 UNION ALL
SELECT 1000653, '2016-03-02 23:04:00.0000000','GDB1625','29.5400', 'CAPACITOR','2016-03-02 23:04:00.0000000',1 UNION ALL
SELECT 1000653, '2015-12-18 07:12:00.0000000','LP1925','29.5400', 'CAPACITOR', '2015-12-18 07:12:00.0000000',1
我正在尝试计算TO_DATE的值,该值比FROM_DATE的SUPERID订单组FROM_DATE中可用的最高日期少1秒。其中ACTIVE COLUMN值为0.但在ACTIVE列= 1的情况下,那么它将是默认值(9999-12-31 00:00:00.0000000)
预期产出:
SUPERID DATE_RECORDED SUPPLIER_SKU PRICEINCVAT DESC FROM_DATE TO_DATE ACTIVE
1000001 2015-12-18 06:45:00.0000000 0 986 490 640 29.5400 CAPACITOR 2015-12-18 06:45:00.0000000 2016-03-03 02:21:59.0000000 0
1000001 2015-12-18 06:45:00.0000000 2347402 32.6900 CAPACITOR 2015-12-18 06:45:00.0000000 2016-03-03 02:21:59.0000000 0
1000001 2015-12-18 06:45:00.0000000 FDB779 30.9200 CAPACITOR 2015-12-18 06:45:00.0000000 2016-03-03 02:21:59.0000000 0
1000001 2015-12-18 06:45:00.0000000 GDB1577 29.3600 CAPACITOR 2015-12-18 06:45:00.0000000 2016-03-03 02:21:59.0000000 0
1000001 2016-03-03 02:22:00.0000000 0 986 490 640 29.5400 CAPACITOR 2016-03-03 02:22:00.0000000 9999-12-31 00:00:00.0000000 1
1000001 2016-03-03 02:22:00.0000000 2347402 32.6900 CAPACITOR 2016-03-03 02:22:00.0000000 9999-12-31 00:00:00.0000000 1
1000001 2016-03-03 02:22:00.0000000 FDB779 30.1800 CAPACITOR 2016-03-03 02:22:00.0000000 9999-12-31 00:00:00.0000000 1
1000001 2016-03-03 02:22:00.0000000 GDB1577 29.3600 CAPACITOR 2016-03-03 02:22:00.0000000 9999-12-31 00:00:00.0000000 1
1000017 2015-12-18 06:47:00.0000000 GDB1496 29.5400 CAPACITOR 2015-12-18 06:47:00.0000000 2016-03-03 02:23:59.0000000 0
1000017 2016-03-03 02:24:00.0000000 GDB1496 29.5400 CAPACITOR 2016-03-03 02:24:00.0000000 9999-12-31 00:00:00.0000000 1
1000653 2015-12-12 07:12:00.0000000 FDB1751 29.5400 CAPACITOR 2015-12-18 07:12:00.0000000 2016-03-02 23:04:00.0000000 0
1000653 2015-12-18 07:12:00.0000000 0 986 494 096 29.5400 CAPACITOR 2015-12-18 07:12:00.0000000 2016-03-02 23:04:00.0000000 0
1000653 2015-12-18 07:12:00.0000000 2393501 29.5400 CAPACITOR 2015-12-18 07:12:00.0000000 2016-03-02 23:04:00.0000000 0
1000653 2015-12-18 07:12:00.0000000 FDB1751 29.5400 CAPACITOR 2015-12-18 07:12:00.0000000 2016-03-02 23:04:00.0000000 0
1000653 2015-12-18 07:12:00.0000000 GDB1625 29.5400 CAPACITOR 2015-12-18 07:12:00.0000000 2016-03-02 23:04:00.0000000 0
1000653 2015-12-18 07:12:00.0000000 LP1925 29.5400 CAPACITOR 2015-12-18 07:12:00.0000000 9999-12-31 00:00:00.0000000 1
1000653 2016-03-02 23:04:00.0000000 GDB1625 29.5400 CAPACITOR 2016-03-02 23:04:00.0000000 9999-12-31 00:00:00.0000000 1
1000653 2016-03-02 23:04:00.0000000 FDB1751 29.5400 CAPACITOR 2016-03-02 23:04:00.0000000 9999-12-31 00:00:00.0000000 1
1000653 2016-03-02 23:04:00.0000000 2393501 29.5400 CAPACITOR 2016-03-02 23:04:00.0000000 9999-12-31 00:00:00.0000000 1
1000653 2016-03-02 23:04:00.0000000 0 986 494 096 29.5400 CAPACITOR 2016-03-02 23:04:00.0000000 9999-12-31 00:00:00.0000000 1
1000653 2016-03-02 23:04:00.0000000 16531 29.5400 CAPACITOR 2016-03-02 23:04:00.0000000 9999-12-31 00:00:00.0000000 1
1000653 2016-03-08 23:04:00.0000000 FDB1751 29.5400 CAPACITOR 2016-03-02 23:04:00.0000000 9999-12-31 00:00:00.0000000 1
代码我试过但没有给出正确的结果:
;WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY SUPERID ORDER BY FROM_DATE) as RN
FROM mytable m
)
SELECT c.SUPERID, c.DATE_RECORDED, c.SUPPLIER_SKU, c.PRICEINCVAT, c.[DESC], c.FROM_DATE,
CASE
WHEN c.ACTIVE = 0 THEN DATEADD(second, -1, c2.FROM_DATE)
ELSE '9999-12-31'
END as TO_DATE,
c.ACTIVE
FROM cte c
LEFT JOIN cte c2 ON c.SUPERID = c2.SUPERID
AND c.RN = c2.RN - 1
如果有可能在SQL中请愿人分享。 感谢
答案 0 :(得分:0)
我发现这个问题难以理解。但是,我期待这样的结构:
select m.*,
dateadd(second, -1,
max(from_date) over (partition by superid)
) as new_to_date
from mytable m;
我不知道row_number()
如何适应逻辑。
答案 1 :(得分:0)
您应该可以使用OUTER APPLY来获取MAX(FROM_DATE) - 1秒。
SELECT SUPERID,
DATE_RECORDED,
SUPPLIER_SKU,
PRICEINCVAT,
[DESC],
FROM_DATE,
CASE WHEN ACTIVE = 1 THEN '9999-12-31' ELSE TO_DATE END AS TO_DATE,
ACTIVE
FROM mytable mt
OUTER APPLY ( SELECT MAX(DATEADD(second, -1, FROM_DATE)) TO_DATE
FROM mytable mt2
WHERE mt2.SUPERID = mt.SUPERID
AND mt2.SUPPLIER_SKU = mt.SUPPLIER_SKU
AND mt2.From_DATE > mt.From_DATE ) oa