好吧,我坚持这个:
Code_User Code_Concept Concept Quantity Amount Total_Amount 12 1 1/2 DIETA 2.000 24.000 24.000 12 10 APARCAMIENTO 2.000 0 0 12 8 COMUNICACIONES 2.000 0 0 12 3 DIETA COMPLETA 2.000 160.000 160.000 12 2 KILOMETRAJE 6.000 160 480 12 17 LOCOMOC. SEDE 2.000 0 0 12 9 MATERIAL DE OFICINA 2.000 0 0 12 7 PEAJES 2.000 0 0 12 18 VARIOS SUPL. DIVERSOS 2.000 0 0 12 NULL NULL 22.000 184.160 184.480 1505 1 1/2 DIETA 10.000 120.000 120.000 1505 10 APARCAMIENTO 22.000 55.380 55.380 1505 2 KILOMETRAJE 12.478.000 3.360 998.240 1505 17 LOCOMOC. SEDE 0 10.000 0 1505 7 PEAJES 26.000 242.300 242.300 1505 18 VARIOS SUPL. DIVERSOS 2.000 47.900 6.600 1505 NULL NULL 12.538.000 478.940 1.422.520 2816 10 APARCAMIENTO 1.000 1.000 1.000 2816 3 DIETA COMPLETA 2.000 160.000 160.000 2816 2 KILOMETRAJE 893.000 560 71.440 2816 17 LOCOMOC. SEDE 3.000 33.000 33.000 2816 18 VARIOS SUPL. DIVERSOS 3.000 4.680 4.680 2816 NULL NULL 902.000 199.240 270.120
我必须为用户1505显示概念:3,8和9(缺少概念),数字为0.用户2816错过概念1,9,8 .. 如何通过Code_User包含所有缺少的概念? 我使用此查询得到了这个结果:
SELECT --eh.Ex_Guid
eh.Code_User
,ISNULL(el.Code_Concept,'9999999999') AS Code_Concept
,CASE WHEN el.Code_Concept IS NULL THEN 'ZZZZZZZZZZ' ELSE MAX(ec.Description) END AS Concept
,SUM(el.Quantity) AS Quantity
,SUM(el.Amount) AS Amount
,SUM(el.Total_Amount) AS Total_Amount
FROM expenses_head AS eh
INNER JOIN expenses_lines AS el
ON eh.Ex_Guid = el.Ex_Guid
INNER JOIN
(
-- jerarquia de usuarios y tipo
SELECT DISTINCT CONVERT(VARCHAR,vhuos.Code_Seller) AS Code_User
,pu.Code_Type
FROM view_hier_user_organ_seller AS vhuos
INNER JOIN prj_users AS pu
ON pu.Code_User = CONVERT(VARCHAR,vhuos.Code_Seller)
WHERE 1=1
AND vhuos.Code_Sales_Org='1010' --FILTRO OBLIGATORIO
AND vhuos.Code_User='10'--USUARIO EN SESION
-- AND Code_User IN (...) -- usuarios multiselect
)AS u
ON eh.Code_User = u.Code_User
RIGHT JOIN
(
-- conceptos por tipo de usuario
SELECT DISTINCT ec.Code_Concept,ec.Description,eo.Code_Type_User
FROM expenses_concepts AS ec
INNER JOIN expenses_organization AS eo
ON ec.Code_Concept = eo.Code_Concept
--ORDER BY Code_Type_User,Description
)AS ec
ON el.Code_Concept = ec.Code_Concept
AND ec.Code_Type_User = u.Code_Type
WHERE 1=1
AND eh.Delete_Date IS NULL
AND el.Delete_Date IS NULL
AND el.Date_Expenses>'20150215' --filtro fecha inicio
AND el.Date_Expenses<'20150510' --filtro fecha fin
GROUP BY eh.Code_User,ROLLUP(el.Code_Concept)
ORDER BY eh.Code_User,Concept,el.Code_Concept
答案 0 :(得分:0)
如果以下查询有效,请告诉我:
select abc.code_user,
abc.code_concept,
sam.concept,
sam.Quantity,
sam.amount,
sam.total_amount
from
(
select distinct code_user,t.code_concept
from sam as a
cross apply(select distinct code_concept from sam) as t(code_concept)
) as abc
left join sam
on abc.code_user = sam.code_user and isnull(abc.code_concept,'') = isnull(sam.code_concept,'')
上述查询将包含所有缺少的code_concept。您可以将此输出存储在临时表中。截断表并将临时表中的记录插入表中。
答案 1 :(得分:0)
首先,谢谢你的答复。我终于使用混合php和我的第一个查询解决了这个问题。 在此解决方案之前,我尝试过交叉连接,但它没有起作用,导致结果数量。小记录工作得很好。 截断不是一种选择。如果多个用户想要使用相同的报表,则并发将保留最新用户的结果。