我有一个动态报告作为数据源的查询。到现在的结果是:
有 3 个与 相关的查询UNION
。第 1 行为公司积累的所有数据。第 2 行是位置的所有数据,第 3 行是详细数据。
它就像一棵树。但我的问题是,累积不正确(AnzahlMinuten)。是否有其他方法可以在动态报告中显示此数据。这 3 个查询可能非常耗时。我还使用 RANK() 函数,因为在使用许可证时我有多个条目。
如果没有其他更简单的解决方案,我在使用 union 的连接查询中的错误在哪里,以至于累积不正确?
SELECT Gesellschaftsname,Standortname,Lizenzname,Abteilungsname,Kostenstelle,
COUNT(DISTINCT username) AS AnzahlUser,
SUM(DISTINCT RuntimeMinute) AS AnzahlMinuten,
1 FROM (SELECT * FROM(SELECT DISTINCT Standortname,
DATEPART(YEAR,PK_Date) AS Jahr,
DATEPART(month,PK_Date) AS Monat,
Lizenzname,COUNT(DISTINCT username) AS AnzUser,
SUM(DISTINCT DATEDIFF(minute,starttime ,pk_date)) AS RuntimeMinute,
starttime,
username,
pk_date,
Abteilungsname,
Gesellschaftsname,
Kostenstelle,
RANK() Over (PARTITION BY starttime ORDER BY pk_date DESC) As Rank
FROM BenutzerLizenz,Benutzer,Abteilung,Lizenz,Standort,Gesellschaft,Kostenstelle
WHERE BenutzerLizenz.PK_ID_user=Benutzer.PK_ID_user AND BenutzerLizenz.PK_ID_lic=Lizenz.PK_ID_lic AND PK_ID_standort=FK_ID_standort AND PK_ID_Abteilung = FK_ID_Abteilung AND PK_ID_Gesellschaft = FK_ID_Gesellschaft AND PK_ID_Kostenstelle = FK_ID_Kostenstelle AND
DATEPART(month,PK_Date) IN ('06','07') AND
DATEPART(YEAR,PK_Date) = '2013' AND
Lizenzname IN ('DESIGNER','iman_nth') AND
Standortname IN ('Unterlüß','Neuenburg')
GROUP BY Standortname, Lizenzname, starttime, pk_date, username ,Abteilungsname, Kostenstelle, Gesellschaftsname) tmp
WHERE Rank = 1)tmp2 GROUP BY Standortname,Lizenzname,Abteilungsname, Kostenstelle, Gesellschaftsname
UNION
SELECT Gesellschaftsname,'','','','',
COUNT(DISTINCT username) AS AnzahlUser,
SUM(DISTINCT RuntimeMinute) AS AnzahlMinuten,2
FROM (SELECT * FROM(SELECT DISTINCT Gesellschaftsname,
DATEPART(YEAR,PK_Date) AS Jahr,
DATEPART(month,PK_Date) AS Monat,
COUNT(DISTINCT username) AS AnzUser,
SUM(DISTINCT DATEDIFF(minute,starttime ,pk_date)) AS RuntimeMinute,
starttime,
username,
pk_date,
RANK() Over (PARTITION BY starttime ORDER BY pk_date DESC) As Rank
FROM BenutzerLizenz,Benutzer,Lizenz,Standort,Gesellschaft
WHERE BenutzerLizenz.PK_ID_user=Benutzer.PK_ID_user AND BenutzerLizenz.PK_ID_lic=Lizenz.PK_ID_lic AND PK_ID_Gesellschaft = FK_ID_Gesellschaft AND
DATEPART(month,PK_Date) IN ('06','07') AND
DATEPART(YEAR,PK_Date) = '2013' AND
Lizenzname IN ('DESIGNER','iman_nth') AND
Standortname IN ('Unterlüß','Neuenburg')
GROUP BY Gesellschaftsname,starttime, pk_date, username) tmp
WHERE Rank = 1)tmp2 GROUP BY Gesellschaftsname
UNION
SELECT '',Standortname,'','','',
COUNT(DISTINCT username) AS AnzahlUser,
SUM(DISTINCT RuntimeMinute) AS AnzahlMinuten,3
FROM (SELECT * FROM(SELECT DISTINCT Standortname,
DATEPART(YEAR,PK_Date) AS Jahr,
DATEPART(month,PK_Date) AS Monat,
COUNT(DISTINCT username) AS AnzUser,
SUM(DISTINCT DATEDIFF(minute,starttime ,pk_date)) AS RuntimeMinute,
starttime,
username,
pk_date,
RANK() Over (PARTITION BY starttime ORDER BY pk_date DESC) As Rank
FROM BenutzerLizenz,Benutzer,Abteilung,Lizenz,Standort
WHERE BenutzerLizenz.PK_ID_user=Benutzer.PK_ID_user AND BenutzerLizenz.PK_ID_lic=Lizenz.PK_ID_lic AND PK_ID_standort=FK_ID_standort AND PK_ID_Abteilung = FK_ID_Abteilung AND
DATEPART(month,PK_Date) IN ('06','07') AND
DATEPART(YEAR,PK_Date) = '2013' AND
Lizenzname IN ('DESIGNER','iman_nth') AND
Standortname IN ('Unterlüß','Neuenburg')
GROUP BY Standortname, starttime, pk_date, username) tmp
WHERE Rank = 1)tmp2 GROUP BY Standortname
ORDER BY 2