如何计算同一行中的天总数和月总数

时间:2018-11-15 06:19:17

标签: sql sql-server tsql

我需要获取零用钱的当天总计(我已经获取了此信息),并且需要今天与帐户代码相关的每月总计。接下来,我将显示每日总计的SQL。有人请帮我计算同一行中的每月总计。

SELECT DPetAcNo as AcNo,
   SUM(DPetAmount) as DayTotal
   FROM PettyDetail
   WHERE DPetComCode='15'
         and DPetLocCode='01'
         and DPetDate=CONVERT(date,'20181113',111)
         GROUP BY DPetAcNo

上面查询的结果显示在下面

Result

根据结果2018/11/13 AcNo 009111总计= 22,995.00 和实际累计(2018/11/01-2018/11/13)总计009111 = 136,265.42

Cumulative total

4 个答案:

答案 0 :(得分:2)

修改后的regbas答案,应该会表现更好,因为它不会考虑太多行:

SELECT DPetAcNo as AcNo,
   SUM(case when DPetDate=CONVERT(date,'20181113',111) then DPetAmount else 0 end) as DayTotal,
   SUM(DPetAmount) as MonthTotal
   FROM PettyDetail
   WHERE DPetComCode='15'
         and DPetLocCode='01'
         and DPetDate >= '2018-11-01'
         AND DPetDate < '2018-12-01'
         AND DPetAcNo IN (select dpetacno from pettydetail WHERE DPetComCode='15'
         and DPetLocCode='01'
         and DPetDate = '2018-11-13')
         GROUP BY DPetAcNo

如果您想跳过CONVERT代码的混乱情况,则Sqlserver会将yyyy-mm-dd字符串隐式转换为日期。拥有涵盖日期,邮政编码,邮政编码,帐户以及金额的索引也将有所帮助

-

解决此问题的另一种方法是使用您似乎已经针对总计和总计编写的查询,如下所示:

Select * from
(/*Insert your query that does daytotal into these brackets*/) d
Inner join
(/*Insert your query that does cumtotal into these brackets*/) c
ON d.DPetAcNo = c.dpetacno

答案 1 :(得分:1)

我假设您的DPetDate是Date或Datetime数据类型。

您需要将每月1号的行过滤到所需的日期。 对于同一结果的日和月总计,请在CASE

中使用SUM ()
SELECT DPetAcNo as AcNo,
   SUM(CASE WHEN DPetDate  = '20181113' THEN DPetAmount ELSE 0 END) as DayTotal,
   SUM(DPetAmount) as MonthTotal,
   FROM PettyDetail
   WHERE DPetComCode='15'
         and DPetLocCode='01'
         and DPetDate >= '20181101'   -- from 1st of Nov
         and DPetDate <= '20181113'   -- to   13  of Nov
         GROUP BY DPetAcNo

编辑1: 仅根据所需日期选择AccNo。使用子查询获取月份总计。或者,您也可以在CROSS APPLY

中进行操作
SELECT  *,
       ( SELECT SUM(DPetAmount) 
         FROM   PettyDetail x 
         WHERE  x.DPetAcNo = p.AcNo 
         AND    x.DPetComCode = '15' 
         AND    x.DPetLocCode = '01'
         AND    x.DPetDate >= '20180101' 
         AND    x.DPetDate <= '20181113') as MonthTotal
FROM
(
    SELECT DPetAcNo as AcNo,
       SUM(CASE WHEN DPetDate  = '20181113' THEN DPetAmount ELSE 0 END) as DayTotal
       FROM PettyDetail p
       WHERE DPetComCode='15'
             and DPetLocCode='01'
             and DPetDate = '20181113'   -- for 13  of Nov only
             GROUP BY DPetAcNo
) p

答案 2 :(得分:0)

尝试一下

SELECT DPetAcNo as AcNo,
   SUM(case when DPetDate=CONVERT(date,'20181113',111) then DPetAmount else 0 end) as DayTotal,
   SUM(case when month(DPetDate)= 11 then DPetAmount else 0 end) as MonthTotal
   FROM PettyDetail
   WHERE DPetComCode='15'
         and DPetLocCode='01'
         GROUP BY DPetAcNo

答案 3 :(得分:0)

自我加入怎么样?

SELECT 
    DPetAcNo AcNo
,   DayTotal
,   SUM(DPetAmount) MonthlyTotal
FROM PettyDetail pd
JOIN (
    SELECT 
        DPetAcNo AcNo
    ,   SUM(DPetAmount) DayTotal
    FROM 
        PettyDetail
    WHERE 
        DPetComCode='15'
    AND DPetLocCode='01'
    AND DPetDate = CONVERT(DATE,'20181113',111)
    GROUP BY DPetAcNo
) pd2 ON pd2.AcNo = dp.DPetAcNo
WHERE 
    MONTH(DPetDate) = MONTH(CONVERT(DATE,'20181113',111)) 
AND YEAR(DPetDate)  = YEAR(CONVERT(DATE,'20181113',111))
GROUP BY DPetAcNo, DayTotal