Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 我需要一个包含列、CatId和标志的视图。如果CatID存在任何子类别,则标志将为1_Sql_Sql Server_Select - Fatal编程技术网

Sql 我需要一个包含列、CatId和标志的视图。如果CatID存在任何子类别,则标志将为1

Sql 我需要一个包含列、CatId和标志的视图。如果CatID存在任何子类别,则标志将为1,sql,sql-server,select,Sql,Sql Server,Select,有两个表:Category和CatID | CatName | | CatID | CategoryName | |---|-------|--------------------| | 1 | 1021 | Home | | 2 | 1022 | Corporate | | 3 | 1023 | Products | | 4 | 1024 | Gardens | | 5 | 1025

有两个表:
Category
CatID | CatName

|   | CatID | CategoryName       |
|---|-------|--------------------|
| 1 | 1021  | Home               |
| 2 | 1022  | Corporate          |
| 3 | 1023  | Products           |
| 4 | 1024  | Gardens            |
| 5 | 1025  | Investor Relations |
| 6 | 1026  | News & Events      |
| 7 | 1027  | Contact Us         |
SubCategory
SubID|CatID

|    | SubID | CatID |
|----|-------|-------|
|  1 |    9  |  1025 |
|  2 |    5  |  1022 |
|  3 |    6  |  1022 |
|  4 |   10  |  1025 |
|  5 |   11  |  1025 |
|  6 |   12  |  1025 |
|  7 |   13  |  1025 |
|  8 |   14  |  1025 |
|  9 |   15  |  1025 |
| 10 |   16  |  1026 |
| 11 |   17  |  1026 |
| 12 |    7  |  1022 |
| 13 |    8  |  1022 |
| 14 |   18  |  1023 |

我想得到一个视图,其中有两列
view
CatID | Flag
,其中
0
如果没有该
CatID
的子类别,则
1
我会计算子类别,然后左键连接:

CREATE VIEW my_view AS
SELECT c.CatId, CASE WHEN cnt IS NOT NULL THEN 1 ELSE 0 END AS Flag
FROM   Categoery c
LEFT JOIN (SELECT   CatId, COUNT(*) AS cnt
           FROM     SubCategory
           GROUP BY CatId) s ON c.CatId = s.CatId

这是MySQL还是SQL Server?到目前为止您也尝试了什么?我删除了相互矛盾的MySQL和MS SQL Server标记-请仅重新添加相关的标记。SQL Server问题