已更新 我有3个这样的表:
[dbo].[CHEQUES]:
|Cla_Pdv | Che | Fecha | Tur |
---------------------------------------------------
| FUEM | 4423 |2016-01-01 | 2 |
---------------------------------------------------
[dbo].[CAPMO]:
|Clave_Pdv | Che | Com | Pla | Descripcion | Precio | Can |
--------------------------------------------------------------------------------------------------------
| FUEM | 4423 | 1 | 184 | POZ | 85 | 1 |
------------------------------------------------------------------------------------------------------
| FUEM | 4423 | 2 | 184 | POZ | 85 | 1 |
------------------------------------------------------------------------------------------------------
| FUEM | 4423 | 3 | 184 | POZ | 84 | 1 |
------------------------------------------------------------------------------------------------------
[dbo].[TURNO]:
|Clave_Pdv | Tur | Descripcion |
-----------------------------------------------
| FUEM | 1 | DESAYUNOS |
-----------------------------------------------
| FUEM | 2 | COMIDAS |
-----------------------------------------------
| FUEM | 3 | CENAS |
-----------------------------------------------
我需要[dbo]。[Capmo]表的每个值,但我想增加第一行的数量,因为两行在'Pla'gettting列上具有相同的值,并输出如下:
| Cheque | Turno | Clave_Pdv | Pla | Descripcion | Precio | Cantidad |
| 4423 | COMIDAS | FUEM | 184 | POZ | 85 | 2 |
| 4423 | COMIDAS | FUEM | 184 | POZ | 84 | 1 |
答案 0 :(得分:1)
您似乎不需要Cheques
和Capmo
之间的联接,因为您的输出可以从Capmo
获得。但是,您在评论中提到了其他表格,可能希望扩展您的问题以包含完整的示例。
SELECT * INTO Cheques FROM (
VALUES('FUEM', 4423, '2016-01-01', 2)) A (Cla_Pdv, Che, Fecha, Tur)
SELECT * INTO Capmo FROM (
VALUES
('FUEM', 4423, 1, 184, 'POZ', 85, 1)
,('FUEM', 4423, 2, 184, 'POZ', 85, 1)
,('FUEM', 4423, 3, 184, 'POZ', 84, 1)
) A (Clave_pdv, Che, Com, Pla, Descripcion, Precio, Can)
SELECT * INTO Turno FROM (
VALUES
('FUEM', 1,'DESAYUNOS')
,('FUEM', 2,'COMIDAS')
,('FUEM', 3,'CENAS')
) A (Clave_pdv, Tur,Descripcion)
SELECT DISTINCT
Cheques.Che Cheque
,Turno.Descripcion Turno
,Capmo.Clave_Pdv
,Capmo.Pla
,Capmo.Descripcion
,Capmo.Precio
,SUM(Capmo.Can) OVER ( PARTITION BY Capmo.Precio ) Cantidad
FROM
Capmo
JOIN Cheques ON Capmo.Clave_pdv = Cheques.Cla_pdv
JOIN Turno ON Capmo.Clave_pdv = Turno.Clave_pdv AND Cheques.Tur = Turno.Tur
ORDER BY Precio DESC;
Cheque Turno Clave_Pdv Pla Descripcion Precio Cantidad
----------- --------- --------- ----------- ----------- ----------- -----------
4423 COMIDAS FUEM 184 POZ 85 2
4423 COMIDAS FUEM 184 POZ 84 1
(2 row(s) affected)