GROUP BY,每个组使用 Key:Value 个过滤器
GROUP BY with Key:Value filters for each group
给定具有以下架构的 table:
记录编号
ProcessId
数据
80830F00-379C-11EA-96B7-372060281AFC
1
4.2
80830F00-379C-11EA-96B7-372060281AFC
2
445588662211448
80830F00-379C-11EA-96B7-372060281AFC
7
1
791F6D30-379C-11EA-96B7-372060281AFC
1
3.3
791F6D30-379C-11EA-96B7-372060281AFC
2
999999999999999
791F6D30-379C-11EA-96B7-372060281AFC
7
2
所以 RecordId
代表一些分组键,每个 ProcessId
都有它的 Data
。
现在我有一个看起来像这样的过滤器:
filter: [
{ "1": "3.3" },
{ "2": "999999999999999" }
]
所以 key
是 ProcessId
而 value
是 Data
。
问题
如何获得所有满足查询的 RecordId
,这意味着每个组(按 RecordId
分组)应包含具有特定数据的特定进程。
我认为应该这样使用:
SELECT RecordId
FROM [SomeTable]
GROUP BY RecordId
HAVING --- the row with ProcessId = 1 AND Data = '3.3'
AND --- the row with ProcessId = 2 AND Data = '999999999999999'
您可以将 having
与 case
一起使用:
SELECT RecordId
FROM [SomeTable]
GROUP BY RecordId
HAVING SUM(CASE WHEN ProcessId = 1 AND Data = '3.3' THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN ProcessId = 2 AND Data = '999999999999999' THEN 1 ELSE 0 END) > 0;
给定具有以下架构的 table:
记录编号 | ProcessId | 数据 |
---|---|---|
80830F00-379C-11EA-96B7-372060281AFC | 1 | 4.2 |
80830F00-379C-11EA-96B7-372060281AFC | 2 | 445588662211448 |
80830F00-379C-11EA-96B7-372060281AFC | 7 | 1 |
791F6D30-379C-11EA-96B7-372060281AFC | 1 | 3.3 |
791F6D30-379C-11EA-96B7-372060281AFC | 2 | 999999999999999 |
791F6D30-379C-11EA-96B7-372060281AFC | 7 | 2 |
所以 RecordId
代表一些分组键,每个 ProcessId
都有它的 Data
。
现在我有一个看起来像这样的过滤器:
filter: [
{ "1": "3.3" },
{ "2": "999999999999999" }
]
所以 key
是 ProcessId
而 value
是 Data
。
问题
如何获得所有满足查询的 RecordId
,这意味着每个组(按 RecordId
分组)应包含具有特定数据的特定进程。
我认为应该这样使用:
SELECT RecordId
FROM [SomeTable]
GROUP BY RecordId
HAVING --- the row with ProcessId = 1 AND Data = '3.3'
AND --- the row with ProcessId = 2 AND Data = '999999999999999'
您可以将 having
与 case
一起使用:
SELECT RecordId
FROM [SomeTable]
GROUP BY RecordId
HAVING SUM(CASE WHEN ProcessId = 1 AND Data = '3.3' THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN ProcessId = 2 AND Data = '999999999999999' THEN 1 ELSE 0 END) > 0;