如果可能,我需要以下示例查詢。
我似乎什么都做不了(我試過如果存在,不存在,等等)。我現在只有一個基本的選擇陳述句,我只是卡住了,我的 SQL 很糟糕:
Select
UserID,
Date,
Type,
Step
from TableA
如果用戶 ID 在“步驟”列中具有具有“值 A”的行和具有“值 B”的行(值 A 的至少一行),則僅顯示具有“值 B”的行(排除所有包含“值 A") 按用戶 ID。
For example:
Original Table Source:
UserID | Date | Type | Step
------- ----------- ------ ---------
24261 | 12/4/2021 | Doc | Value B
24261 | 12/4/2021 | Sig | Value A
24315 | 12/4/2021 | Sig | Value A
24331 | 12/4/2021 | Doc | Value B
24331 | 12/4/2021 | Sig | Value A
24328 | 12/4/2021 | Text | Value C
24341 | 12/4/2021 | Doc | Value B
24341 | 12/4/2021 | Sig | Value A
24341 | 12/4/2021 | Doc | Value B
24341 | 12/4/2021 | Sig | Value A
24357 | 12/4/2021 | Text | Value C
24357 | 12/4/2021 | Sig | Value A
24357 | 12/4/2021 | text | Value C
預期輸出:
UserID | Date | Type | Step
------- ----------- ------ ---------
24261 | 12/4/2021 | Doc | Value B
24315 | 12/4/2021 | Sig | Value A
24331 | 12/4/2021 | Doc | Value B
24328 | 12/4/2021 | Text | Value C
24341 | 12/4/2021 | Doc | Value B
24341 | 12/4/2021 | Doc | Value B
24357 | 12/4/2021 | Text | Value C
24357 | 12/4/2021 | Sig | Value A
24357 | 12/4/2021 | Text | Value C
任何幫助,將不勝感激。
uj5u.com熱心網友回復:
此選項可能有效 - 它將值 B 連接到所有擁有它的用戶 ID/日期,然后在 A.Step 和 B.Step 列中保留所有沒有值 A 和值 B 的記錄。
Select
A.UserID,
A.Date,
A.Type,
A.Step
from t as A
LEFT JOIN (
SELECT DISTINCT
UserID,
Date,
Type,
Step
FROM t
WHERE Step = 'Value B') as B
ON B.UserID = A.UserID AND B.Date = A.Date
WHERE NOT (A.Step = 'Value A' AND B.Step IS NOT NULL)
包括我對 Stu 小提琴的查詢 -
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=11115c225929e9bd0a46ff3071c00948
uj5u.com熱心網友回復:
您可以嘗試使用以下查詢
with stepB AS(
select * from tableA where userID in(
select distinct userID from tableA where step ='Value A'
intersect
select distinct userID from tableA where step ='Value B')
and step ='Value B')
select * from stepB b
union
select * from tableA a where not exists(select * from stepB where a.step=b.step)
uj5u.com熱心網友回復:
我們可以使用 CTE 來確定哪些記錄同時具有 Value A 和 Value B,并且只選擇它們的非 Value A 記錄,否則拉取所有內容:
with cte as
(
select t.UserID
from TableA t
inner join TableA t2
on t.UserID = t2.UserID
and t.Step = 'Value B'
and t2.Step = 'Value A'
)
select *
from TableA
where (TableA.UserID not in (select UserID from cte))
or (TableA.UserID in (select UserID from cte) and TableA.Step <> 'Value A')
uj5u.com熱心網友回復:
我最初嘗試過分析函式,但意識到使用exists更可靠:
with x as (
select * ,
case when exists (select * from t t2 where t.userid=t2.userid and t2.step='Value A') then 1 end
case when exists (select * from t t2 where t.userid=t2.userid and t2.step='Value B') then 1 end Both
from t
)
select UserId, date, type, Step
from x
where (Both>0 and step='value b') or both is null
order by UserId
演示小提琴
uj5u.com熱心網友回復:
您可以從同一個表中執行 UNION ALL SELECT first row with Value B 然后 userID not existing in user id with Value B
SELECT
UserID,
Date,
Type,
Step
FROM Table WHERE Step = 'Value B'
UNION ALL
SELECT
UserID,
Date,
Type,
Step
FROM Table t1
WHERE Step <> 'Value B'
AND NOT EXISTS (
SELECT 1
FROM Table t2
WHERE t2.Step = 'Value B' AND t2.UserId = t1.UserId)
uj5u.com熱心網友回復:
……小提琴……
create table t(userid int, step varchar(10));
insert into t(userid, step)
values
(24261, 'Value B'),
(24261, 'Value A'),
(24315, 'Value A'),
(24331, 'Value B'),
(24331, 'Value A'),
(24328, 'Value C'),
(24341, 'Value B'),
(24341, 'Value A'),
(24341, 'Value B'),
(24341, 'Value A'),
(24357, 'Value C'),
(24357, 'Value A'),
(24357, 'Value C');
select *
from t as a
where step <> 'value A'
or (step = 'value A' and not exists(select * from t as b where b.userid=a.userid and b.step='value B'))
order by userid,step;
select *
from
(
select *, max(case when step='value B' then 1 else 0 end) over(partition by userid) as userhasB
from t
) as d
where step <> 'value A' or userhasB = 0;
uj5u.com熱心網友回復:
測驗:演示:dbfiddle
步驟總結:
- 獲取一組在“CTE”中同時具有 A 和 B 步驟的不同用戶
- 取表一個主資料集并左連接到這個 CTE
- 限制為兩種可能性之一。
- CTE 用戶值為 NULL,在這種情況下,他們沒有兩個 A/B 步驟
- 僅回傳 CTE 中的用戶,不包括步驟值為 A 的用戶。
.. 布置
With TableA As (
SELECT '24261' USERID,'12/4/2021' DateA,'Doc' Type,'Value B' Step UNION ALL
SELECT '24261','12/4/2021','Sig ','Value A' UNION ALL
SELECT '24315','12/4/2021','Sig ','Value A' UNION ALL
SELECT '24331','12/4/2021','Doc ','Value B' UNION ALL
SELECT '24331','12/4/2021','Sig ','Value A' UNION ALL
SELECT '24328','12/4/2021','Text','Value C' UNION ALL
SELECT '24341','12/4/2021','Doc ','Value B' UNION ALL
SELECT '24341','12/4/2021','Sig ','Value A' UNION ALL
SELECT '24341','12/4/2021','Doc ','Value B' UNION ALL
SELECT '24341','12/4/2021','Sig ','Value A' UNION ALL
SELECT '24357','12/4/2021','Text','Value C' UNION ALL
SELECT '24357','12/4/2021','Sig ','Value A' UNION ALL
SELECT '24357','12/4/2021','text','Value C'),
CTE AS (SELECT USERID, count(*) cnt
FROM (SELECT distinct userID, Step from TableA WHERE Step in ('Value A', 'Value B')) a
GROUP BY USERID
HAVING Count(*) > 1)
SELECT A.*
FROM TableA A
LEFT JOIN CTE
on A.UserID = CTE.USERID
WHERE (CTE.USERID is not null AND Step <> 'Value A')
OR (CTE.USERID is null)
給我們:
-------- ----------- ------ ---------
| USERID | DateA | Type | Step |
-------- ----------- ------ ---------
| 24261 | 12/4/2021 | Doc | Value B |
| 24315 | 12/4/2021 | Sig | Value A |
| 24331 | 12/4/2021 | Doc | Value B |
| 24328 | 12/4/2021 | Text | Value C |
| 24341 | 12/4/2021 | Doc | Value B |
| 24341 | 12/4/2021 | Doc | Value B |
| 24357 | 12/4/2021 | Text | Value C |
| 24357 | 12/4/2021 | Sig | Value A |
| 24357 | 12/4/2021 | text | Value C |
-------- ----------- ------ ---------
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/379302.html
標籤:sql sql-server
上一篇:如何提取上個月的資料
