Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.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
需要帮助在Microsoft SQL Server中分组和创建select语句吗_Sql_Sql Server - Fatal编程技术网

需要帮助在Microsoft SQL Server中分组和创建select语句吗

需要帮助在Microsoft SQL Server中分组和创建select语句吗,sql,sql-server,Sql,Sql Server,我有3个具有以下结构的表: 我想要的结果是: 我尝试了多种分组语句,但如果有人能帮我的话,我无法接近我需要的结构。谢谢这里有一个方法: select u.userid, u.name, count(distinct case when b.visibility = 1 then b.blogid end) as num_public_blogs, count(distinct case when b.visibility = 0 then b.blogid end

我有3个具有以下结构的表:

我想要的结果是:

我尝试了多种分组语句,但如果有人能帮我的话,我无法接近我需要的结构。谢谢这里有一个方法:

select u.userid, u.name,
       count(distinct case when b.visibility = 1 then b.blogid end) as num_public_blogs,
       count(distinct case when b.visibility = 0 then b.blogid end) as num_private_blogs,
       count(case when b.visibility = 1 then bp.blogid end) as num_public_posts,
       count(case when b.visibility = 0 then bp.blogid end) as num_private_posts
from users u left join
     blogs b
     on b.userid = u.userid left join
     blogposts bp
     on bp.blogid = b.blogid
group by u.userid, u.name;
这里有一种方法:

select u.userid, u.name,
       count(distinct case when b.visibility = 1 then b.blogid end) as num_public_blogs,
       count(distinct case when b.visibility = 0 then b.blogid end) as num_private_blogs,
       count(case when b.visibility = 1 then bp.blogid end) as num_public_posts,
       count(case when b.visibility = 0 then bp.blogid end) as num_private_posts
from users u left join
     blogs b
     on b.userid = u.userid left join
     blogposts bp
     on bp.blogid = b.blogid
group by u.userid, u.name;

请不要电子表格的截图。我如何在堆栈溢出中创建一个表而不用花很长时间?这就是你的回答?认真地为什么你会期望有人永远回答你的问题?只是指出,这是一个电子表格截图超级简单,我不知道如何做它的其他方式,现在如果你不想解释,那么请不要评论,我也没有看到,在张贴部分的任何地方,关于发布电子表格快照的任何事情,似乎都是你不喜欢的。请不要电子表格的截图。我如何在堆栈溢出中创建一个表而不花很长时间?这就是你的回答?认真地为什么你会期望有人永远回答你的问题?只是指出,这是一个电子表格截图超级简单,我不知道如何做它的其他方式,现在如果你不想解释,那么请不要评论,我也没有看到,在张贴部分的任何地方,任何关于发布电子表格的照片,似乎都是你不喜欢的。