在一个查询中按年份求和

时间:2017-09-22 13:03:14

标签: sql sql-server sql-server-2008

我写了以下查询

Select 
   SUM(CASE WHEN tr.flg = 'SEND' THEN TR.PR_TRN_AMT ELSE 0 END) AS PR_CURYEAR,
   SUM(CASE WHEN tr.flg ='RECIVED' THEN TR.PR_TRN_AMT ELSE 0   END) AS PR_CURYEAR    
FROM 
   [TRNDB].[dbo].[DAILY_PR] AS TR,
   [TRNDB].[dbo].[PR_MAIN] AS M,
WHERE
   M.PR_ID=TR.PR_CODE  
   AND TR.TRAN_DATE BETWEEN '2017-01-01' AND '2017-12-31'

此查询结果显示正确的详细信息。但是在相同的查询结果中想要从2017-01-01到2017-12-31和2016-01-01到2016-12-31的总和。我该如何更改查询?

3 个答案:

答案 0 :(得分:0)

只需改变你的where子句......

where  
   M.PR_ID=TR.PR_CODE 
   AND TR.TRAN_DATE BETWEEN '2016-01-01' AND '2017-12-31'

您也可以将年份放入查询中。我添加了它并使用了显式连接:

Select 
    SUM(CASE WHEN tr.flg = 'SEND' THEN TR.PR_TRN_AMT ELSE 0 END) AS PR_CURYEAR,
    SUM(CASE WHEN tr.flg ='RECIVED' THEN TR.PR_TRN_AMT ELSE 0   END) AS PR_CURYEAR,
    DATEPART(year,TR.TRAN_DATE) as TheYear
FROM 
    [TRNDB].[dbo].[DAILY_PR] AS TR 
    INNER JOIN [TRNDB].[dbo].[PR_MAIN] AS M ON
    M.PR_ID=TR.PR_CODE
WHERE  
    TR.TRAN_DATE BETWEEN '2016-01-01' AND '2017-12-31'
GROUP BY
    DATEPART(year,TR.TRAN_DATE) 

或许是这样,将其拆分为单独的列。

Select 
    SUM(CASE WHEN tr.flg = 'SEND' AND DATEPART(year,TR.TRAN_DATE) = 2017 THEN TR.PR_TRN_AMT ELSE 0 END) AS PR_CURYEAR,
    SUM(CASE WHEN tr.flg ='RECIVED' AND DATEPART(year,TR.TRAN_DATE) = 2017  THEN TR.PR_TRN_AMT ELSE 0   END) AS PR_CURYEAR,
    SUM(CASE WHEN tr.flg = 'SEND' AND DATEPART(year,TR.TRAN_DATE) = 2016 THEN TR.PR_TRN_AMT ELSE 0 END) AS PR_PREVYEAR,
    SUM(CASE WHEN tr.flg ='RECIVED' AND DATEPART(year,TR.TRAN_DATE) = 2016  THEN TR.PR_TRN_AMT ELSE 0   END) AS PR_PREVYEAR
FROM 
    [TRNDB].[dbo].[DAILY_PR] AS TR 
    INNER JOIN [TRNDB].[dbo].[PR_MAIN] AS M ON
    M.PR_ID=TR.PR_CODE
WHERE  
    TR.TRAN_DATE BETWEEN '2016-01-01' AND '2017-12-31'

答案 1 :(得分:0)

你可以试试这个。

SELECT
    SUM(CASE WHEN (TR.TRAN_DATE BETWEEN '2017-01-01' AND '2017-12-31') AND tr.flg = 'SEND' THEN TR.PR_TRN_AMT ELSE 0 END) AS PR_CURYEAR,
    SUM(CASE WHEN (TR.TRAN_DATE BETWEEN '2017-01-01' AND '2017-12-31') AND tr.flg ='RECIVED' THEN TR.PR_TRN_AMT ELSE 0   END) AS PR_CURYEAR,
    SUM(CASE WHEN (TR.TRAN_DATE BETWEEN '2016-01-01' AND '2016-12-31') AND tr.flg = 'SEND' THEN TR.PR_TRN_AMT ELSE 0 END) AS PR_PREYEAR,
    SUM(CASE WHEN (TR.TRAN_DATE BETWEEN '2016-01-01' AND '2016-12-31') AND tr.flg ='RECIVED' THEN TR.PR_TRN_AMT ELSE 0   END) AS PR_PREYEAR
FROM 
    [TRNDB].[dbo].[DAILY_PR] AS TR 
    INNER JOIN [TRNDB].[dbo].[PR_MAIN] AS M ON M.PR_ID=TR.PR_CODE 
WHERE 
    TR.TRAN_DATE BETWEEN '2016-01-01' AND '2017-12-31'

答案 2 :(得分:0)

您需要按年度对结果进行分组。

FROM nginx:1.11

# Remove sym links from nginx image
RUN rm /var/log/nginx/access.log
RUN rm /var/log/nginx/error.log

# Install logrotate
RUN apt-get update && apt-get -y install logrotate

# Copy MyApp nginx config
COPY config/nginx.conf /etc/nginx/nginx.conf

#Copy logrotate nginx configuration
COPY config/logrotate.d/nginx /etc/logrotate.d/

# Start nginx and cron as a service
CMD service cron start && nginx -g 'daemon off;'