条件为 SQL 服务器的子查询
SUBQUERIES with a condition SQL Server
我想在SQL服务器中寻求一点帮助。
我需要对客户购买的东西进行总计 $。
我的子查询只对整个 table 求和,但我需要类似
的东西
Numero D TotalItem TotalBOUGHT
-----------------------------------------------
111 800 1200
111 200 1200
111 100 1200
111 100 1200
455 200 300
455 100 300
这是我的代码
SELECT
NumeroD, Descrip AS ClientName, CodClie AS ID,
Descrip1 AS Description, TotalItem, CodUbic,
Día_Transaccion AS Day, Mes AS Month, ANO AS YEAR,
(SELECT SUM(TotalItem) FROM FORMULAFINAL) AS TotalAmount
FROM
FORMULAFINAL
WHERE
Descrip LIKE 'Hector%'
GROUP BY
TotalItem, NumeroD, Descrip, CodClie, Descrip1,
TotalItem, CodUbic, Día_Transaccion, Mes, Ano
ORDER BY
Ano DESC, NumeroD, Descrip, Descrip1, CodClie, TotalItem,
CodUbic, Día_Transaccion, Mes
enter image description here
也许使用像下面这样的窗口函数
select
NumeroD, Descrip AS ClientName, CodClie AS ID, Descrip1 AS Description, TotalItem, CodUbic, Día_Transaccion AS Day, Mes AS Month, ANO AS YEAR,
SUM(TotalItem) OVER (partition by NumeroD, DAY, MONTH, YEAR ) AS TotalAmount
FROM FORMULAFINAL
WHERE Descrip LIKE 'Hector%'
ORDER BY Ano DESC, NumeroD, Descrip, Descrip1, CodClie, TotalItem, CodUbic, Día_Transaccion, Mes
我想在SQL服务器中寻求一点帮助。
我需要对客户购买的东西进行总计 $。
我的子查询只对整个 table 求和,但我需要类似
的东西Numero D TotalItem TotalBOUGHT
-----------------------------------------------
111 800 1200
111 200 1200
111 100 1200
111 100 1200
455 200 300
455 100 300
这是我的代码
SELECT
NumeroD, Descrip AS ClientName, CodClie AS ID,
Descrip1 AS Description, TotalItem, CodUbic,
Día_Transaccion AS Day, Mes AS Month, ANO AS YEAR,
(SELECT SUM(TotalItem) FROM FORMULAFINAL) AS TotalAmount
FROM
FORMULAFINAL
WHERE
Descrip LIKE 'Hector%'
GROUP BY
TotalItem, NumeroD, Descrip, CodClie, Descrip1,
TotalItem, CodUbic, Día_Transaccion, Mes, Ano
ORDER BY
Ano DESC, NumeroD, Descrip, Descrip1, CodClie, TotalItem,
CodUbic, Día_Transaccion, Mes
enter image description here
也许使用像下面这样的窗口函数
select
NumeroD, Descrip AS ClientName, CodClie AS ID, Descrip1 AS Description, TotalItem, CodUbic, Día_Transaccion AS Day, Mes AS Month, ANO AS YEAR,
SUM(TotalItem) OVER (partition by NumeroD, DAY, MONTH, YEAR ) AS TotalAmount
FROM FORMULAFINAL
WHERE Descrip LIKE 'Hector%'
ORDER BY Ano DESC, NumeroD, Descrip, Descrip1, CodClie, TotalItem, CodUbic, Día_Transaccion, Mes