我有这个(匿名)观点:
CREATE view <View Name>
AS
WITH <common_table_expression 1> AS (
SELECT DISTINCT
Column1 = LEFT(Column2, 36),
Column3,
Column4
FROM [Shared Server].Database.dbo.Table
WHERE Column5 in (99, 100, 101, 102, 107)
AND (Column3 > DATEADD(YEAR, -1, GETDATE())
OR Column4 = 'Active')
),
<common_table_expression2> AS (
SELECT
Column6 = COALESCE(columna, columnb, columnc, columnd, columne, columnf, columng),
Column7 = COALESCE(columnh, columni, columnj, columnk, columnl, columnm, columnn),
Column8,
r.Column9,
r.Column10,
p.Column3,
p.Column4,
Column11 = coalesce (columno, columnp),
Column12 = coalesce(columnq, cef.columnr, ef.columns, ic.columnt, ngs.columnu, oef.columnv, sfc.columnw)
FROM Table1 r
INNER JOIN Table2 p ON r.Column13 = p.Column14
LEFT JOIN Table3 q ON r.Column15 = q.Column16
left join Table4 a on a.Column17 = Column18
left join Table5 cef on cef.Column19 = Column20
left join Table6 ef on ef.Column21 = Column22
left join Table7 ic on ic.Column23 = Column24
left join Table8 ngs on ngs.Column25 = Column26
left join Table9 oef on oef.Column27 = Column28
left join Table10 sfc on sfc.Column29 = Column30
WHERE q.Column31 IS NULL
OR q.Column31 = 'BA6B87E5-B3DF-E111-B758-00155D650A0F'
),
<common_table_expression3> AS (
SELECT Column6, today = COUNT(0)
FROM <common_table_expression2>
WHERE Column3 BETWEEN DATEADD(DAY, -1, GETDATE()) and GETDATE()
AND Column4 = 'Inactive'
GROUP BY Column6
),
<common_table_expression4> AS (
SELECT
r.Column12,
r.Column6,
r.Column7,
Column32 = MAX(today),
Column33 = SUM(CASE WHEN Column9 = 1 THEN 1 ELSE 0 END),
Column34 = SUM(CASE WHEN Column9 = 2 THEN 1 ELSE 0 END),
Column11 = MAX(Column11),
Column3 = MAX(Column3)
FROM <common_table_expression2> r
INNER JOIN <common_table_expression3> d on r.Column6 = d.Column6
GROUP BY r.Column6, r.Column7, r.Column12
)
SELECT
Column12,
Column36 = 'uniquecode',
Column37 = 1,
Column11 = CONVERT(Date, Column11),
Column3
FROM <common_table_expression4>
UNION ALL
SELECT
Column12,
Column36 = 'uniquecode2',
Column37 = CASE active WHEN 0 THEN 1 ELSE ROUND((CAST(inactive AS DECIMAL)/CAST(active + resigned AS DECIMAL)), 2) END,
Column11 = CONVERT(Date, Column11),
Column3
FROM <common_table_expression4>
GO
然而,运行需要44分钟,请有人帮助我!它正在寻找一个似乎是问题的共享服务器,但我不确定如何以不同的方式编写它以使其更快。我想知道tabled函数是否可行?
预计执行计划声明:“查询1:查询成本(相对于批次):50%USE [Database1]
USE DATABASE费用:0%
查询2:查询成本(相对于批次):50% / ******对象:查看[dbo]。[查看名称]脚本日期:07/21/2015 10:57:42 ****** / SET ANSI_NULLS ON
SET ON / OFF成本:0%
对于我糟糕的解释道歉,如果需要任何其他信息,请告诉我。
谢谢!