我有这个存储过程:
SELECT
s.Id AS IdCategory,
s.Description AS Category,
COUNT(s.Description) AS TotalYEAR
INTO
#AuditYear
FROM
[dbo].[Audit] aa (NOLOCK)
INNER JOIN
[dbo].[AuditActOrCondition] a (NOLOCK) ON aa.Id = a.IdAudit
INNER JOIN
[dbo].[ActOrCondition] ac (NOLOCK) ON a.IdActCondition = ac.Id
INNER JOIN
[dbo].[ACType] t (NOLOCK) ON ac.Type = t.Id
INNER JOIN
[dbo].[ACSubType] s (NOLOCK) ON ac.Subtype = s.Id
WHERE
YEAR(aa.CreateDate) = (SELECT YEAR
FROM [dbo].[KPIS] k1 (NOLOCK)
WHERE k1.Id = @Id)
GROUP BY
s.Id, s.Description
SELECT
s.Id AS IdCategory,
s.Description AS Category,
COUNT(s.Description) AS TotalMonth
INTO
#AuditMonth
FROM
[dbo].[Audit] aa (NOLOCK)
INNER JOIN
[dbo].[AuditActOrCondition] a (NOLOCK) ON aa.Id = a.IdAudit
INNER JOIN
[dbo].[ActOrCondition] ac (NOLOCK) ON a.IdActCondition = ac.Id
INNER JOIN
[dbo].[ACType] t (NOLOCK) ON ac.Type = t.Id
INNER JOIN
[dbo].[ACSubType] s (NOLOCK) ON ac.Subtype = s.Id
WHERE
MONTH(aa.CreateDate) = (SELECT MONTH
FROM [dbo].[KPIS] k1 (NOLOCK)
WHERE k1.Id = @Id)
GROUP BY
s.Id, s.Description
SELECT
y.IdCategory, y.Category,
ISNULL(m.TotalMonth,0) AS TotalMonth,
y.TotalYEAR
FROM
#AuditYear y
LEFT JOIN
#AuditMonth m ON y.IdCategory = m.IdCategory
我想添加一个名为BranchOfficeId
的列,所以我做INNER JOIN
之类的:
SELECT
s.Id AS IdCategory,
s.Description AS Category,
COUNT(s.Description) AS TotalYEAR,
b.BranchOfficeId
INTO
#AuditYear
FROM
[dbo].[Audit] aa (NOLOCK)
INNER JOIN
[dbo].[Department] AS d (NOLOCK) ON aa.IdDepartment = d.Id
INNER JOIN
[dbo].[BranchOffice] AS b (NOLOCK) ON b.BranchOfficeId = d.BranchOfficeId
INNER JOIN
[dbo].[AuditActOrCondition] a (NOLOCK) ON aa.Id = a.IdAudit
INNER JOIN
[dbo].[ActOrCondition] ac (NOLOCK) ON a.IdActCondition = ac.Id
INNER JOIN
[dbo].[ACType] t (NOLOCK) ON ac.Type = t.Id
INNER JOIN
[dbo].[ACSubType] s (NOLOCK) ON ac.Subtype = s.Id
WHERE
YEAR(aa.CreateDate) = (SELECT YEAR
FROM [dbo].[KPIS] k1 (NOLOCK)
WHERE k1.Id = @Id)
GROUP BY
s.Id, s.Description, b.BranchOfficeId
SELECT
s.Id AS IdCategory,
s.Description AS Category,
COUNT(s.Description) AS TotalMonth
INTO
#AuditMonth
FROM
[dbo].[Audit] aa (NOLOCK)
INNER JOIN
[dbo].[AuditActOrCondition] a (NOLOCK) ON aa.Id = a.IdAudit
INNER JOIN
[dbo].[ActOrCondition] ac (NOLOCK) ON a.IdActCondition = ac.Id
INNER JOIN
[dbo].[ACType] t (NOLOCK) ON ac.Type = t.Id
INNER JOIN
[dbo].[ACSubType] s (NOLOCK) ON ac.Subtype = s.Id
WHERE
MONTH(aa.CreateDate) = (SELECT MONTH
FROM [dbo].[KPIS] k1 (NOLOCK)
WHERE k1.Id = @Id)
GROUP BY
s.Id, s.Description
SELECT
y.IdCategory, y.Category,
ISNULL(m.TotalMonth,0) AS TotalMonth,
y.TotalYEAR
FROM
#AuditYear y
LEFT JOIN
#AuditMonth m ON y.IdCategory = m.IdCategory
但是当我更新我的复杂类型时,它只获得列:
public int IdCategory { get; set; }
public string Category { get; set; }
public int TotalMonth { get; set; }
public Nullable<int> TotalYEAR { get; set; }
任何人都可以解释为什么我无法获得BranchOfficeId
价值吗?此致
答案 0 :(得分:0)
SELECT y.IdCategory
,y.Category
,ISNULL(m.TotalMonth,0) AS TotalMonth
,y.TotalYEAR
FROM #AuditYear y
LEFT JOIN #AuditMonth m
ON y.IdCategory = m.IdCategory
#AuditYear是您第一次选择的结果,那么,您只需要在最后一次选择中使用BranchOfficeId列:
SELECT y.IdCategory
,y.Category
,ISNULL(m.TotalMonth,0) AS TotalMonth
,y.TotalYEAR, y.BranchOfficeId
FROM #AuditYear y
LEFT JOIN #AuditMonth m
ON y.IdCategory = m.IdCategory