我正在创建以下SQL表
$options = array(
'uri' => 'http://aahshhs.com', //wath do this line?????
'location' => 'http://localhost/exer/Server.php',);
获取包含行数和金额的两列表格。
现在我想要的只是最后一行正下方一行中SELECT Nr, nu_betrag_wert AS Amount
FROM
(SELECT DISTINCT
processid, nu_betrag_wert, Nr
FROM NU_WERTE
WHERE Nr <> '(Nr)' AND nu_betrag_wert is NOT NULL) AS nu_betrag
列的总和。
我尝试了类似这样的事情,但没有成功:
Amount
有人可以帮我吗?
答案 0 :(得分:0)
我已经成功地在两个SELECT中使用UNION来生成我认为你要求的东西 - 它显示了值列表和底部单独行中的最终求和。
也许是这样的:
CREATE TABLE #nu_werte ( processid INT IDENTITY, nu_betrag_wert int, nr VARCHAR(10) )
INSERT INTO #nu_werte ( nr, nu_betrag_wert )
SELECT 'Gut', 1 UNION
SELECT '(Nr)', 3 UNION
SELECT 'Gut', 5 UNION
SELECT 'Tag', 6 UNION
SELECT 'Tag', 8
WITH nu_betrag ( processid, nu_betrag_wert, nr )
AS
(
SELECT
processid,
nu_betrag_wert,
nr
FROM
#nu_werte
WHERE
nr != '(Nr)' AND
nu_betrag_wert IS NOT NULL
)
SELECT nr, nu_betrag_wert AS amount
FROM
nu_betrag
UNION
SELECT
NULL, SUM(nu_betrag_wert) AS amount
FROM
nu_betrag
答案 1 :(得分:0)
刚检查过:
;WITH NU_WERTE AS (
SELECT DISTINCT *
FROM (VALUES
(1,10,'(N0)'),(2,20,'(N0)'),(3,30,'(N0)'),(4,40,'(N0)'),(5,60,'(N0)'),
(6,11,'(N1)'),(7,22,'(N1)'),(8,33,'(N1)'),(9,44,'(N1)'),(10,NULL,'(N1)'),
(11,NULL,'(N1)'),(12,20,'(Nr)'),(13,10,'(Nr)')
) as t (processid, nu_betrag_wert, Nr)
)
SELECT Nr, nu_betrag_wert AS Amount
FROM
(SELECT DISTINCT
processid, nu_betrag_wert, Nr
FROM NU_WERTE
WHERE Nr <> '(Nr)' AND nu_betrag_wert is NOT NULL) AS nu_betrag
UNION ALL
SELECT
Nr, SUM(nu_betrag_wert) AS Zahlbetrag
FROM
(SELECT DISTINCT
processid, nu_betrag_wert, Nr
FROM NU_WERTE
WHERE Nr <> '(Nr)' AND nu_betrag_wert is NOT NULL) AS nu_betrag
GROUP BY
Nr WITH ROLLUP
输出:
Nr Amount
(N0) 10
(N0) 20
(N0) 30
(N0) 40
(N0) 60
(N1) 11
(N1) 22
(N1) 33
(N1) 44
(N0) 160
(N1) 110
NULL 270