我有以下 2 個表:
表格1
| 第 1 列 | Col2 | 第 3 列 |
|---|---|---|
| Pr1 | C1 | A型 |
| Pr2 | C2 | B型 |
| Pr3 | C3 | C型 |
| Pr4 | C4 | A型 |
| Pr5 | C5 | A型 |
映射表
| Col4 | 查找Col2 | 查找Col3 | 第 5 列 |
|---|---|---|---|
| 1111 | C1 | A型 | 是的 |
| 1111 | A型 | 不 | |
| 1111 | B型 | 不 | |
| 1111 | C型 | 不 | |
| 2222 | C1 | A型 | 是的 |
| 2222 | C2 | A型 | 是的 |
| 2222 | A型 | 不 | |
| 2222 | B型 | 不 | |
| 2222 | C型 | 不 |
CREATE TABLE Table1
([Col1] varchar(3), [Col2] varchar(2), [Col3] varchar(5))
;
INSERT INTO Table1
([Col1], [Col2], [Col3])
VALUES
('Pr1', 'C1', 'TypeA'),
('Pr2', 'C2', 'TypeB'),
('Pr3', 'C3', 'TypeC'),
('Pr4', 'C4', 'TypeA')
;
CREATE TABLE MappingTable
([Col4] int, [lookupCol2] varchar(4), [lookupCol3] varchar(5), [Col5] varchar(3))
;
INSERT INTO MappingTable
([Col4], [lookupCol2], [lookupCol3], [Col5])
VALUES
(1111, 'C1', 'TypeA', 'Yes'),
(1111, NULL, 'TypeA', 'No'),
(1111, NULL, 'TypeB', 'No'),
(1111, NULL, 'TypeC', 'No'),
(2222, 'C1', 'TypeA', 'Yes'),
(2222, 'C2', 'TypeA', 'Yes'),
(2222, NULL, 'TypeA', 'No'),
(2222, NULL, 'TypeB', 'No'),
(2222, NULL, 'TypeC', 'No')
;
我在下面寫了生成不正確資料的查詢:
select a.col1, a.col2, a.col3, b.col4, b.col5
from Table1 A
inner join
MappingTable B
on a.Col3 = b.lookupcol3
and a.Col2 = case when b.lookupcol2 is null then a.col2 else b.lookupcol2 end
錯誤資料的螢屏截圖:
期望的結果
| 第 1 列 | Col2 | 第 3 列 | Col4 | 第 5 列 |
|---|---|---|---|---|
| Pr1 | C1 | A型 | 1111 | 是的 |
| Pr1 | C1 | A型 | 2222 | 是的 |
| Pr2 | C2 | B型 | 1111 | 不 |
| Pr2 | C2 | B型 | 2222 | 不 |
| Pr3 | C3 | C型 | 1111 | 不 |
| Pr3 | C3 | C型 | 2222 | 不 |
| Pr4 | C4 | A型 | 1111 | 不 |
| Pr4 | C4 | A型 | 2222 | 不 |
| Pr5 | C5 | A型 | 1111 | 不 |
| Pr5 | C5 | A型 | 2222 | 不 |
要求是從映射表中獲取每個 Col1 記錄的映射資料。
憂慮:
Table1鍵列Col2和Col3在映射表中有Pr1的映射記錄,所以可以直接拉出匹配的記錄,但是Pr4和Pr5的鍵列對于Col2不匹配。在映射表中,如果對于任何 Col4 和 lookupCol3 組合,lookupcol2 為空,則它是標準記錄,因此在 Pr4 和 Pr5 的情況下,查詢應獲取 lookupCol2 為空的標準記錄。
不知道我在這里錯過了什么。
uj5u.com熱心網友回復:
您可以使用dense_rank解決方案來標記具有特定記錄和標準記錄的結果并將其過濾掉。
declare @Table_1 table (Col1 varchar(3), Col2 varchar(2), Col3 varchar(6));
insert into @Table_1 (Col1, Col2, Col3)
values
('Pr1', 'C1', 'Type A'),
('Pr2', 'C2', 'Type B'),
('Pr3', 'C3', 'Type C'),
('Pr4', 'C4', 'Type A'),
('Pr5', 'C5', 'Type A');
declare @MappingTable table (Col4 varchar(4), LookupCol2 varchar(6), LookupCol3 varchar(6), Col5 bit);
insert into @MappingTable (Col4, LookupCol2, LookupCol3, Col5)
values
('1111', 'C1', 'Type A', 1),
('1111', '', 'Type A', 0),
('1111', '', 'Type B', 0),
('1111', '', 'Type C', 0),
('2222', 'C1', 'Type A', 1),
('2222', 'C2', 'Type A', 1),
('2222', '', 'Type A', 0),
('2222', '', 'Type B', 0),
('2222', '', 'Type C', 0);
with cte as (
select A.Col1, A.Col2, A.Col3, B.Col4, B.Col5
-- If a specific and standard record exists this will be 2 for the standard record
, dense_rank() over (partition by A.Col1, A.Col2, A.Col3 order by B.LookupCol2 desc) rn
from @Table_1 A
inner join @MappingTable B on A.Col3 = B.LookupCol3
and A.Col2 = case when B.LookupCol2 = '' then A.Col2 else B.LookupCol2 end
)
select Col1, Col2, Col3, Col4, Col5
from cte
-- Filter out the standard records when a specific record exists
-- Comment out the following line and add rn to the output to understand how it works
where rn = 1
order by Col1, Col2;
回傳:
| 第 1 列 | Col2 | 第 3 列 | Col4 | 第 5 列 |
|---|---|---|---|---|
| Pr1 | C1 | A型 | 1111 | 1 |
| Pr1 | C1 | A型 | 2222 | 1 |
| Pr2 | C2 | B型 | 1111 | 0 |
| Pr2 | C2 | B型 | 2222 | 0 |
| Pr3 | C3 | C型 | 1111 | 0 |
| Pr3 | C3 | C型 | 2222 | 0 |
| Pr4 | C4 | A型 | 1111 | 0 |
| Pr4 | C4 | A型 | 2222 | 0 |
| Pr5 | C5 | A型 | 1111 | 0 |
| Pr5 | C5 | A型 | 2222 | 0 |
Note the DDL DML which if you add to your questions makes them much easier to answer (because we don't have to type it all in).
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/359853.html
標籤:sql sql-server 查询语句
