Sql 如何根据列';发生顺序
我需要使用date列提取所有与其各自ID相关联的行,这些行在type列中的“In”出现在“Out”之前。在提供的数据中,只有Id 1和Id 2可以通过测试。 试验数据如下:Sql 如何根据列';发生顺序,sql,sql-server,Sql,Sql Server,我需要使用date列提取所有与其各自ID相关联的行,这些行在type列中的“In”出现在“Out”之前。在提供的数据中,只有Id 1和Id 2可以通过测试。 试验数据如下: CREATE TABLE #Table ( id INT, [type] varchar(25), [Dates] Date) INSERT INTO #Table VALUES (1, 'In', '2018-10-01'), (1, 'In', '2018-11-01'), (1, 'Out', '2018-12-
CREATE TABLE #Table (
id INT,
[type] varchar(25),
[Dates] Date)
INSERT INTO #Table
VALUES (1, 'In', '2018-10-01'),
(1, 'In', '2018-11-01'),
(1, 'Out', '2018-12-01'),
(2, 'In', '2018-10-01'),
(2, 'Out', '2018-11-01'),
(2, 'In', '2018-12-01'),
(3, 'Out', '2018-10-01'),
(3, 'In', '2018-11-01')
输出应如下所示:
+----+------+------------+
| id | type | date |
+----+------+------------+
| 1 | In | 2018-10-01 |
| 1 | In | 2018-11-01 |
| 1 | Out | 2018-12-01 |
| 2 | In | 2018-10-01 |
| 2 | Out | 2018-11-01 |
| 2 | In | 2018-12-01 |
+----+------+------------+
我真的对这个问题感到困惑。
我从
SELECT #Table.*, MIN(CASE WHEN #Table.[type] = 'In' THEN #Table.Dates ELSE NULL END) As A
,MIN(CASE WHEN #Table.[type] = 'Out' THEN #Table.Dates ELSE NULL END) As B
FROM #Table
GROUP BY #Table.id, #Table.[type], #Table.Dates
不知道接下来该怎么办…如果我理解正确,聚合和
让
做这个把戏:
select t.id
from #Table t
group by t.id
having min(case when type = 'In' then dates end) < max(case when type = 'Out' then dates end);
这是Gordon使用窗口聚合应用于查询的逻辑:
这可能对您有用:
With CTE As
(
Select Id,[Type], [Dates], ROW_NUMBER() OVER(Partition By Id Order By Dates) As rn From #Tbl
)
Select * From cte
Where cte.Id In (Select Distinct Id From CTE Where rn = 1 And [Type] = 'In')
您可以使用如下所示的内部联接尝试以下操作
CREATE TABLE #Table (
id INT,
[type] varchar(25),
[Dates] Date)
INSERT INTO #Table
VALUES (1, 'In', '2018-10-01'),
(1, 'In', '2018-11-01'),
(1, 'Out', '2018-12-01'),
(2, 'In', '2018-10-01'),
(2, 'Out', '2018-11-01'),
(2, 'In', '2018-12-01'),
(3, 'Out', '2018-10-01'),
(3, 'In', '2018-11-01')
SELECT DISTINCT #Table.* FROM(
SELECT * FROM #Table
)a inner join (
SELECT * FROM #Table
)b on a.id = b.id and a.Dates < b.Dates and a.[type] = 'In' and b.[type] = 'Out'
inner join #Table on a.id = #Table.id
我还需要它所有对应的行,而不仅仅是Id@Gordon Linoffim,我只是不确定为什么我们在“Out”之前的type列中使用MAX而不是MINIt's your logic'In':-)您想要在MIN(In date)处的行
select *
from #Table
where id in
( Gordon's Select )
With CTE As
(
Select Id,[Type], [Dates], ROW_NUMBER() OVER(Partition By Id Order By Dates) As rn From #Tbl
)
Select * From cte
Where cte.Id In (Select Distinct Id From CTE Where rn = 1 And [Type] = 'In')
CREATE TABLE #Table (
id INT,
[type] varchar(25),
[Dates] Date)
INSERT INTO #Table
VALUES (1, 'In', '2018-10-01'),
(1, 'In', '2018-11-01'),
(1, 'Out', '2018-12-01'),
(2, 'In', '2018-10-01'),
(2, 'Out', '2018-11-01'),
(2, 'In', '2018-12-01'),
(3, 'Out', '2018-10-01'),
(3, 'In', '2018-11-01')
SELECT DISTINCT #Table.* FROM(
SELECT * FROM #Table
)a inner join (
SELECT * FROM #Table
)b on a.id = b.id and a.Dates < b.Dates and a.[type] = 'In' and b.[type] = 'Out'
inner join #Table on a.id = #Table.id
id type Dates
--------------------
1 In 2018-10-01
1 In 2018-11-01
1 Out 2018-12-01
2 In 2018-10-01
2 In 2018-12-01
2 Out 2018-11-01