Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 编写此脚本以使其不返回聚合错误的更好方法_Sql_Sql Server_Sql Server 2008_Tsql - Fatal编程技术网

Sql 编写此脚本以使其不返回聚合错误的更好方法

Sql 编写此脚本以使其不返回聚合错误的更好方法,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,由于存在t3.Date,此脚本给我一个聚合错误。 请问还有其他的书写方式吗 SELECT t1.ID, t1.Date, t2.Type, t1.Username FROM t1 INNER JOIN t2 ON t1.CareID = t2.CareID AND t1.Date = t2.Date AND t1.Username = t2.Username LEFT JOIN t3 ON t1.CareID = t3.CareID AND t1.Username = t3.Username

由于存在t3.Date,此脚本给我一个聚合错误。 请问还有其他的书写方式吗

SELECT t1.ID, t1.Date, t2.Type, t1.Username
FROM t1
INNER JOIN t2
ON t1.CareID = t2.CareID AND 
t1.Date = t2.Date AND
t1.Username = t2.Username
LEFT JOIN t3
ON t1.CareID = t3.CareID AND t1.Username = t3.Username
GROUP BY t1.ID, t1.Date, t2.Type, t1.Username
HAVING COUNT(*) = 1 AND t1.Date <= t3.Date
选择t1.ID、t1.Date、t2.Type、t1.Username
从t1开始
内连接t2
在t1.CareID=t2.CareID和
t1.日期=t2.日期和
t1.Username=t2.Username
左连接t3
在t1.CareID=t3.CareID和t1.Username=t3.Username上
按t1.ID、t1.Date、t2.Type、t1.Username分组

让COUNT(*)=1和t1.Date试试下面的脚本

SELECT t1.ID, t1.Date, t2.Type, t1.Username
FROM t1
INNER JOIN t2
ON t1.CareID = t2.CareID AND 
t1.Date = t2.Date AND
t1.Username = t2.Username
LEFT JOIN t3
ON t1.CareID = t3.CareID AND t1.Username = t3.Username AND t1.Date <= t3.Date
GROUP BY t1.ID, t1.Date, t2.Type, t1.Username
HAVING COUNT(t1.ID) = 1  
选择t1.ID、t1.Date、t2.Type、t1.Username
从t1开始
内连接t2
在t1.CareID=t2.CareID和
t1.日期=t2.日期和
t1.Username=t2.Username
左连接t3

在t1.CareID=t3.CareID和t1.Username=t3.Username和t1.Date上,尝试使用以下脚本

SELECT t1.ID, t1.Date, t2.Type, t1.Username
FROM t1
INNER JOIN t2
ON t1.CareID = t2.CareID AND 
t1.Date = t2.Date AND
t1.Username = t2.Username
LEFT JOIN t3
ON t1.CareID = t3.CareID AND t1.Username = t3.Username AND t1.Date <= t3.Date
GROUP BY t1.ID, t1.Date, t2.Type, t1.Username
HAVING COUNT(t1.ID) = 1  
选择t1.ID、t1.Date、t2.Type、t1.Username
从t1开始
内连接t2
在t1.CareID=t2.CareID和
t1.日期=t2.日期和
t1.Username=t2.Username
左连接t3
在t1.CareID=t3.CareID和t1.Username=t3.Username和t1.Date上删除:

 AND t1.Date <= t3.Date
删除:

 AND t1.Date <= t3.Date

您的语句缺少别名,因此请编写类似于
Count(t1.Id)
的语句,并删除
和t1.Date您的语句缺少别名,因此编写类似于
Count(t1.Id)
的语句,并删除
和t1.Date Having Count(*)的语句。请指定表别名,并且您尚未将group by应用于t3,所以不能用在拥有close@SandipPatel很抱歉,你能不能扩展一下,或者粘贴一个例子?谢谢,这取决于“这”是什么。可能是t1.date