我想要显示的SQL结果,每个人都有不同的活动,每个活动的价格加上费用。我和他们一起分组(见附图)。突出显示的结果为黄色,应该是我的最终报告。请帮忙。 赋予的
SQL查询:
SELECT DISTINCT Name, Surname, TypeParticipant, i.IDBill, ip.Idparticipant, cast((f.couttotal / 100.0) AS DECIMAL(20, 2)) AS price, left(f.texte, 20) AS ActivityName, a.Activityfee, convert(VARCHAR, g.datedebut, 111) DateDébut, convert(VARCHAR, g.datefin, 111) DateFIn
FROM Fin.FactureItem i
LEFT JOIN ins.Inscription ip
ON ip.Idinscription = i.IDinscription
LEFT JOIN Fin.FactureItem f
ON f.IDfacture = i.IDfacture
INNER JOIN Part.participant p
ON p.idparticipant = ip.idparticipant
INNER JOIN Acts.Groupe g
ON g.idgroupe = ip.idgroupe
INNER JOIN Acts.Activite a
ON a.idactivite = g.idactivite
INNER JOIN Part.ParticipantTypeParticipant t
ON p.idparticipant = t.idparticipant – Blessed 11 secs ago
INNER JOIN Part.TypeParticipant ct
ON ct.idtypeparticipant = t.idtypeparticipant
WHERE i.IDfacture IS NOT NULL
AND i.idinscription IS NOT NULL
AND p.idparticipant IN (
45103
,12354
)
答案 0 :(得分:0)
所有其他列都需要aggregation
。这将根据您在image
中的输出而生效。但是,我认为最高价应该是230美元。无论如何,根据我们的输出,我需要修改查询。您可能需要进一步的子查询。
WITH cte
AS (
SELECT DISTINCT nom
,prenom
,TitreFRA AS TypeParticipant
,i.IDfacture AS IDBill
,ip.Idparticipant
,cast((f.couttotal / 100.0) AS DECIMAL(20, 2)) AS price
,left(f.texte, 20) AS Activityandfees
,a.nomFra Activityname
,convert(VARCHAR, g.datedebut, 111) DateDébut
,convert(VARCHAR, g.datefin, 111) DateFIn
FROM Finances.FactureItem i
LEFT JOIN Inscriptions.Inscription ip ON ip.Idinscription = i.IDinscription
LEFT JOIN Finances.FactureItem f ON f.IDfacture = i.IDfacture
INNER JOIN participants.participant p ON p.idparticipant = ip.idparticipant
INNER JOIN Activites.Groupe g ON g.idgroupe = ip.idgroupe
INNER JOIN Activites.Activite a ON a.idactivite = g.idactivite
INNER JOIN Participants.ParticipantTypeParticipant t ON p.idparticipant = t.idparticipant
INNER JOIN participants.TypeParticipant ct ON ct.idtypeparticipant = t.idtypeparticipant
WHERE i.IDfacture IS NOT NULL AND i.idinscription IS NOT NULL AND p.idparticipant IN (
4513
,12354
)
)
,CT1
AS (
SELECT nom
,prenom
,TypeParticipant
,Activityname
,DateDébut
,Activityandfees
,price
,ROW_NUMBER() OVER (
PARTITION BY ACTIVITYANDFEES
ORDER BY ACTIVITYNAME DESC
) rn
FROM cte
)
,ct2
AS (
SELECT NOM
,PRENOM
,TYPEPARTICIPANT
,ACTIVITINAME
,Price
,0 Price1
,0 price2
,0 price3
FROM ct1
WHERE Activityandfees = 'Ajout de I Activite' AND rn = 1
UNION ALL
SELECT NOM
,PRENOM
,TYPEPARTICIPANT
,ACTIVITINAME
,0 Price
,price price1
,0 price2
,0 price3
FROM ct1
WHERE Activityandfees = 'Frias daccompagname' AND rn = 1
UNION ALL
SELECT NOM
,PRENOM
,TYPEPARTICIPANT
,ACTIVITINAME
,0 Price
,0 price1
,price price2
,0 price3
FROM ct1
WHERE Activityandfees = 'Frias dinscription' AND rn = 1
UNION ALL
SELECT NOM
,PRENOM
,TYPEPARTICIPANT
,ACTIVITINAME
,0 Price
,0 price1
,0 price2
,price Price3
FROM ct1
WHERE Activityandfees = 'Subvention' AND rn = 1
)
SELECT NOM
,PRENOM
,TYPEPARTICIPANT
,ACTIVITINAME
,sum(cast(price as float)) AjoutActivityprice
,sum(cast(price1 as float)) daccompagnameActivityprice
,sum(cast(price2 as float)) dinscriptionActivityprice
,sum(cast(price3 as float)) Subventionprice
FROM ct2
GROUP BY NOM
,PRENOM
,TYPEPARTICIPANT
,ACTIVITINAME