我有一个复杂的SQL查询,它返回一系列分析过程的计数值。类别 889'进一步拆分为由两个表的连接确定的子类别。查询如下:
DECLARE @tbl AS TABLE (
ordnum INT,
fldrinit VARCHAR(8),
giscode VARCHAR(15),
day INT null)
DECLARE @curmonth int
SET @curmonth = 6
INSERT INTO @tbl
select o.ordnum, o.FldrInit, a.GisCode, @curmonth
from tod..CurrentOrders as o
left join
tod..addr as a
on o.RecNumAddr = a.RecNumAddr
inner join
tod..CustInfo as c
on o.CustNum = c.CustNum
where datepart(year, o.OrdDte) = '2014'
and datepart(month, o.OrdDte) = '6'
and o.OrdStat = 'c'
and o.CustNum not in ('1000045506', '1000126887', '1000132094', '1000149661', '1000163278')
and o.OrdType not like '%[ABCEGIMNQSUY]%'
and o.OrdType not like 'H%'
and o.FldrInit not in ('892', '555', '909', '988', '989', '996', '895', '1882', '1885', '1886', '1889', '1898', '1899', '1900', '1901', '1902', 'ADD')
and c.CustNme not like '%TEST%'
SELECT
(SELECT count(*) as [883] from @tbl where fldrinit = '883') as [883],
(SELECT count(*) as [885] from @tbl where fldrinit = '885') as [885],
(SELECT count(*) as [886] from @tbl where fldrinit = '886') as [886],
(SELECT count(*) as [887] from @tbl where fldrinit = '887') as [887],
(SELECT count(*) as [889 Total] from @tbl where fldrinit = '889') as [889 Total],
(SELECT count(*) as [889 G1 Pt] from @tbl where fldrinit = '889'
AND giscode like 'hap%'
or giscode like 'sap%') as [889 G1 Pt],
(SELECT count(*) as [889 G1 Road] from @tbl where fldrinit = '889'
AND giscode like 'has%'
or giscode like 'sas%') as [889 G1 Road],
(SELECT count(*) as [889 Parcelstream Pt] from @tbl where fldrinit = '889'
and giscode like 'A????') as [889 Parcelstream Pt],
(SELECT count(*) as [889 Parcelstream Rd] from @tbl where fldrinit = '889'
and giscode like 'R????') as [889 Parcelstream Rd],
(SELECT count(*) as [900] from @tbl where fldrinit = '900') as [900],
(SELECT count(*) as [902] from @tbl where fldrinit = '902') as [902],
(SELECT count(*) as [Manual] from @tbl where fldrinit not in ('883', '885', '886', '887', '889', '900', '902')) as [Manual],
(SELECT count(*) as [Grand Total] from @tbl) as [Grand Total];
首先,查询构建器不接受" DECLARE"声明。我可以使用查询的第一部分(不使用DECLARE或SELECT COUNT(*))为每个进程在矩阵内构建单独的表达式(不包括DECLARE或SELECT COUNT(*),但我需要889的所有内容。任何提示?这个软件都是新的。