Sql server SQL Server:像用户名一样组合
我正在尝试合并Sql server SQL Server:像用户名一样组合,sql-server,Sql Server,我正在尝试合并用户名行中的类似名称 如果我有以下几点: Bob Barker Johnny Rogers Bob Barker Bob Barker Neal String Bobby McNight Johnny Rogers 我希望是: Bob Barker Johnny Rogers Neal String Bobby McNight 到目前为止,我的问题是: SELECT [UserName] ,[AffNumber] ,[blah] ,[tDate]
用户名
行中的类似名称
如果我有以下几点:
Bob Barker
Johnny Rogers
Bob Barker
Bob Barker
Neal String
Bobby McNight
Johnny Rogers
我希望是:
Bob Barker
Johnny Rogers
Neal String
Bobby McNight
到目前为止,我的问题是:
SELECT [UserName]
,[AffNumber]
,[blah]
,[tDate]
,[DIF]
FROM [WWebsite].[dbo].[Log], [WWebsite].[dbo].[Users]
WHERE blah = '62055'
AND tDate > '2013-12-31 00:00:00'
AND tDate < '2014-04-01 00:00:00'
Users
表包含以下列:
id, UserId, DIF, WalkDate,...
id, UserName, Weight,...
选择DISTINCT
[用户名]
,[AffNumber]
,[废话]
,[tDate]
,[DIF]
从[WWebsite].[dbo].[Log]a
在a.UserId=b.Id上加入[WWebsite].[dbo].[Users]b
其中blah='62055'
和tDate>“2013-12-31 00:00:00”
日期<'2014-04-01 00:00:00'
如果你也想要这些复制品,你可以尝试这样做:
SELECT [UserName]
,max([AffNumber])
,max([blah])
,max([tDate])
,max([DIF])
FROM [WWebsite].[dbo].[Log]
JOIN [WWebsite].[dbo].[Users] b on a.UserId = b.Id
WHERE blah = '62055'
AND tDate > '2013-12-31 00:00:00'
AND tDate < '2014-04-01 00:00:00'
GROUP BY UserName
选择[用户名]
,最大值([AffNumber])
,最大值([blah])
,最大值([tDate])
,最大值([DIF])
来自[WWebsite].[dbo].[Log]
在a.UserId=b.Id上加入[WWebsite].[dbo].[Users]b
其中blah='62055'
和tDate>“2013-12-31 00:00:00”
日期<'2014-04-01 00:00:00'
按用户名分组
其他列是否也返回相同的值?是用户名还是所有其他字段都相同,如AffNumber、Blah、tDate、Dif等?如果它们是不同的,那么您希望其他领域会发生什么?你想数一数他们,平均值,总和等吗?它们是不同的记录,那么对它们进行“分组”的计划是什么?@shree.pat18用户名为什么不从中选择不同的用户名??非常有效!谢谢
SELECT [UserName]
,max([AffNumber])
,max([blah])
,max([tDate])
,max([DIF])
FROM [WWebsite].[dbo].[Log]
JOIN [WWebsite].[dbo].[Users] b on a.UserId = b.Id
WHERE blah = '62055'
AND tDate > '2013-12-31 00:00:00'
AND tDate < '2014-04-01 00:00:00'
GROUP BY UserName