需要查询以确定每个问题的附件数量
Need query to determine number of attachments for each issue
我在 SQL 服务器中有一个数据库,该数据库具有三个表:问题、附件和请求者。我需要一个查询 returns "Issues" 和 "Attachments" 表中包含的所有列。下面列出的是我创建的查询,但它没有按预期工作:
SELECT A.*,
B.*,
SubQuery.attachmentcount
FROM [DB].[dbo].[issues] AS A
FULL OUTER JOIN [DB].[dbo].[requestors] AS B
ON A.issue_id = B.issue_id,
(SELECT Count(attachments.attachment_id) AS AttachmentCount
FROM issues
LEFT OUTER JOIN attachments
ON issues.issue_id = attachments.issue_id
WHERE attachments.attachment_status = 1
GROUP BY issues.issue_id) AS SubQuery;
下面列出了描述三个表的图片:
关于如何修复我的查询有什么想法吗?
谢谢,
"I need a single query that returns all the columns contained in the "问题”和 "Attachments" 表”。
基于这句话试试这个:
SELECT A.Issue_ID, I.Issue_Name,r.Name, COUNT(A.attachment_id) AS Count
FROM Attachments as A
INNER JOIN Issues I on I.issue_id = A.issue_id
INNER JOIN requestors as R on A.issue_id = R.requestor_id
WHERE A.attachment_status = 1
GROUP BY A.Issue_ID, I.Issue_Name, r.Name
--Specify all columns by name (don't use *)
保持简单并尝试这个!
SELECT i.Issue_ID, i.Issue_Name, COUNT(a.attachment_id) AS AttachmentCount
FROM attachments a JOIN
issues i ON
i.issue_id = a.issue_id
WHERE a.attachment_status = 1
GROUP BY i.Issue_ID, i.Issue_Name
在 Select 列表 和 Group By Clause 中添加您的所需的列 ] 你已经 完成了 。
我在 SQL 服务器中有一个数据库,该数据库具有三个表:问题、附件和请求者。我需要一个查询 returns "Issues" 和 "Attachments" 表中包含的所有列。下面列出的是我创建的查询,但它没有按预期工作:
SELECT A.*,
B.*,
SubQuery.attachmentcount
FROM [DB].[dbo].[issues] AS A
FULL OUTER JOIN [DB].[dbo].[requestors] AS B
ON A.issue_id = B.issue_id,
(SELECT Count(attachments.attachment_id) AS AttachmentCount
FROM issues
LEFT OUTER JOIN attachments
ON issues.issue_id = attachments.issue_id
WHERE attachments.attachment_status = 1
GROUP BY issues.issue_id) AS SubQuery;
下面列出了描述三个表的图片:
关于如何修复我的查询有什么想法吗?
谢谢,
"I need a single query that returns all the columns contained in the "问题”和 "Attachments" 表”。 基于这句话试试这个:
SELECT A.Issue_ID, I.Issue_Name,r.Name, COUNT(A.attachment_id) AS Count
FROM Attachments as A
INNER JOIN Issues I on I.issue_id = A.issue_id
INNER JOIN requestors as R on A.issue_id = R.requestor_id
WHERE A.attachment_status = 1
GROUP BY A.Issue_ID, I.Issue_Name, r.Name
--Specify all columns by name (don't use *)
保持简单并尝试这个!
SELECT i.Issue_ID, i.Issue_Name, COUNT(a.attachment_id) AS AttachmentCount
FROM attachments a JOIN
issues i ON
i.issue_id = a.issue_id
WHERE a.attachment_status = 1
GROUP BY i.Issue_ID, i.Issue_Name
在 Select 列表 和 Group By Clause 中添加您的所需的列 ] 你已经 完成了 。