【发布时间】:2016-08-18 12:07:42
【问题描述】:
我有两个视图,其中数据表示如下:
查看 1
| username | function | level | location |
|:-----------|------------:|:------------:| :-------------:|
| John | Operation | Country | United Kingdom |
| John | Operation | Area | South West |
| John | Operation | Shop | 0001 |
查看 2
| shop | region | country | area |
|:-----------|------------:|:---------------:| :-------------:|
| 1200 | u1 | United Kingdom | West |
| 1201 | u2 | United Kingdom | West |
| 1000 | f1 | France | South West |
| 1100 | i1 | Italy | South West |
| 1111 | s1 | Spain | South West |
| 1112 | n2 | Norway | South West |
| 0001 | o1 | Japan | Asia |
两个VIEW之间的关系是每个AREA有多个Country,每个Country有多个REGION,每个REGION有多个店铺。
尝试:生成一个视图,根据顶部表格上的 LEVEL 选择,从顶部表格数据中为每个用户生成所有行将从底部表格生成。所以在这种情况下 VIEW 应该显示
预期结果:所以在这种情况下,视图应该显示 AREA South West 下的所有 SHOPS、REGIONS、COUNTRIES,COUNTRY United Kingdom 和 SHOP 0001 下的所有 SHOPS 和 REGIONS。
| username | function | level | location |
|:-----------|------------:|:---------------:| :-------------:|
| John | Operation | shop | 0001 |
| John | Operation | shop | 1001 |
| John | Operation | shop | ... |
| John | Operation | Country | United Kingdom|
| John | Operation | Country | ... |
| John | Operation | Country | ... |
| John | Operation | Region | ... |
| John | Operation | Region | ... |
| John | Operation | Region | ... |
| John | Operation | Area | South West |
| John | Operation | Area | ... |
| John | Operation | Area | ... |
不确定如何使用 CASE 语句完成此操作。任何帮助,将不胜感激。
【问题讨论】:
-
您应该至少完成了预期输出中的
location列。您应该首先弄清楚如何将两个视图连接在一起;我看不到这样做的简单方法。 -
我有点疑惑。 In View 1 John 在 Country United Kingdom、Area South West 和 Shop 0001。 In View 2 Shop 0001 在日本,位于亚洲地区。英国在西部地区,而西南地区包括其他欧洲国家?
-
@JonathanWillcock 你是对的。可以访问 0001 商店意味着在结果中应该有 John 的一行,其中 Level 为 SHOP,位置为 0001。 VIEW 1 表示 John 可以访问 UK 下的所有商店、“SOUTH WEST 区域”下的所有国家、地区和商店" 以及恰好位于 COUNTRY japan、AREA asia 和 region o1 下的 SHOP 0001。这里AREA west下有其他国家,COUNTRY下有多个REGIONS,REGIONS下有SHOPS。
标签: sql-server sql-server-2008 join case