Привет я реализую procedure, чтобы показывать связь продаж из-за отметки, у меня есть следующее:
DECLARE @fechaInicial DATETIME = '01/01/2019'
DECLARE @fechaFinal DATETIME = '31/01/2019'
DECLARE @id_municipio DECIMAL(18,0) = NULL
DECLARE @id_marca DECIMAL(18,0) = NULL
BEGIN
SET @fechainicial = @fechainicial+' 00:00:00'
SET @fechafinal = @fechafinal+' 23:59:59'
SET NOCOUNT ON;
CREATE TABLE #ConjuntosDatos(
valor DECIMAL(18,0),
id_marca DECIMAL(18,0),
id_municipio DECIMAL(18,0)
)
insert into #ConjuntosDatos
SELECT DISTINCT F.valor,a.id_marca, F.id_municipio
FROM factura F
INNER JOIN detalle_factura df ON df.id_factura = F.id_factura
INNER JOIN articulo a ON a.articuloid = df.articuloid
WHERE F.fecha >= @fechaInicial and F.fecha <= @fechaFinal
AND ((@id_municipio IS NULL) OR (f.id_municipio = @id_municipio))
AND ((@id_marca IS NULL) OR (a.id_marca = @id_marca))
END
BEGIN
SELECT m.nombre as marca,
(select isnull(SUM(valor),0) from #ConjuntosDatos where id_marca = m.id_marca and id_municipio =1048) as Arauca,
(select isnull(SUM(valor),0) from #ConjuntosDatos where id_marca = m.id_marca and id_municipio =1049) as Arauquita,
(select isnull(SUM(valor),0) from #ConjuntosDatos where id_marca = m.id_marca and id_municipio =1051) as Fortul,
(select isnull(SUM(valor),0) from #ConjuntosDatos where id_marca = m.id_marca and id_municipio =1052) as PtoRondon,
(select isnull(SUM(valor),0) from #ConjuntosDatos where id_marca = m.id_marca and id_municipio =1053) as Saravena,
(select isnull(SUM(valor),0) from #ConjuntosDatos where id_marca = m.id_marca and id_municipio =1054) as Tame,
(select isnull(SUM(valor),0) from #ConjuntosDatos where id_marca = m.id_marca and id_municipio =1125) as PtoJordan,
(select isnull(SUM(valor),0) from #ConjuntosDatos where id_marca = m.id_marca) Total
FROM marca m
DROP TABLE #ConjuntosDatos
END
от моего предыдущего query я получаю следующие реестры
то, что я стремлюсь к тому, чтобы реализовать, состоит в том, чтобы линии которых полное поле было равно нулю, не появились. Который я могу делать, чтобы этого добиваться
Пример: Статья: 6, 7, 17, 18, 19, 20 не явилось бы, так как общее количество cero
BГЎsicamente у тебя есть две формы:
SELECT *
FROM (SELECT m.nombre as marca,
(select isnull(SUM(valor),0) from #ConjuntosDatos where id_marca = m.id_marca and id_municipio =1048) as Arauca,
(select isnull(SUM(valor),0) from #ConjuntosDatos where id_marca = m.id_marca and id_municipio =1049) as Arauquita,
(select isnull(SUM(valor),0) from #ConjuntosDatos where id_marca = m.id_marca and id_municipio =1051) as Fortul,
(select isnull(SUM(valor),0) from #ConjuntosDatos where id_marca = m.id_marca and id_municipio =1052) as PtoRondon,
(select isnull(SUM(valor),0) from #ConjuntosDatos where id_marca = m.id_marca and id_municipio =1053) as Saravena,
(select isnull(SUM(valor),0) from #ConjuntosDatos where id_marca = m.id_marca and id_municipio =1054) as Tame,
(select isnull(SUM(valor),0) from #ConjuntosDatos where id_marca = m.id_marca and id_municipio =1125) as PtoJordan,
(select isnull(SUM(valor),0) from #ConjuntosDatos where id_marca = m.id_marca) Total
FROM marca m
) T
WHERE T.Total <> 0
Твоя конечная консультация ты можешь писать внутри другого FROM
а следовательно ты можешь соглашаться на колонны, вычисленные как Total
из прозрачной формы.
GROUP BY
SELECT m.nombre as marca,
SUM(ISNULL(CASE WHEN id_municipio = 1048 THEN d.valor ELSE 0 END, 0)) AS Arauca,
SUM(ISNULL(CASE WHEN id_municipio = 1049 THEN d.valor ELSE 0 END, 0)) AS Arauquita,
SUM(ISNULL(CASE WHEN id_municipio = 1051 THEN d.valor ELSE 0 END, 0)) AS Fortul,
SUM(ISNULL(CASE WHEN id_municipio = 1052 THEN d.valor ELSE 0 END, 0)) AS PtoRondon,
SUM(ISNULL(CASE WHEN id_municipio = 1053 THEN d.valor ELSE 0 END, 0)) AS Saravena,
SUM(ISNULL(CASE WHEN id_municipio = 1054 THEN d.valor ELSE 0 END, 0)) AS Tame,
SUM(ISNULL(CASE WHEN id_municipio = 1125 THEN d.valor ELSE 0 END, 0)) AS PtoJordan,
SUM(ISNULL(d.valor,0)) AS Total
FROM marca m
LEFT JOIN #ConjuntosDatos d
on d.id_marca = m.id_marca
GROUP BY m.nombre
HAVING SUM(ISNULL(d.valor,0)) <> 0
год Будь soluciГіn он много mГЎs Гіptima, из-за которого мы прекращаем использовать субконсультации на уровне колонн, что-то, что обычно не совсем Гіptimo. Различие с твоей консультацией состоит в том, что мы используем условную сумму согласно municipio
, и конечный фильтр нужен осуществлять это посредством HAVING
, так как общее количество - один SUM
из GROUP BY
.