编写 select 语句时遇到问题,即 returns 用户别名每年出现在查询日志中的 1 行
Having trouble writing select statement that returns 1 row for each year that a user alias appears in a query log
我正在将查询日志 table 中的数据插入到用户 table 中。在查询日志table中,每次执行查询时都会出现用户别名。在用户 table 中,我希望用户别名列具有适用于别名的每个日期和公司名称组合的用户别名,没有重复行。我需要这个的原因是因为我正在创建一个视觉效果,需要反映每年为每个企业执行查询的用户的不同数量。我还需要能够按别名、业务和日期进行切片。我的目标是让结果看起来像..
User Alias LOB Date
Joe Business 1 2015-01-01
Joe Business 1 2016-01-01
Carol Business 1 2015-01-01
Carol Business 1 2016-01-01
这是我目前使用下面的语句得到的结果。
User Alias LOB Date
Joe Business 1 2015-01-01
Carol Business 1 2015-01-01
我知道语句中的 "DISTINCT" 需要去掉,但在那之后,我卡住了。
INSERT INTO [DBO].[USERS] ([User Alias], [LOB], [Date])
SELECT DISTINCT [User Alias], [LOB] , ( SELECT TOP 1 [Date] FROM Business) [Date]
FROM Business [LOG]
WHERE NOT EXISTS (
SELECT 1
FROM [dbo].[Users] [DIM]
WHERE [LOG].[User Alias] = [DIM].[User Alias]
AND [LOG].[LOB] = [DIM].[LOB]
AND [LOG].[Date] = [DIM].[Date]
)
AND [User Alias] IS NOT NULL
AND [LOB] IS NOT NULL
GROUP BY Year(以及别名和 LOB)和 SELECT MIN(日期)而不是 TOP 1 子查询。
编辑:关于如何实施我的建议的更精确的例子 --
INSERT INTO [DBO].[USERS] ([User Alias], [LOB], [Date])
SELECT [User Alias], [LOB] , MIN([Date]) [Date]
FROM Business [LOG]
WHERE NOT EXISTS (
SELECT 1
FROM [dbo].[Users] [DIM]
WHERE [LOG].[User Alias] = [DIM].[User Alias]
AND [LOG].[LOB] = [DIM].[LOB]
AND YEAR([LOG].[Date]) = YEAR([DIM].[Date])
)
AND [User Alias] IS NOT NULL
AND [LOB] IS NOT NULL
GROUP BY [User Alias], [LOB] , YEAR([Date])
这似乎可以解决问题。
SELECT distinct [User Alias], [LOB], datepart(yyyy,[Date]) [Date]
FROM Business [LOG]
WHERE NOT EXISTS (SELECT 1 FROM [dbo].[Users] [DIM]
WHERE [LOG].[User Alias] = [DIM].[User Alias]
and [LOG].[LOB] = [DIM].[LOB] and [LOG].[Date] = [DIM].[Date])
and [User Alias] is not null
and [LOB] is not null
GROUP BY [DATE], [User Alias], [LOB]
我正在将查询日志 table 中的数据插入到用户 table 中。在查询日志table中,每次执行查询时都会出现用户别名。在用户 table 中,我希望用户别名列具有适用于别名的每个日期和公司名称组合的用户别名,没有重复行。我需要这个的原因是因为我正在创建一个视觉效果,需要反映每年为每个企业执行查询的用户的不同数量。我还需要能够按别名、业务和日期进行切片。我的目标是让结果看起来像..
User Alias LOB Date
Joe Business 1 2015-01-01
Joe Business 1 2016-01-01
Carol Business 1 2015-01-01
Carol Business 1 2016-01-01
这是我目前使用下面的语句得到的结果。
User Alias LOB Date
Joe Business 1 2015-01-01
Carol Business 1 2015-01-01
我知道语句中的 "DISTINCT" 需要去掉,但在那之后,我卡住了。
INSERT INTO [DBO].[USERS] ([User Alias], [LOB], [Date])
SELECT DISTINCT [User Alias], [LOB] , ( SELECT TOP 1 [Date] FROM Business) [Date]
FROM Business [LOG]
WHERE NOT EXISTS (
SELECT 1
FROM [dbo].[Users] [DIM]
WHERE [LOG].[User Alias] = [DIM].[User Alias]
AND [LOG].[LOB] = [DIM].[LOB]
AND [LOG].[Date] = [DIM].[Date]
)
AND [User Alias] IS NOT NULL
AND [LOB] IS NOT NULL
GROUP BY Year(以及别名和 LOB)和 SELECT MIN(日期)而不是 TOP 1 子查询。
编辑:关于如何实施我的建议的更精确的例子 --
INSERT INTO [DBO].[USERS] ([User Alias], [LOB], [Date])
SELECT [User Alias], [LOB] , MIN([Date]) [Date]
FROM Business [LOG]
WHERE NOT EXISTS (
SELECT 1
FROM [dbo].[Users] [DIM]
WHERE [LOG].[User Alias] = [DIM].[User Alias]
AND [LOG].[LOB] = [DIM].[LOB]
AND YEAR([LOG].[Date]) = YEAR([DIM].[Date])
)
AND [User Alias] IS NOT NULL
AND [LOB] IS NOT NULL
GROUP BY [User Alias], [LOB] , YEAR([Date])
这似乎可以解决问题。
SELECT distinct [User Alias], [LOB], datepart(yyyy,[Date]) [Date]
FROM Business [LOG]
WHERE NOT EXISTS (SELECT 1 FROM [dbo].[Users] [DIM]
WHERE [LOG].[User Alias] = [DIM].[User Alias]
and [LOG].[LOB] = [DIM].[LOB] and [LOG].[Date] = [DIM].[Date])
and [User Alias] is not null
and [LOB] is not null
GROUP BY [DATE], [User Alias], [LOB]