簡體   English   中英

SQL - 按屬性過濾行

[英]SQL - Filter rows by attributes

我有 3 個表用戶、工作流、屬性。 WORKFLOWS 表的值將是 NEW、UPDATE 和 DELETE,並且 ATTRIBUTES 表包含工作流屬性,如評論、doneBy、dateOfDone 等有很多。 用戶和工作流表通過 USR_ID 連接,工作流和屬性通過 WF_ID 連接。

+--------+------------+------------+    +--------+---------+------------+
|USR_ID  | USR_NAME   | IS_GENUINE |    |WF_ID   | WF_NAME | USR_ID     |
+--------|------------+------------+    +--------|---------+------------+
| 1      |  John      |   Y        |    | 1      |  NEW    |   1        |
| 2      |  King      |   Y        |    | 2      |  Update |   1        |
| 3      |  Mark      |   N        |    | 3      |  Delete |   1        |
| 4      |  Smith     |   N        |    | 4      |  Delete |   2        |
| 5      |  Zack      |   Y        |    | 5      |  Update |   2        |
+---------------------+------------+    | 6      |  New    |   3        |
                                        | 7      |  Update |   5        |
                                        +------------------+------------+
+--------+--------------+----------------+-------+
|ATTR_ID | ATTR_NAME   | ATTR_VALUE      | WF_ID |
+--------|------------- +----------------+-------+
| 1      |  comment     |   good         |  1    |
| 2      |  doneBy      |   suresh       |  1    |
| 3      |  comment     |   good         |  2    |
| 4      |  doneBy      |   suresh       |  2    |
| 5      |  comment     |   bad          |  3    |
| 6      |  doneBy      |   Raj          |  3    |
| 7      |  comment     |   Wow          |  4    |
| 8      |  doneBy      |   Amit         |  4    |
| 9      |  comment     |   good         |  7    |
| 10     |  doneBy      |   suresh       |  7    |
+-----------------------+----------------+-------+

如果 IS_GENUINE = 'y' 和 comment = 'good' 和 doneBy = 'suresh',那么我想獲取用戶信息以及工作流和屬性的主要問題出現了。

預計 Output

+--------+------------+---------+-----------+------------+
|USR_ID  | USR_NAME   | WF_NAME | ATTR_NAME | ATTR_VALUE |
+--------|------------+---------+-----------|------------+
| 1      |  John      |  NEW    |  comment  | good       |
+--------|------------+---------+-----------|------------+
| 1      |  John      |  NEW    |  doneBy   | suresh     |
+--------|------------+---------+-----------|------------+
| 1      |  John      |  UPDATE |  comment  | good       |
+--------|------------+---------+-----------|------------+
| 1      |  John      |  UPDATE |  doneBy   | suresh     |
+--------|------------+---------+-----------|------------+
| 1      |  John      |  DELETE |  comment  | bad        |
+--------|------------+---------+-----------|------------+
| 1      |  John      |  DELETE |  doneBy   | Raj        |
+--------|------------+---------+-----------|------------+
| 5      |  Zack      |  UPDATE |  comment  | good       |
+--------|------------+---------+-----------|------------+
| 5      |  Zack      |  UPDATE |  doneBy   | suresh     |
+--------|------------+---------+-----------|------------+

您可以使用 windows function count如下:

Select USR_ID, USR_NAME, WF_NAME, ATTR_NAME, ATTR_VALUE from
(Select U.USR_ID, U.USR_NAME, W.WF_NAME, A.ATTR_NAME, A.ATTR_VALUE, 
        count(case when a.attr_name = 'comment' and attr_value = 'good' then 1 end) over (partition by a.wf_id) as comm_cnt,
        count(case when a.attr_name = 'doneBy' and attr_value = 'suresh' then 1 end) over (partition by a.wf_id) as suresh_cnt
from
Users u join workflow w
On u.user_id = w.user_id
Join attributes a on a.wf_id = w.wf_id
Where is_genuine = 'Y')
Where comm_cnt = 1 and suresh_cnt = 1;

試試這個:

SELECT user_id, usr_name, wf_name, attr_name, attr_value
FROM users AS u JOIN attributes AS a ON u.user_id = a.user_id , workflows
    JOIN attr_value ON b.wf_id = c.wf_id
WHERE is_genuine = 'y' AND
    (attr_name = 'comment' AND attr_value = 'good') OR
    (attr_name = 'doneBy' AND attr_value = 'suresh');

PS我會重組ATTRIBUTES 嘗試這個:

+--------+---------+----------+-------+
|ATTR_ID | COMMENT | BY       | WF_ID |
+--------|---------+----------+-------+
| 1      |  good   |  suresh  |  1    |
| 2      |  good   |  suresh  |  2    |
| 3      |  bad    |  Raj     |  3    |
| 4      |  Wow    |  Amit    |  4    |
| 5      |  good   |  suresh  |  7    |
+------------------+----------+-------+

然后查詢可能如下所示:

SELECT user_id, usr_name, wf_name, attr_name, attr_value
FROM users AS u JOIN attributes AS a ON u.user_id = a.user_id , workflows
    JOIN attr_value ON b.wf_id = c.wf_id
WHERE is_genuine = 'y' AND
    (comment = 'good' AND by = 'suresh');

結果將如下所示:

+---------+----------+---------+---------+--------+
| usr_id  | usr_name | wf_name | comment | by     |
+---------|----------+---------+---------|--------+
|  1      |  John    |  new    |  good   | suresh |
|  1      |  John    |  update |  good   | suresh |
|  1      |  John    |  delete |  bad    | Raj    |
|  5      |  Zack    |  update |  good   | suresh |
+---------|----------+---------+---------|--------+

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM