简体   繁体   English

SQL - 按属性过滤行

[英]SQL - Filter rows by attributes

I have 3 tables USERS, WORKFLOWS, ATTRIBUTES.我有 3 个表用户、工作流、属性。 WORKFLOWS tables values will be NEW, UPDATE and DELETE and ATTRIBUTES table contains workflow attributes like comment, doneBy, dateOfDone etc there are many. WORKFLOWS 表的值将是 NEW、UPDATE 和 DELETE,并且 ATTRIBUTES 表包含工作流属性,如评论、doneBy、dateOfDone 等有很多。 Users and Workflows table are connected by USR_ID and workflows and Attributes are connected by WF_ID.用户和工作流表通过 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    |
+-----------------------+----------------+-------+

Now comes the major question I want to fetch the user info along with workflows and attributes if the IS_GENUINE = 'y' and comment = 'good' and doneBy = 'suresh'.如果 IS_GENUINE = 'y' 和 comment = 'good' 和 doneBy = 'suresh',那么我想获取用户信息以及工作流和属性的主要问题出现了。

Expected Output预计 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     |
+--------|------------+---------+-----------|------------+

You can use windows function count as follows:您可以使用 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;

Try this one:试试这个:

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 I would restructure ATTRIBUTES . PS我会重组ATTRIBUTES Try this:尝试这个:

+--------+---------+----------+-------+
|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    |
+------------------+----------+-------+

Then the query can look like this:然后查询可能如下所示:

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');

Results will look like this:结果将如下所示:

+---------+----------+---------+---------+--------+
| 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