sql - tricky WHERE clause constraint; filter by one fields value but not omit other possible values
问题描述
I have dimension tables Person, Position, Department and a fact table Job. Job marries a position to a person. A person can have more than one job and in more than one department. Need to provide user ability to filter by department – however, if I simply constraint results WHERE dept_id IN (‘MATH’), then I’m not representing an accurate picture; forfeiting an employee’s possible other jobs in other departments. I need to the ability to filter by one department and still surface records of all positions in all departments. Wondering if I should create a bridge table. I will be reporting from the Data Warehouse and SSAS Tabular model so im trying to work the DAX as well as the SQL
解决方案
您可以使用相关子查询来选择该部门中的所有人员,而不限制返回的实际部门:
...
WHERE person_id IN(
SELECT DISTINCT person_id
FROM Person p
JOIN Department d
ON p.department_id = d.department_id
WHERE d.dept_id = 'MATH'
)
推荐阅读
- c++ - c++ getline函数和后台进程
- vba - 自动按下网页上的“按钮”
- wordpress - GTMetrix 忽略 Jetpack CDN
- sql - ROWID 是在 Teradata 中选择-卸载-更新行的安全方法吗?这是最快的方法吗?
- c - 如何使用 openSSL api 将公钥转换为字符串?
- mysql - 如何使用本机同步函数从 Amazon Aurora MySQL 数据库集群调用 Lambda 函数
- mysql - 我被mysql中的更新困住了
- node.js - 无论我问什么,我的 Alexa 意图都没有定义插槽
- html - 可以在 flex 显示中将文本居中对齐
- mysql - 我的 SQL 使用具有相同 id 的数据创建一行