原谅基本问题,但我对SQL比较新。我有几个问题,我想使用UNION函数。我认为我在订购我必须得到的相应结果的项目时遇到问题,他们有一些具有相同名称的列。但我仍然有问题。这是我想加入的。
SELECT
CAST([DateTime] as DATE) as "Date"
, REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(SG_NAME.EnterpriseName,'UCM.',''),'_SG',''),'_en_Field',' English Field'),'_en_Tier2',' English'),'_es_Tier2',' Spanish')as "Group"
, sum(SG.CallsOffered) as "Calls Offered"
, sum(CallsHandled) as "Calls Handled"
, SUM(SG.RouterCallsAbandQ) as "Abandons"
, DATENAME(weekday,[datetime])as "Day of Week"
, DATENAME(week, [datetime]) as Week
, DATENAME(year, [datetime]) as "Year"
, DATENAME (month, [datetime]) as "Month"
, sum (SG.LoggedOnTime)/60/60 as "Hours Logged On"
--, HoldTime
FROM archive.t_Skill_Group_Interval AS SG
INNER JOIN awdb.Skill_Group AS SG_NAME
ON SG.SkillTargetID=SG_NAME.SkillTargetID
WHERE
DATEDIFF(DAY,[DateTime],GETDATE())<180
AND DATEPART(WEEKDAY,[datetime])in (2,3,4,5,6)
AND SG.SkillTargetID in ( '5003','5007','5069','5062','5300')
group by
CAST([DateTime] as DATE)
, SG_NAME.EnterpriseName
,DATENAME(weekday,[datetime])
, DATENAME(week, [datetime])
, DATENAME(year, [datetime])
,DATENAME (month, [datetime])
ORDER BY SG_NAME.EnterpriseName,"Date"
----UNION (I'd like to Union these two queries)
SELECT
CAST([DateTime] as DATE) as "Date"
, REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE
(CT_NAME.EnterpriseName,'DL_',''),'_Main_CT',' Main'),'87180_',''),'_CT',''),'_Span Main','Spanish Main'),'_Switchboard_Transfer',' Switch Transfer'),'DPS_','') as "Group"
, sum(CT.CallsOffered) as "Calls Offered"
, SUM(CT.CallsHandled) as "Calls Handled"
, SUM (CT.AbandInterval1+CT.AbandInterval2+CT.AbandInterval3+CT.AbandInterval4+CT.AbandInterval5+CT.AbandInterval6+CT.AbandInterval7+CT.AbandInterval8+CT.AbandInterval9+CT.AbandInterval10) as "Abandons"
, DATENAME(weekday,[datetime])as "Day of Week"
, DATENAME(week, [datetime]) as Week
, DATENAME(year, [datetime]) as "Year"
, DATENAME (month, [datetime]) as "Month"
, SUM(CT.TalkTime) as "Hours Logged On"
--, CallsAnswered
--, a.LoggedOnTime
--, HoldTime
--, b.SkillTargetID
--, a.SkillTargetID
FROM archive.t_Call_Type_Interval AS CT
INNER JOIN archive.t_Call_Type AS CT_NAME
ON CT.CallTypeID=CT_NAME.CallTypeID
WHERE
DATEDIFF(DAY,[DateTime],GETDATE())<180
AND DATEPART(WEEKDAY,[datetime])in (2,3,4,5,6)
--DATEDIFF(DAY,[DateTime],GETDATE())<180
AND CT.CallTypeID in ('5122','5115','5285','5289','5290','5291','5288','5214')
group by
CAST([DateTime] as DATE)
, CT_NAME.EnterpriseName
,DATENAME(weekday,[datetime])
, DATENAME(week, [datetime])
, DATENAME(year, [datetime])
,DATENAME (month, [datetime])
ORDER BY CT_NAME.EnterpriseName,"Date"
答案 0 :(得分:1)
如果您想使用UNION
运算符,则最后必须ORDER BY
(而不是在每个UNION
分支中执行此操作)。此外,如果您知道UNION
分支产生DISTINCT
值,请使用UNION ALL
,因为它不会尝试删除重复项,并且更有效,尤其是当存在大量重复行时。