Sql 选择聚合忽略原因所在
我正在尝试将现有视图转换为可以使用的格式 视图Sql 选择聚合忽略原因所在,sql,sql-server,tsql,Sql,Sql Server,Tsql,我正在尝试将现有视图转换为可以使用的格式 视图vw\u temp\u apphoursrasttwoentries如下所示: RowNumber | PersNr | Client | Localtion | Agent | Date | Calweek | Year ----------+--------+--------+-----------+-------+------------+---------+------ 1 | 123 | 1 |
vw\u temp\u apphoursrasttwoentries
如下所示:
RowNumber | PersNr | Client | Localtion | Agent | Date | Calweek | Year
----------+--------+--------+-----------+-------+------------+---------+------
1 | 123 | 1 | 1 | ag-01 | 2020-01-01 | 1 | 2021
2 | 123 | 1 | 1 | ag-01 | 2020-01-03 | 1 | 2021
1 | 9999 | 1 | 4 | ag-01 | 2020-01-01 | 1 | 2021
2 | 9999 | 1 | 4 | ag-01 | 2020-01-07 | 1 | 2021
PersNr | Client | Localtion | Agent | minDate | MaxDate | DateDiff | Calweek | Year
-------+--------+-----------+-------+------------+------------+----------+---------+-------
123 | 1 | 1 | ag-01 | 2020-01-01 | 2020-01-03 | 3 | 1 | 2021
9999 | 1 | 4 | ag-01 | 2020-01-01 | 2020-01-07 | 7 | 1 | 2021
我需要不同格式的数据,如下所示:
RowNumber | PersNr | Client | Localtion | Agent | Date | Calweek | Year
----------+--------+--------+-----------+-------+------------+---------+------
1 | 123 | 1 | 1 | ag-01 | 2020-01-01 | 1 | 2021
2 | 123 | 1 | 1 | ag-01 | 2020-01-03 | 1 | 2021
1 | 9999 | 1 | 4 | ag-01 | 2020-01-01 | 1 | 2021
2 | 9999 | 1 | 4 | ag-01 | 2020-01-07 | 1 | 2021
PersNr | Client | Localtion | Agent | minDate | MaxDate | DateDiff | Calweek | Year
-------+--------+-----------+-------+------------+------------+----------+---------+-------
123 | 1 | 1 | ag-01 | 2020-01-01 | 2020-01-03 | 3 | 1 | 2021
9999 | 1 | 4 | ag-01 | 2020-01-01 | 2020-01-07 | 7 | 1 | 2021
在原始格式中,一个人只有两行(行编号1和2
)。我想匹配每一列,并在新视图中具有最小和最大日期以及差异
我的代码:
select a.persnr, a.client, a.location, a.agent, a.calweek, a.year,
max(a.date) as maxdate, min(b.date) as mindate
, DATEDIFF(day,a.date,b.date) as dDiff
from vw_temp_appHoursLastTwoEntries a
left join vw_temp_appHoursLastTwoEntries b on
a.persnr = b.persnr and a.client = b.client and
a.agent = b.agent and a.date = b.date
where a.date != b.date and DATEDIFF(day,a.date,b.date) != 0
or (a.date is not null and b.date is not null)
group by a.persnr, a.client, a.location, a.agent, a.calweek, a.year, DATEDIFF(day,a.date,b.date)
问题:
select a.persnr, a.client, a.location, a.agent, a.calweek, a.year,
max(a.date) as maxdate, min(b.date) as mindate
, DATEDIFF(day,a.date,b.date) as dDiff
from vw_temp_appHoursLastTwoEntries a
left join vw_temp_appHoursLastTwoEntries b on
a.persnr = b.persnr and a.client = b.client and
a.agent = b.agent and a.date = b.date
where a.date != b.date and DATEDIFF(day,a.date,b.date) != 0
or (a.date is not null and b.date is not null)
group by a.persnr, a.client, a.location, a.agent, a.calweek, a.year, DATEDIFF(day,a.date,b.date)
我现在得到的值似乎是原因不起作用,但我不明白为什么
a.date!=b、 date
不应返回min-
和maxdates
相同的行。即使min-
和maxdates
不同,datediff
也不会返回任何其他值0
。这是您想要的:
declare @Test table (RowNumber int, PersNr int, Client int, Localtion int, Agent varchar(5), [Date] date, Calweek int, [Year] int);
insert into @Test (RowNumber, PersNr, Client, Localtion, Agent, [Date], Calweek, [Year])
values
(1, 123, 1, 1, 'ag-01', '2020-01-01', 1, 2021),
(2, 123, 1, 1, 'ag-01', '2020-01-03', 1, 2021),
(1, 9999, 1, 4, 'ag-01', '2020-01-01', 1, 2021),
(2, 9999, 1, 4, 'ag-01', '2020-01-07', 1, 2021);
select a.PersNr, a.Client, a.Localtion, a.Agent, a.Calweek, a.[Year]
, max(a.[date]) as maxdate
, min(b.[date]) as mindate
, abs(datediff(day,a.[date],b.[date])) as dDiff
from @Test a
left join @Test b on
a.persnr = b.persnr and a.client = b.client and
a.agent = b.agent --and a.[date] = b.[date]
where (/*a.[date] != b.[date] and*/ datediff(day,a.[date],b.[date]) != 0)
and /* not OR */ (a.[date] is not null and b.[date] is not null)
group by a.persnr, a.client, a.Localtion, a.agent, a.calweek, a.[Year], abs(datediff(day,a.[date],b.[date]));
返回:
人事
客户
地点
代理人
卡尔韦克
年
maxdate
心仪的
迪夫
123
1.
1.
ag-01
1.
2021
2020-01-03
2020-01-01
2.
9999
1.
4.
ag-01
1.
2021
2020-01-07
2020-01-01
6.
这个条件真的是关于条款的一部分吗?非常感谢!第二次你救了我:)完全错过了,非常感谢你的解释!