SQL视图聚合函数不按行拉取
我正在使用SQLServerManagementStudio,并试图创建一个带有聚合函数的视图,然后我将查询该视图,以便将所需的所有数据点收集在一起 我正在查看包含以下内容的表格: 联系人表: contactid 123 lastname Overflow firstname Stack 我收到这个错误消息:Msg 512,16级,状态1,第2行 子查询返回了多个值。当子查询在=、!=、=或者当子查询用作表达式时 我试过各种方法来获得最长的约会时间。任何帮助都将不胜感激。SQL视图聚合函数不按行拉取,sql,sql-server,view,Sql,Sql Server,View,我正在使用SQLServerManagementStudio,并试图创建一个带有聚合函数的视图,然后我将查询该视图,以便将所需的所有数据点收集在一起 我正在查看包含以下内容的表格: 联系人表: contactid 123 lastname Overflow firstname Stack 我收到这个错误消息:Msg 512,16级,状态1,第2行 子查询返回了多个值。当子查询在=、!=、=或者当子查询用作表达式时 我试过各种方法来获得最长的约会时间。任何帮助都将不胜感激。 谢谢 在子查询中,您
谢谢 在子查询中,您将获得contactType为29的所有表调用行。我想那不是你想要的 这应该做到:
Create VIEW LastIntakeView AS
SELECT c.contactid, c.lastname, c.firstname, MAX(ca.starttime) X
FROM sfcd.dbo.Contacts c left join sfcd.dbo.Calls ca on c.contactid = ca.contactid
where ca.ContactTypeID=29
group by c.contactid, c.lastname, c.firstname
子查询向ca.StartTime返回的行数超过1行,这对于运算符=,!=,=,是不可接受的,它可以被etc中的操作员接受。
但在您的情况下,请尝试加入子查询返回
Create VIEW LastIntakeView
AS
SELECT c.contactid,
c.lastname,
c.firstname,
cl.Maxstarttime
FROM sfcd.dbo.Contacts c
left join (
select Contactid, MAX(starttime) as MaxStartTime
from sfcd.dbo.Calls
where ContactTypeID=29
group by contactid
) cl
on c.contactid = cl.contactid
. 另外,请指定您正在使用的SQL Server版本。我在这方面遇到了此错误,但上面的另一个答案有效。不幸的是,我不知道如何调试。谢谢你的帮助。Msg 4511,级别16,状态1,过程LastIntakeView1,第2行创建视图或函数失败,因为没有为列4指定列名。
Create VIEW LastIntakeView AS
SELECT c.contactid, c.lastname, c.firstname, MAX(ca.starttime) X
FROM sfcd.dbo.Contacts c left join sfcd.dbo.Calls ca on c.contactid = ca.contactid
where ca.ContactTypeID=29
group by c.contactid, c.lastname, c.firstname
Create VIEW LastIntakeView
AS
SELECT c.contactid,
c.lastname,
c.firstname,
cl.Maxstarttime
FROM sfcd.dbo.Contacts c
left join (
select Contactid, MAX(starttime) as MaxStartTime
from sfcd.dbo.Calls
where ContactTypeID=29
group by contactid
) cl
on c.contactid = cl.contactid