如何解决选择总和不为零的行
我有两个表:#temptable_1
和#temptable_2
。我需要选择其他表中没有可减去的行。让我澄清一下:
-
带有“代码1”列和“代码1”列的行的“价格”值为“ -2000”。具有“代码4”列的行的“价格”值为“ 2000”。 这意味着,如果我们对这些列求和,则总和将为零。所以这两行 不应选择。
-
具有“代码2”列和“代码”列的行具有值为“ -10780.56”的“金额”。列“代码5”的行的“金额”值为“ 10780.56”。 这意味着,如果我们对这些列求和,则总和将为零。所以这两行 不应选择。
-
具有“代码3”列和“代码”列的行具有值为“ -12936.67”的“ AmountIncVAT”。具有“代码6”列的行具有一个“ AmountIncVAT”,其值为“ 12936.67”。这意味着,如果我们对这些列求和,则总和将为零。所以这两行 不应选择。
-
具有“代码”列和“代码7”列的行应位于选择结果中,因为它不能与
temptable_1
的其他列求和
因此,可以汇总的列为:价格,增值税,金额,金额IncVAT
表格如下:
DROP TABLE IF EXISTS #temptable_1
CREATE TABLE #temptable_1 ( [Name] varchar(10),[Code] varchar(50),[id_Car] int,[PositionName] varchar(1000),[Count] decimal(18,3),[id_Unit] int,[Price] decimal(15,2),[VAT] decimal(15,[Amount] decimal(15,[AmountIncVAT] decimal(15,2))
INSERT INTO #temptable_1
VALUES
( '1','Code 1',632324,'Position 1',1.000,1036,-2000.00,-401.00,2001.00,2401.00),( '2','Code 2','Position 2',-10780.56,-2156.11,10.67),( '3','Code 3','Position 3',-18780.56,-1,558.56,-12936.67)
DROP TABLE IF EXISTS #temptable_2
CREATE TABLE #temptable_2 ( [Name] varchar(10),2))
INSERT INTO #temptable_2
VALUES
( '4','Code 4','Position 4',2000.00,402.00,3001.00,8101.00),( '5','Code 5','Position 5',11780.56,2156.11,10780.56,18936.67),( '6','Code 6','Position 6',10580.56,3,-21780.56,12936.67),( '7','Code 7','Position 7',8.000,NULL,415.34,664.54,3322.72,3987.26)
期望的结果是用“代码7”显示一行:
7 Code 6 632324 Position 6 8.000 NULL 415.34 664.54 3322.72 3987.26
我写了以下代码:
SELECT
*
FROM
(
SELECT
COUNT(1) OVER (PARTITION BY ABS(q.Price) ORDER BY ABS(q.Price)) as CountByPrice,COUNT(1) OVER (PARTITION BY ABS(q.Vat) ORDER BY ABS(q.Vat)) as CountByVat,COUNT(1) OVER (PARTITION BY ABS(q.Amount) ORDER BY ABS(q.Amount)) as CountByAmount,COUNT(1) OVER (PARTITION BY ABS(q.AmountIncVAT) ORDER BY ABS(q.AmountIncVAT)) as CountByAmountIncVat,q.*
FROM
(
SELECT * FROM #temptable_1
UNION ALL
SELECT * FROM #temptable_2
) AS q
) AS q1
WHERE q1.CountByPrice =1 AND q1.CountByVat =1 AND q1.CountByAmount = 1 AND q1.CountByAmountIncVat = 1
是否可以缩短这段代码的时间?
解决方法
WITH
-- your input
temptable_1(Name,Code,id_Car,PositionName,Count,id_Unit,Price,VAT,Amount,AmountIncVAT)
AS (
SELECT '1','Code 1',632324,'Position 1',1.000,1036,-2000.00,-401.00,2001.00,2401.00
UNION ALL SELECT '2','Code 2','Position 2',-10780.56,-2156.11,10.67
UNION ALL SELECT '3','Code 3','Position 3',-18780.56,-1,558.56,-12936.67
),temptable_2 ( Name,AmountIncVAT)
AS (
SELECT'4','Code 4','Position 4',2000.00,402.00,3001.00,8101.00
UNION ALL SELECT'5','Code 5','Position 5',11780.56,2156.11,10780.56,18936.67
UNION ALL SELECT'6','Code 6','Position 6',10580.56,3,-21780.56,12936.67
UNION ALL SELECT'7','Code 7','Position 7',8.000,NULL,415.34,664.54,3322.72,3987.26
)
-- your input ends here. Real query starts here,replace comma with WITH,botht AS (
SELECT * FROM temptable_1
UNION ALL
SELECT * FROM temptable_2
)
SELECT
*
FROM botht b
WHERE NOT EXISTS(
SELECT 1 FROM botht c WHERE b.price = c.price * -1
)
AND NOT EXISTS(
SELECT 1 FROM botht c WHERE b.amount = c.amount * -1
)
AND NOT EXISTS(
SELECT 1 FROM botht c WHERE b.amountIncVAT = c.amountIncVAT * -1
)
;
-- out Name | Code | id_Car | PositionName | Count | id_Unit | Price | VAT | Amount | AmountIncVAT
-- out ------+--------+--------+--------------+-------+---------+--------+--------+---------+--------------
-- out 7 | Code 7 | 632324 | Position 7 | 8.000 | | 415.34 | 664.54 | 3322.72 | 3987.26
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。