我需要一些帮助来总结两个(或更多)别名列。 我知道我需要一个派生表来完成它,但到目前为止,我迷失了在线教程和文档,因为他们的例子太简单了。他们只有一张桌子,两列等。
这可能是我最好的选择: 我需要计算两个别名列的总和:' InFxO'和' OnTxO'我的代码如下:
ALTER PROC [dbo].[DIFOTIS]
@Mode as Varchar (5)
AS
Begin
Declare
@StartDate date,
@EndDate date
SET @StartDate=
CASE @Mode
WHEN 'MTD' THEN DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)
WHEN 'YTD' THEN DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)
WHEN 'QTD' THEN DATEADD(qq,DATEDIFF(qq,0,GETDATE()),0)
WHEN 'WTD' THEN DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0)
END
Set @EndDate=
CASE @Mode
WHEN 'MTD' THEN DATEADD(mm,DATEDIFF(mm,0,GETDATE())+1,0)
WHEN 'YTD' THEN DATEADD(yy,DATEDIFF(yy,0,GETDATE())+1,0)
WHEN 'QTD' THEN DATEADD(qq,DATEDIFF(qq,0,GETDATE())+1,0)
WHEN 'WTD' THEN DATEADD(wk,DATEDIFF(wk,0,GETDATE())+1,0)
END
Select DATEPART(ISO_WEEK,d.DateOpn) AS 'Week#'
,Clients.CustCateg, Clients.ClntGroup
,d.DocumentCode as 'CORD_DocumentCode'
,CDSPDocs.DocumentCode AS 'DESP_DocumentCode'
,Count(CORDLines.Qnty) AS 'Cord_Lines'
,SUM(CORDLines.Qnty) AS 'CORD_Qty'
,Count(CDSPLines.Qnty) AS 'DESP_Lines'
,Sum(CDSPLines.Qnty) AS 'DESP_Qty'
,CDSPLines.Status, d.Status as d_status
,d.OpenDate, d.DateDue
,CDSPDocs.PostDate AS 'DESP_PostedDate'
,d.DocType, DATEDIFF(day, d.OpenDate, d.DateDue) AS 'Lead times'
--in-full
,CASE WHEN SUM(CORDLines.Qnty) = Sum(CDSPLines.Qnty) THEN '1' ELSE '0' END as InFxO
--On-Time by order according to Despatch SLAs
,CASE WHEN (Clients.ClntGroup IN ('Local Market','Local Market - Pharm','Web Sales - Local','Web Sales - Export', 'Mail Order','Mail Order - Export')) AND (Datediff(day, d.OpenDate, CDSPDocs.PostDate) - (Datediff(Week, d.OpenDate, CDSPDocs.PostDate)*2) <= 2) then '1'
WHEN (Clients.ClntGroup = 'Export Market') AND (Datediff(day, d.OpenDate, CDSPDocs.PostDate) - (Datediff(Week, d.OpenDate, CDSPDocs.PostDate)*2) <= 14) then '1'
WHEN (Clients.ClntGroup = 'Export Market') or Clients.CustCateg = 'UK Transfer' AND (d.DateDue >= CDSPDocs.PostDate) then '1'
ELSE '0'
END as OnTxO
From dbo.Documents AS d INNER JOIN
dbo.Clients ON d.ObjectID = dbo.Clients.ClntID AND Clients.ClientName <> 'Samples - Free / Give-aways' LEFT Outer JOIN
dbo.DocumentsLines AS CORDLines ON d.DocID = CORDLines.DocID AND CORDLines.TrnType = 'L'
LEFT OUTER JOIN
dbo.DocumentsLines AS CDSPLines ON CORDLines.TranID = CDSPLines.SourceID AND CDSPLines.TrnType = 'L' AND (CDSPLines.Status = 'Posted' OR CDSPLines.Status = 'Closed') LEFT OUTER JOIN
dbo.Documents AS CDSPDocs ON CDSPLines.DocID = CDSPDocs.DocID
WHERE (d.DocType IN ('CASW', 'CORD','MORD'))
AND (CORDLines.LneType NOT In ('Fght','MANF','Stor', 'PACK','EXPS'))
AND d.DateOpn >= @StartDate AND d.DateOpn < @EndDate
AND (CORDLines.LneType is not null)
AND (d.DateDue <= Convert(Date, GetDate(), 101))
Group by d.DateOpn
,d.DocumentCode
,Clients.CustCateg
,CDSPDocs.DocumentCode
,d.[Status]
,d.DocType
,d.OpenDate
,d.DateReq
,CDSPDocs.PostDate
,CDSPLines.[Status]
,Clients.ClntGroup
,d.DocumentName
,d.DateDue
,d.DateOpn
ORDER BY d.DateOpn, 'Week#'
END
GO
感谢所有帮助。 谢谢 ħ
答案 0 :(得分:0)
下面是一个将原始查询包装在派生表中的示例,这样您就不需要为SUM重复CASE表达式。您可以类似地将查询包装在公用表表达式中以实现相同的结果。
我建议您仅使用单引号括起字符串文字,并使用方括号或双引号将标识符(列名,别名和对象名称)括起来,如SQL Server联机丛书参考中所述(http://msdn.microsoft.com/en-us/library/ms175874.aspx )。标识符只有在不符合标识符命名规则或是保留关键字时才需要包含在内。
ALTER PROC dbo.DIFOTIS @Mode AS varchar(5)
AS
BEGIN
DECLARE @StartDate date
, @EndDate date;
SET @StartDate = CASE @Mode
WHEN 'MTD'
THEN DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)
WHEN 'YTD'
THEN DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0)
WHEN 'QTD'
THEN DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)
WHEN 'WTD'
THEN DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0)
END;
SET @EndDate = CASE @Mode
WHEN 'MTD'
THEN DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 1, 0)
WHEN 'YTD'
THEN DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, 0)
WHEN 'QTD'
THEN DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 1, 0)
WHEN 'WTD'
THEN DATEADD(wk, DATEDIFF(wk, 0, GETDATE()) + 1, 0)
END;
SELECT Week#
, CustCateg
, ClntGroup
, CORD_DocumentCode
, DESP_DocumentCode
, Cord_Lines
, CORD_Qty
, DESP_Lines
, DESP_Qty
, Status
, d_status
, OpenDate
, DateDue
, DESP_PostedDate
, DocType
, [Lead times]
, InFxO
, OnTxO
, InFxO + OnTxO AS InFullAndOneTime
FROM (
SELECT DATEPART(ISO_WEEK, d.DateOpn) AS Week#
, Clients.CustCateg
, Clients.ClntGroup
, d.DocumentCode AS CORD_DocumentCode
, CDSPDocs.DocumentCode AS DESP_DocumentCode
, COUNT(CORDLines.Qnty) AS Cord_Lines
, SUM(CORDLines.Qnty) AS CORD_Qty
, COUNT(CDSPLines.Qnty) AS DESP_Lines
, SUM(CDSPLines.Qnty) AS DESP_Qty
, CDSPLines.Status
, d.Status AS d_status
, d.OpenDate
, d.DateDue
, CDSPDocs.PostDate AS DESP_PostedDate
, d.DocType
, DATEDIFF(DAY, d.OpenDate, d.DateDue) AS [Lead times]
--in-full
, CASE WHEN SUM(CORDLines.Qnty) = SUM(CDSPLines.Qnty) THEN 1
ELSE 0
END AS InFxO
--On-Time by order according to Despatch SLAs
, CASE WHEN ( Clients.ClntGroup IN ( 'Local Market',
'Local Market - Pharm',
'Web Sales - Local',
'Web Sales - Export',
'Mail Order',
'Mail Order - Export' ) )
AND ( DATEDIFF(DAY, d.OpenDate, CDSPDocs.PostDate)
- ( DATEDIFF(WEEK, d.OpenDate,
CDSPDocs.PostDate) * 2 ) <= 2 )
THEN 1
WHEN ( Clients.ClntGroup = 'Export Market' )
AND ( DATEDIFF(DAY, d.OpenDate, CDSPDocs.PostDate)
- ( DATEDIFF(WEEK, d.OpenDate,
CDSPDocs.PostDate) * 2 ) <= 14 )
THEN 1
WHEN ( Clients.ClntGroup = 'Export Market' )
OR Clients.CustCateg = 'UK Transfer'
AND ( d.DateDue >= CDSPDocs.PostDate ) THEN '1'
ELSE 0
END AS OnTxO
FROM dbo.Documents AS d
INNER JOIN dbo.Clients ON d.ObjectID = dbo.Clients.ClntID
AND Clients.ClientName <> 'Samples - Free / Give-aways'
LEFT OUTER JOIN dbo.DocumentsLines AS CORDLines ON d.DocID = CORDLines.DocID
AND CORDLines.TrnType = 'L'
LEFT OUTER JOIN dbo.DocumentsLines AS CDSPLines ON CORDLines.TranID = CDSPLines.SourceID
AND CDSPLines.TrnType = 'L'
AND ( CDSPLines.Status = 'Posted'
OR CDSPLines.Status = 'Closed'
)
LEFT OUTER JOIN dbo.Documents AS CDSPDocs ON CDSPLines.DocID = CDSPDocs.DocID
WHERE ( d.DocType IN ( 'CASW', 'CORD', 'MORD' ) )
AND ( CORDLines.LneType NOT IN ( 'Fght', 'MANF', 'Stor',
'PACK', 'EXPS' ) )
AND d.DateOpn >= @StartDate
AND d.DateOpn < @EndDate
AND ( CORDLines.LneType IS NOT NULL )
AND ( d.DateDue <= CONVERT(date, GETDATE(), 101) )
GROUP BY d.DateOpn
, d.DocumentCode
, Clients.CustCateg
, CDSPDocs.DocumentCode
, d.Status
, d.DocType
, d.OpenDate
, d.DateReq
, CDSPDocs.PostDate
, CDSPLines.Status
, Clients.ClntGroup
, d.DocumentName
, d.DateDue
, d.DateOpn
) AS derived_table
ORDER BY d.DateOpn
, Week#;
END;
GO