STATUS DATE MARKET COUNTRY ACC INCT VOL TOTAL
INC 7/2/2018 CHICAGO US 123456 AB 3 0.06
DEL 7/3/2018 CHICAGO US 123456 AB 3 0.06
INC 7/3/2018 CHICAGO US 67890 AB 3 0.06
INC 7/6/2018 CHICAGO US 700000 CBO 25 0.5
INC 7/11/2018 EUROPE WG 253235 EFDX 1 0.02
INC 7/18/2018 NEWYORK US 700000 RTY 2 0.04
INC 7/24/2018 CHICAGO US 700000 CBO 2 0.04
INC 7/25/2018 EUROPE WG 253235 EFDX 1 0.02
在(inc,del)下,如果我发现MARKET,COUNTRY,ACC,INCT,VOL,TOTAL相同,那么我只需要取第三条记录就消除前两个记录 INC 7/3/2018芝加哥美国67890 AB 3 0.06
我的预期输出是
STATUS DATE MARKET COUNTRY ACC INCT VOL TOTAL <br>
INC 7/3/2018 CHICAGO US 67890 AB 3 0.06 <br>
INC 7/6/2018 CHICAGO US 700000 CBO 25 0.5<br>
INC 7/11/2018 EUROPE WG 253235 EFDX 1 0.02<br>
INC 7/18/2018 NEWYORK US 700000 RTY 2 0.04<br>
INC 7/24/2018 CHICAGO US 700000 CBO 2 0.04<br>
INC 7/25/2018 EUROPE WG 253235 EFDX 1 0.02<br>
我只需要在SQL Server和Oracle中的SQL中执行此操作。我将不胜感激。非常感谢。
答案 0 :(得分:1)
使用公用表表达式(CTE)找出哪些行具有偏移的“ INC”和“ DEL”记录。然后,从该CTE中选择并排除那些。像这样:
WITH tmp as (
SELECT id.*,
sum(decode(status,'INC',total,'DEL',-total))
over ( partition by market, country, acc, inct, vol, total) incdel
FROM input_data id )
SELECT status, trx_date, market, country, acc, inct, vol, total
FROM tmp
WHERE incdel != 0;
这是一个完整的示例,其中包含测试数据:
with input_data (STATUS, TRX_DATE, MARKET, COUNTRY, ACC, INCT, VOL, TOTAL ) AS (
SELECT 'INC', TO_DATE('7/2/2018','MM/DD/YYYY'), 'CHICAGO', 'US', 123456, 'AB', 3, 0.06 FROM DUAL UNION ALL
SELECT 'DEL', TO_DATE('7/3/2018','MM/DD/YYYY'), 'CHICAGO', 'US', 123456, 'AB', 3, 0.06 FROM DUAL UNION ALL
SELECT 'INC', TO_DATE('7/3/2018','MM/DD/YYYY'), 'CHICAGO', 'US', 67890, 'AB', 3, 0.06 FROM DUAL UNION ALL
SELECT 'INC', TO_DATE('7/6/2018','MM/DD/YYYY'), 'CHICAGO', 'US', 700000, 'CBO', 25, 0.5 FROM DUAL UNION ALL
SELECT 'INC', TO_DATE('7/11/2018','MM/DD/YYYY'), 'EUROPE', 'WG', 253235, 'EFDX', 1, 0.02 FROM DUAL UNION ALL
SELECT 'INC', TO_DATE('7/18/2018','MM/DD/YYYY'), 'NEWYORK', 'US', 700000, 'RTY', 2, 0.04 FROM DUAL UNION ALL
SELECT 'INC', TO_DATE('7/24/2018','MM/DD/YYYY'), 'CHICAGO', 'US', 700000, 'CBO', 2, 0.04 FROM DUAL UNION ALL
SELECT 'INC', TO_DATE('7/25/2018','MM/DD/YYYY'), 'EUROPE', 'WG', 253235, 'EFDX', 1, 0.02 FROM DUAL ),
tmp as (
SELECT id.*,
sum(decode(status,'INC',total,'DEL',-total))
over ( partition by market, country, acc, inct, vol, total) incdel
FROM input_data id )
SELECT status, trx_date, market, country, acc, inct, vol, total
FROM tmp
WHERE incdel != 0;
结果:
+--------+-----------+---------+---------+--------+------+-----+-------+
| STATUS | TRX_DATE | MARKET | COUNTRY | ACC | INCT | VOL | TOTAL |
+--------+-----------+---------+---------+--------+------+-----+-------+
| INC | 03-JUL-18 | CHICAGO | US | 67890 | AB | 3 | 0.06 |
| INC | 24-JUL-18 | CHICAGO | US | 700000 | CBO | 2 | 0.04 |
| INC | 06-JUL-18 | CHICAGO | US | 700000 | CBO | 25 | 0.5 |
| INC | 25-JUL-18 | EUROPE | WG | 253235 | EFDX | 1 | 0.02 |
| INC | 11-JUL-18 | EUROPE | WG | 253235 | EFDX | 1 | 0.02 |
| INC | 18-JUL-18 | NEWYORK | US | 700000 | RTY | 2 | 0.04 |
+--------+-----------+---------+---------+--------+------+-----+-------+
答案 1 :(得分:0)
有几种方法可以实现此目的。您可以执行2 CTE。发布您的查询将有所帮助!该查询不完整,您必须完成它。但是主要的想法在这里。
;with subQry1 (
select STATUS DATE MARKET COUNTRY ACC INCT VOL, sum(??) total,
from table
where ...
group by STATUS DATE MARKET COUNTRY ACC INCT VOL
), subQry2 (
select STATUS DATE MARKET COUNTRY ACC INCT VOL, total,
rownumber() over(partition by MARKET, COUNTRY,ACC, INCT, VOL, TOTAL order by MARKET, COUNTRY,ACC, INCT, VOL, TOTAL) rnk
from subQry1
) select * from subQry2 where rnk = 1;
答案 2 :(得分:0)
听起来像'DEL'
会“删除” 'INC'
隐藏的记录吗?这表明NOT EXISTS
可能会有所帮助。
在不知道实际查询或架构的情况下,我无法为您提供有效的查询,只是一个大概的样子:
SELECT T1.STATUS,
T1.DATE,
T1.MARKET,
...
T1.TOTAL
FROM ELBAT T1
WHERE T1.STATUS = 'INC'
AND NOT EXISTS (SELECT *
FROM ELBAT T2
WHERE T2.STATUS = 'DEL'
AND T2.MARKET = T1.MARKET
...
AND T2.TOTAL = T1.TOTAL);