同一和不同表中的多个列合并为一个列

时间:2019-01-04 06:04:07

标签: sql sql-server database ssms

我正在尝试使用下面的代码将多列合并为一行。

select A.CreateDate as JoinDate, B.Section, C.UserName, cast(B.TotalProgress as decimal(10,0)) as TotalProgress
from B
join C on B.UserName = C.UserName 
join A on C.UserId = A.UserId
WHERE (A.[CreateDate] >= '1/1/2018') 
AND ((A.[CreateDate]) <= ('1/1/2019'))
UNION
select A.CreateDate as JoinDate, B.Section, C.UserName, cast(D.TotalProgress as decimal(10,0)) as TotalProgress
from B 
join C on B.UserName = C.UserName 
join A on C.UserId = A.UserId 
join D on C.UserName = D.UserName 
WHERE (A.[CreateDate] >= '1/1/2018') 
AND ((A.[CreateDate]) <= ('1/1/2019'))
UNION
select A.CreateDate as JoinDate, B.Section, C.UserName, cast(B.TotalProgress_A as decimal(10,0)) as TotalProgress
from B 
join C on B.UserName = C.UserName 
join A on C.UserId = A.UserId
WHERE (A.[CreateDate] >= '1/1/2018') 
AND ((A.[CreateDate]) <= ('1/1/2019'))
order by A.CreateDate desc

问题是查询正在重复。我认为问题在于select语句中的TotalProgress。有什么办法可以消除查询中的重复项?我正在使用SQL Server 2016 Management Studio运行此查询。

下面是我运行查询时的结果。

JoinDate    | Section | UserName | TotalProgress
2019-01-03  | 1A      | test1    | 0
2019-01-03  | 1A      | test1    | 1
2019-01-03  | 1A      | test1    | 22
2019-01-03  | 2B      | test2    | 0
2019-01-03  | 2B      | test2    | 33
2019-01-03  | 3C      | test3    | 0
2019-01-03  | 3C      | test3    | 1

如您所见。您会注意到进度是唯一的不同。我试图使用不重复但不起作用。在某些情况下,总进度中的某些行全为0。但是总的来说,我希望它像这样。

JoinDate    | Section | UserName | TotalProgress
2019-01-03  | 1A      | test1    | 22
2019-01-03  | 2B      | test2    | 33
2019-01-03  | 3C      | test3    | 1

2 个答案:

答案 0 :(得分:0)

您参加了3次Union,因此即将出现具有不同TotalProgress的类似记录。似乎正常。 您想要什么-将所有3行作为3列放在一行中?

答案 1 :(得分:0)

您不应为此逻辑使用 if (bereq.url ~ "^(/media/|/skin/|/js/|/)(?:(?:index|litespeed)\.php/)?") { unset beresp.http.Vary; set beresp.do_gzip = true; if (beresp.status != 200 && beresp.status != 404) { set beresp.ttl = 15s; set beresp.uncacheable = true; return (deliver); } else { if (beresp.http.Set-Cookie) { set beresp.http.X-Varnish-Set-Cookie = beresp.http.Set-Cookie; unset beresp.http.Set-Cookie; } //unset beresp.http.Cache-Control; //unset beresp.http.Expires; // unset beresp.http.Pragma; // unset beresp.http.Cache; // unset beresp.http.Age; // if (beresp.http.X-Turpentine-Esi == "1") { set beresp.do_esi = true; } if (beresp.http.X-Turpentine-Cache == "0") { set beresp.ttl = 15s; set beresp.uncacheable = true; return (deliver); } else { if (true && bereq.url ~ ".*\.(?:css|js|jpe?g|png|gif|ico|swf|mp4)(?=\?|&|$)") { //unset beresp.http.set-cookie; set beresp.ttl = 28800s; set beresp.http.Cache-Control = "max-age=28800"; } elseif (bereq.http.X-Varnish-Esi-Method) { if (bereq.http.X-Varnish-Esi-Access == "private" && bereq.http.Cookie ~ "frontend=") { set beresp.http.X-Varnish-Session = regsub(bereq.http.Cookie, "^.*?frontend=([^;]*);*.*$", "\1"); } if (bereq.http.X-Varnish-Esi-Method == "ajax" && bereq.http.X-Varnish-Esi-Access == "public") { // Public ajax requests set beresp.http.Cache-Control = "max-age=" + regsub( bereq.url, ".*/ttl/(\d+)/.*", "\1"); } set beresp.ttl = std.duration( regsub( bereq.url, ".*/ttl/(\d+)/.*", "\1s"), 300s); if (beresp.ttl == 0s) { set beresp.ttl = 15s; set beresp.uncacheable = true; return (deliver); } } else { set beresp.ttl = 3600s; } } } 。我想您想要这样的东西:

union

select A.CreateDate as JoinDate, B.Section, C.UserName, bd.TotalProgress from B join C on B.UserName = C.UserName join A on C.UserId = A.UserId left join D on C.UserName = D.UserName outer apply (select max(TotalProgress) as TotalProgress from (values (B.TotalProgress ), (B.TotalProgress_A), (D.TotalProgress ) ) v(TotalProgress) ) bd where A.[CreateDate] >= '2018-01-01' and A.[CreateDate]) < '2019-01-01' -- guessing you don't really want any data from 2019 的目的仅仅是获得三列的最大值。