SQL Server matching all rows from Table1 with all rows from Table2

TMMDev

someone please help me with this query, i have 2 tables

Employee

EmployeeID   LanguageID
1            1
1            2
1            3
2            1
2            3
3            1
3            2
4            1
4            2
4            3

Task

TaskID   LanguageID   LangaugeRequired
1        1            1
1        2            0
2        1            1
2        2            1
2        3            1
3        2            0
3        3            1

LangaugeID is connected to table langauge (this table is for explaination only)

   LangaugeID   LanguageName
   1            English
   2            French
   3            Italian

is there a possilbe way to make a query which gets employees where they can speak all the languages required for each task?

for example:

  1. Task ID 1 requires only LanguageID = 1, so the result should be EmployeeID 1,2,3,4
  2. Task ID 2 requires all 3 languages, so the result should be EmployeeID 1,4
  3. Task ID 3 requires only LanguageID = 3, so the result should be EmployeeID 1,2,4
Iłya Bursov

here is another variant to do this:

select t1.taskid, t2.employeeid from
(
    select a.taskid, count(distinct a.languageid) as lang_cnt
    from
    task as a
    where a.LangaugeRequired=1
    group by a.taskid
) as t1
left outer join
(
    select a.taskid, b.employeeid, count(distinct b.languageid) as lang_cnt
    from
    task as a
    inner join
    employee as b
    on (a.LangaugeRequired=1 and a.languageid=b.languageid)
    group by a.taskid, b.employeeid
) as t2
on (t1.taskid=t2.taskid and t1.lang_cnt=t2.lang_cnt)
###
here you can insert where statement, like:
where t1.taskid=1 and t2.employeeid=1
if such query returns row - this employee can work with this task, if no rows - no
###
order by t1.taskid, t2.employeeid

as you see, this query creates two temporary tables and then joins them.

first table (t1) calculates how many languages are required for each task

second table (t2) finds all employees who has at least 1 language required for task, groups by task/employee to find how many languages can be taken by this employee

the main query performs LEFT JOIN, as there can be situations when no employees can perform task

here is the output:

task    employee
1       1
1       2
1       3
1       4
2       1
2       4
3       1
3       2
3       4

update: simpler, but less correct variant, because it will not return tasks without possible employees

select a.taskid, b.employeeid, count(distinct b.languageid) as lang_cnt
from
task as a
inner join
employee as b
on (a.LangaugeRequired=1 and a.languageid=b.languageid)
group by a.taskid, b.employeeid
having count(distinct b.languageid) = (select count(distinct c.languageid) from task as c where c.LangaugeRequired=1 and c.taskid=a.taskid)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

sql query to select matching rows for all or nothing criteria

来自分类Dev

MySQL SELECT * FROM table1,table2,table3

来自分类Dev

仅显示table1中与table2不同的字段(SQL Server 2008)

来自分类Dev

Select ONE random row from Table1 where id does not exist in Table2

来自分类Dev

SQL Server:基于table1中的值在特定table2列中返回值

来自分类Dev

SQL - 从 table2 更新 table1 值

来自分类Dev

使用批处理插入时如何识别错误行(INSERT INTO table2 SELECT * FROM table1)

来自分类Dev

使用批处理插入时如何识别错误行(INSERT INTO table2 SELECT * FROM table1)

来自分类Dev

SQL Server-将Table1加入Table2 ON子串(T1.Field)= T2.ID

来自分类Dev

SQL Server: Insert Multiple Rows to a table based on a column in a different table

来自分类Dev

Codeigniter query updating all rows instead of 1

来自分类Dev

SQL table1 row_id不显示table2 row_id

来自分类Dev

“With”命令比使用临时表慢 Select * into #table1 from

来自分类Dev

Issue with Deleting rows from my second Table in my page

来自分类Dev

Remove rows conditionally from a data.table in R

来自分类Dev

How to dynamically count rows in a table from two another tables?

来自分类Dev

如果table2包含更多内容,则Oracle SQL联合table1与table2的结果为空

来自分类Dev

MySQL过程的ALL ROWS参数

来自分类Dev

SQL Server - SELECT all values in table without having them in the GROUP BY

来自分类Dev

检查Table1中的字段组合是否在另一个Table2(SQL)中存在

来自分类Dev

Oracle SQL-从table1查找不存在table2条件的记录

来自分类Dev

Alasql:UNION(或UNION ALL)仅给出来自table1的结果

来自分类Dev

SQL Table Rows to Columns - Possible PIVOT?

来自分类Dev

SQL Server:查询以获取表 2 的 Col2 中条件的 Table1 的 Col1 中的值的总和

来自分类Dev

Entity Framework 6 Database First Approach Not Creating All Tables From SQL Server

来自分类Dev

Get all subitems (children, grandchildren) and parents from a self referencing table

来自分类Dev

How to copy all hive table from one Database to other Database

来自分类Dev

extract div part from table preg_match_all

来自分类Dev

Update all mysql records from one table into another

Related 相关文章

  1. 1

    sql query to select matching rows for all or nothing criteria

  2. 2

    MySQL SELECT * FROM table1,table2,table3

  3. 3

    仅显示table1中与table2不同的字段(SQL Server 2008)

  4. 4

    Select ONE random row from Table1 where id does not exist in Table2

  5. 5

    SQL Server:基于table1中的值在特定table2列中返回值

  6. 6

    SQL - 从 table2 更新 table1 值

  7. 7

    使用批处理插入时如何识别错误行(INSERT INTO table2 SELECT * FROM table1)

  8. 8

    使用批处理插入时如何识别错误行(INSERT INTO table2 SELECT * FROM table1)

  9. 9

    SQL Server-将Table1加入Table2 ON子串(T1.Field)= T2.ID

  10. 10

    SQL Server: Insert Multiple Rows to a table based on a column in a different table

  11. 11

    Codeigniter query updating all rows instead of 1

  12. 12

    SQL table1 row_id不显示table2 row_id

  13. 13

    “With”命令比使用临时表慢 Select * into #table1 from

  14. 14

    Issue with Deleting rows from my second Table in my page

  15. 15

    Remove rows conditionally from a data.table in R

  16. 16

    How to dynamically count rows in a table from two another tables?

  17. 17

    如果table2包含更多内容,则Oracle SQL联合table1与table2的结果为空

  18. 18

    MySQL过程的ALL ROWS参数

  19. 19

    SQL Server - SELECT all values in table without having them in the GROUP BY

  20. 20

    检查Table1中的字段组合是否在另一个Table2(SQL)中存在

  21. 21

    Oracle SQL-从table1查找不存在table2条件的记录

  22. 22

    Alasql:UNION(或UNION ALL)仅给出来自table1的结果

  23. 23

    SQL Table Rows to Columns - Possible PIVOT?

  24. 24

    SQL Server:查询以获取表 2 的 Col2 中条件的 Table1 的 Col1 中的值的总和

  25. 25

    Entity Framework 6 Database First Approach Not Creating All Tables From SQL Server

  26. 26

    Get all subitems (children, grandchildren) and parents from a self referencing table

  27. 27

    How to copy all hive table from one Database to other Database

  28. 28

    extract div part from table preg_match_all

  29. 29

    Update all mysql records from one table into another

热门标签

归档