Sql 能让工会正常运作吗
我把代码改成这个,现在我得到了我想要的数字唯一的问题是它没有排序我需要它按Corpid排序,然后按日期01-02-03等等我不太确定Sql 能让工会正常运作吗,sql,sql-server,Sql,Sql Server,我把代码改成这个,现在我得到了我想要的数字唯一的问题是它没有排序我需要它按Corpid排序,然后按日期01-02-03等等我不太确定 Select CorpID, Convert(VarChar(2),Month(E.BeginDate)) + '/' + Convert(VarChar(4),Year(E.BeginDate)), Count(Year(e.BeginDate)) As 'total Screen' --Count(Month(E.CurrentB
Select CorpID,
Convert(VarChar(2),Month(E.BeginDate)) + '/' + Convert(VarChar(4),Year(E.BeginDate)),
Count(Year(e.BeginDate)) As 'total Screen'
--Count(Month(E.CurrentBeginDate))
From dbo.NonCalls E
Where E.BeginDate between {d'2013-01-01'} and {d'2013-12-31'}
Group By CorpID, Year(E.BeginDate),Month(E.BeginDate)
Union ALL
Select CorpID,
Convert(VarChar(2),Month(E.CurrentBeginDate)) + '/' + Convert(VarChar(4),Year(E.CurrentBeginDate)),
Count(Year(e.CurrentBeginDate)) As 'total Screen'
--Count(Month(E.CurrentBeginDate))
From dbo.Employee E
Where E.CurrentBeginDate between {d'2013-01-01'} and {d'2013-12-31'}
Group By CorpID, Year(E.CurrentBeginDate),Month(E.CurrentBeginDate)
--Order By CorpID, Year(E.CurrentBeginDate), Month(E.CurrentBeginDate)
在联合体的第二部分中有2个CurrentBeginDate,导致该部分返回5列,但第一部分仅返回4列
how to get that accomplish any help would be greatly apreciated.
选择
瘦削的,
CurrentBeginDate作为您“关于对现在已正确联合的数据进行排序的新问题”的答案:
您需要将联合结果集视为派生表,并使用order.by从中进行选择
SELECT
CorpID ,
CurrentBeginDate <--HERE,
CONVERT(VARCHAR(2), MONTH(E.CurrentBeginDate)) + '/'
+ CONVERT(VARCHAR(4), YEAR(E.CurrentBeginDate)) AS CurrentBeginDate <--HERE,
COUNT(YEAR(e.CurrentBeginDate)) AS 'total Screen' ,
'' AS d1
选择*
FROM(错误消息是?您使用的是什么数据库?Msg 205,级别16,状态1,第1行使用UNION、INTERSECT或EXCEPT运算符组合的所有查询在其目标列表中必须具有相同数量的表达式。数据库是SQL@user3571281-“SQL"是不是db,它是一种多个dbs实现的标准化语言。就像走进杂货店看冰淇淋一样——Dreyer's有香草味,Costco、Haagen Daz、Ben&Jerry's等也有香草味。哦,它们都有细微的不同。不要包含注释掉的代码——它不应该运行,而且可能会把事情搞混,当然也一样不是服务器的好处。(从查询中删除它是否也会导致它运行?)。您的查询似乎没有以引发给定错误的方式无效。
SELECT *
FROM (<your unioned query)
ORDER BY CorpID
,Year(CurrentBeginDate)
,Month(CurrentBeginDate)