我正在尝试使用视图创建一个输出,该视图将来自查询2的引号与来自查询1的引号和来自查询3的策略与来自查询1的策略相结合。查询1是我的主要查询... 2和3将保持联系。
查询1:
SELECT
A.AgencyId,
SUM(A.Quotes) AS Quotes,
SUM(A.NB) AS Policies,
CONVERT(DATE, A.Date) AS Date,
C.WeekEndDate
FROM
dbo.Agent_Qts_Daily A
INNER JOIN
dbo.AgentList B ON A.AgencyID = B.AgencyID
LEFT JOIN
dbo.WE_DATES C ON A.Date = C.Date
WHERE
Year = '2016'
GROUP BY
A.AgencyID, A.Date, C.WeekEndDate
查询2:
SELECT
B.AgencyID,
SUM(A.Quote_Cnt) AS Quotes,
CONVERT(DATE, A.DateRecd) AS Date,
C.WeekEndDate
FROM
dbo.TestData A
INNER JOIN
dbo.AgentList B ON A.AgtID = B.AGTID
LEFT JOIN
dbo.WE_DATES C ON A.DateRecd = C.Date
WHERE
A.FirmGroup <> 'ABC'
AND A.Source = 'DEF'
AND C.Year = '2016'
GROUP BY
B.AgencyID, A.DateRecd, C.WeekEndDate
查询3:
SELECT
B.AgencyID,
SUM(A.sold_Cnt) AS Policies,
CONVERT(DATE, A.DateBound) AS Date,
C.WeekEndDate
FROM
dbo.TestData A
INNER JOIN
dbo.AgentList B ON A.AgtID = B.AGTID
LEFT JOIN
dbo.WE_DATES C ON A.DateBound = C.Date
WHERE
A.FirmGroup <> 'ABC'
AND A.sold_Cnt = '1'
AND A.Source = 'DEF'
AND C.Year = '2016'
GROUP BY
B.AgencyID, A.DateBound, C.WeekEndDate
答案 0 :(得分:0)
你可以做这样的事情,或者你也可以使用sp来达到相同的标准
CREATE VIEW ViewName
AS
SELECT
A.AgencyId,
SUM(A.Quotes) AS Quotes,
SUM(A.NB) AS Policies,
CONVERT(DATE, A.Date) AS Date,
C.WeekEndDate
FROM
dbo.Agent_Qts_Daily A
INNER JOIN dbo.AgentList B ON A.AgencyID = B.AgencyID
LEFT JOIN dbo.WE_DATES C ON A.Date = C.Date
WHERE Year = '2016'
GROUP BY
A.AgencyID,
A.Date,
C.WeekEndDate
UNION ALL
SELECT
B.AgencyID,
SUM(A.Quote_Cnt) AS Quotes,
0 AS Policies,
CONVERT(DATE, A.DateRecd) AS Date,
C.WeekEndDate
FROM
dbo.TestData A
INNER JOIN dbo.AgentList B ON A.AgtID = B.AGTID
LEFT JOIN dbo.WE_DATES C ON A.DateRecd = C.Date
WHERE
A.FirmGroup <> 'ABC'
AND A.Source = 'DEF'
AND C.Year = '2016'
GROUP BY
B.AgencyID,
A.DateRecd,
C.WeekEndDate
UNION ALL
SELECT
B.AgencyID,
0 AS Quotes,
SUM(A.sold_Cnt) AS Policies,
CONVERT(DATE, A.DateBound) AS Date,
C.WeekEndDate
FROM
dbo.TestData A
INNER JOIN dbo.AgentList B ON A.AgtID = B.AGTID
LEFT JOIN dbo.WE_DATES C ON A.DateBound = C.Date
WHERE
A.FirmGroup <> 'ABC'
AND A.sold_Cnt = '1'
AND A.Source = 'DEF'
AND C.Year = '2016'
GROUP BY
B.AgencyID,
A.DateBound,
C.WeekEndDate