0

我正在尝试将我之前定义的两列中的值相加。我想知道是否有办法在查询中执行此操作。

下面是我的代码,我正在尝试将命名列 SP 和 IP 的值相加:

DECLARE    @Flag nvarchar(10),
           @Month int,
           @Year int,
           @ViewName as nvarchar(50), 
           @PrePlumb as int

SET @Flag = 'Grid'
SET @Month = 01 
SET @Year = 2013 
SET @ViewName = 'Delivered'

SELECT     PID, 
           [Customer Name], 
           [Project Name], 
           Delivered1, 
           CAST(SUM(Production) AS decimal(10, 2)) AS Value, 
           CAST(SUM(SellingPrice) AS decimal(10, 2)) AS SP, 
           CAST(SUM(IP) AS decimal(10, 2)) AS IP, 
           CAST(SUM(Plumb) AS Decimal(10, 2)) As Plumb
FROM       (
               SELECT     SpecificationHeader.Qno, 
                          SpecificationHeader.PID, 
                          Customers3.[Customer Name], 
                          Projects2.[Project Name], 
                          MAX(SpecDetailsExt.Delivered) AS Delivered1,
                          MAX(SpecDetails.SellingPrice) AS SellingPrice,
                          MAX(SpecDetails.FactoryRate) AS Production, 
                          (CASE 
                               WHEN MAX(SpecificationHeader.[Installation Costs]) = 'Yes' 
                               THEN MAX(specdetails.InstallationCostPlusMargin) ELSE 0 
                          END) AS IP, 
                          (CASE 
                               WHEN MAX(SpecificationHeader.[Plumbing Costs]) = 'Yes' 
                               THEN MAX(specdetails.PlumbCostPlusMargin) ELSE 0 
                          END) AS Plumb, 
                          MAX(CONVERT(date, SpecDetailsExt.Delivered, 3)) AS Delivered
       FROM       SpecDetailsExt 
                          INNER JOIN SpecDetails 
                              ON SpecDetailsExt.SrecId = SpecDetails.SrecId 
                          INNER JOIN SpecificationHeader 
                              ON SpecDetails.QuoteNo = SpecificationHeader.Qno 
                          INNER JOIN Projects2 
                              ON SpecificationHeader.PID = Projects2.PID 
                          INNER JOIN Customers3 
                              ON Projects2.[Customer Number] = Customers3.[Customer Number]
       WHERE      (SpecificationHeader.MoveToOrder <> '') 
                          AND (SpecDetailsExt.Delivered <> N'') 
                          AND (DATEPART(MONTH, CONVERT(date, SpecDetailsExt.Delivered , 3)) = @Month)
                          AND (YEAR(CONVERT(date, SpecDetailsExt.Delivered , 3)) = @Year)
               GROUP BY   SpecificationHeader.Qno, 
                          Customers3.[Customer Name],  
                          Projects2.[Project Name], 
                          SpecificationHeader.PID, 
                          SpecDetailsExt.SrecId
    ) AS x
    GROUP BY PID, 
            [Customer Name], 
            [Project Name], 
            Delivered1
    ORDER BY Delivered1 desc, 
            [Customer Name], 
            [Project Name]
4

1 回答 1

0

使用 CTE:

DECLARE    @Flag nvarchar(10),
           @Month int,
           @Year int,
           @ViewName as nvarchar(50), 
           @PrePlumb as int

SET @Flag = 'Grid'
SET @Month = 01 
SET @Year = 2013 
SET @ViewName = 'Delivered'

;WITH MyCTE AS
(
SELECT     PID, 
           [Customer Name], 
           [Project Name], 
           Delivered1, 
           CAST(SUM(Production) AS decimal(10, 2)) AS Value, 
           CAST(SUM(SellingPrice) AS decimal(10, 2)) AS SP, 
           CAST(SUM(IP) AS decimal(10, 2)) AS IP, 
           CAST(SUM(Plumb) AS Decimal(10, 2)) As Plumb
FROM       (
               SELECT     SpecificationHeader.Qno, 
                          SpecificationHeader.PID, 
                          Customers3.[Customer Name], 
                          Projects2.[Project Name], 
                          MAX(SpecDetailsExt.Delivered) AS Delivered1,
                          MAX(SpecDetails.SellingPrice) AS SellingPrice,
                          MAX(SpecDetails.FactoryRate) AS Production, 
                          (CASE 
                               WHEN MAX(SpecificationHeader.[Installation Costs]) = 'Yes' 
                               THEN MAX(specdetails.InstallationCostPlusMargin) ELSE 0 
                          END) AS IP, 
                          (CASE 
                               WHEN MAX(SpecificationHeader.[Plumbing Costs]) = 'Yes' 
                               THEN MAX(specdetails.PlumbCostPlusMargin) ELSE 0 
                          END) AS Plumb, 
                          MAX(CONVERT(date, SpecDetailsExt.Delivered, 3)) AS Delivered
       FROM       SpecDetailsExt 
                          INNER JOIN SpecDetails 
                              ON SpecDetailsExt.SrecId = SpecDetails.SrecId 
                          INNER JOIN SpecificationHeader 
                              ON SpecDetails.QuoteNo = SpecificationHeader.Qno 
                          INNER JOIN Projects2 
                              ON SpecificationHeader.PID = Projects2.PID 
                          INNER JOIN Customers3 
                              ON Projects2.[Customer Number] = Customers3.[Customer Number]
       WHERE      (SpecificationHeader.MoveToOrder <> '') 
                          AND (SpecDetailsExt.Delivered <> N'') 
                          AND (DATEPART(MONTH, CONVERT(date, SpecDetailsExt.Delivered , 3)) = @Month)
                          AND (YEAR(CONVERT(date, SpecDetailsExt.Delivered , 3)) = @Year)
               GROUP BY   SpecificationHeader.Qno, 
                          Customers3.[Customer Name],  
                          Projects2.[Project Name], 
                          SpecificationHeader.PID, 
                          SpecDetailsExt.SrecId
    ) AS x
    GROUP BY PID, 
            [Customer Name], 
            [Project Name], 
            Delivered1
    ORDER BY Delivered1 desc, 
            [Customer Name], 
            [Project Name]
)

SELECT     PID, 
           [Customer Name], 
           [Project Name], 
           Delivered1, 
           Value, 
           SP, 
           IP, 
           Plumb,
           SP + IP AS Combined
FROM       MyCTE
于 2013-08-13T08:51:53.487 回答