sql依靠join和union语句

时间:2019-06-05 01:54:49

标签: sql sql-server

我有以下表格(city1,city2,city3(等)和Users)

city1表

id   userid   notes                    noteAdded
1    55       this is a test           2019-03-18 14:40:05.227
2    56       another test             2019-04-18 14:40:05.227
3    55       third test               2019-04-16 14:40:05.227

city2表

id    userid    notes                    noteAdded
1     55        this is city2 notes      2019-04-18 14:40:05.227
2     57        this is another note     2019-04-17 14:40:05.227

city3表

id    userid    notes    noteAdded
........

用户表

id     username
55     bob smith
56     james brown
57     scott bean

我正在尝试按提交时间desc排序

id    userid   username     noteadded            count
1     55       bob smith    04-18-fulldatehere   3
2     56       james brown  04-18...             1
3     57       scott bean   04-17...             1
4     55       bob smith    04-16...             3

我有以下

select userid, noteadded, username
from (
  SELECT userid, noteadded, u.username
  FROM city1 cone
  left join users on cone.userid = u.id 

  union

  SELECT userid, noteadded, u.username
  FROM city2 ctwo
  left join users u on ctwo.userid = u.id
)
as rr
order by noteadded desc;

这是可行的,但是一旦我在该子命令之前添加了COUNT(userid)(括号之间的select语句),我就会收到错误消息,指出rr.userid无效。如果删除计数,则该语句有效。我添加as rr部分的唯一原因是因为没有它,我无法嵌套子查询,即我无法做到这一点:

select userid, noteadded, username
from (
  SELECT userid, noteadded, u.username
  FROM city1 cone
  left join users on cone.userid = u.id 

  union

  SELECT userid, noteadded, u.username
  FROM city2 ctwo
  left join users u on ctwo.userid = u.id
)
order by noteadded desc;

我对sql语句的目标是测量用户的活跃程度-从而进行计数。最终,我计划限制结果和/或按日期计数。说,只显示过去30天内的物品。

2 个答案:

答案 0 :(得分:0)

如果我的理解正确,您可以将{city}表一起union,然后执行join

select u.id, u.username, count(*), max(noteadded)
from users u left join
     (select c.*
      from ((select c.* from city1) union all
            (select c.* from city2) union all
            (select c.* from city3) 
           ) c
     ) c
     on c.userid = u.id
group by u.id

答案 1 :(得分:0)

如果要计数(这是一种汇总功能),则还需要group by,对于要显示的每一列,您必须汇总或group by

>
declare @City1 table (id int, userid int, notes varchar(128), noteAdded datetime);
declare @City2 table (id int, userid int, notes varchar(128), noteAdded datetime);
declare @User table (id int, username varchar(64));

insert into @City1 (id, userid, notes, noteAdded)
  select 1,    55,       'this is a test',           '2019-03-18 14:40:05.227' union all
  select 2,    56,       'another test',             '2019-04-18 14:40:05.227' union all
  select 3,    55,       'third test',               '2019-04-16 14:40:05.227';

insert into @City2 (id, userid, notes, noteAdded)
  select 1,    55,       'this is city2 notes',           '2019-04-18 14:40:05.227' union all
  select 2,    57,       'this is another note',          '2019-04-17 14:40:05.227';

insert into @User (id, username)
  select 55,     'bob smith' union all
  select 56,     'james brown' union all
  select 57,     'scott bean'

select userid
  , username
  , max(noteadded) noteadded /* Has to be an aggregate since we're grouping */
  , count(userid) [count]
from (
  SELECT userid, noteadded, u.username
  FROM @city1 cone
  left join @user u on cone.userid = u.id 

  union all

  SELECT userid, noteadded, u.username
  FROM @city2 ctwo
  left join @user u on ctwo.userid = u.id
)
as rr
group by UserId, username;
--order by noteadded desc; -- Can't order by a column being aggregated

PS:如果我可以鼓励您以这种格式发布所有问题,即创建临时表或表变量,插入数据并尝试查询。这样做使人们很容易回答很多,并会鼓励更多更快的响应。