-1

我正在触发以下查询>>

SELECT sauda_date, 
   Sum(CASE 
         WHEN buy_sell = 1 
              AND scrip_code = 'ADANIENT' 
              AND sauda_date BETWEEN CONVERT(DATETIME, '1 Mar 2013') AND 
                                     CONVERT(DATETIME, '1 Apr 2013') THEN 
         trade_qty 
         ELSE 0 
       END)            AS BuyQty, 
   Sum(CASE 
         WHEN buy_sell = 2 
              AND scrip_code = 'ADANIENT' 
              AND sauda_date BETWEEN CONVERT(DATETIME, '1 Mar 2013') AND 
                                     CONVERT(DATETIME, '1 Apr 2013') THEN 
         trade_qty 
         ELSE 0 
       END)            AS SellQty, 
   Sum(CASE 
         WHEN buy_sell = 1 
              AND scrip_code = 'ADANIENT' THEN trade_qty 
         ELSE 0 
       END) - Sum(CASE 
                    WHEN buy_sell = 2 
                         AND scrip_code = 'ADANIENT' THEN trade_qty 
                    ELSE 0 
                  END) AS CarryForword 
FROM   tradefile 
WHERE  scrip_code = 'ADANIENT' 
GROUP  BY sauda_date 

我得到以下结果:

    sauda_date | Buy_Qty | SellQty | CarryForward

   01 Mar 2013     2000      0         2000
   01 Mar 2013        0     2000          0
   01 Mar 2013     4000      0         4000
   02 Mar 2013        0     1000       3000
   02 Mar 2013     2000       0        5000
   03 Mar 2013        0     4000       1000

但我希望它是:

    sauda_date | Buy_Qty | SellQty | CarryForward

   01 Mar 2013     6000     2000       4000
   02 Mar 2013     2000     1000       5000
   03 Mar 2013        0     4000       1000

从表格中我们可以看出:

CaryyForward=Buy_Qty-Sell_Qty;

01 Mar 2013 is date and Buy_Qty=sum(buy_Qty) for 01 Mar 2013.
02 Mar 2013 is date and Buy_Qty=sum(buy_Qty) for 02 Mar 2013.

[Sell_Qty 和 CarryForward 相同]。

我尝试了不同的 group by in case when ,但后来知道它在 case when 中是不允许的。

我也尝试使用不同的 where 条件但没有奏效。

请帮我。

4

4 回答 4

1

将查询放在子查询中,例如

SELECT  sauda_date , 
        MAX(Buy_Qty) Buy_Qty,
        MAX(SellQty) SellQty, 
        MAX(CarryForward) CarryForward
FROM
        (
            -- ...put your current query here ...
        ) x
GROUP   BY sauda_date
于 2013-04-04T06:39:11.460 回答
0

您可以将 group by 子句与多个参数一起使用。请注意我在代码末尾添加的部分:

select sauda_date,
           SUM(CASE WHEN Buy_sell = 1 
                         and scrip_code='ADANIENT' 
                         and Sauda_Date between convert(datetime,'1 Mar 2013') 
                                        and convert(datetime,'1 Apr 2013') 
                    THEN Trade_Qty ELSE 0 END) AS BuyQty,
           SUM(CASE WHEN Buy_sell = 2 
                         and scrip_code='ADANIENT' 
                         and Sauda_Date between convert(datetime,'1 Mar 2013') 
                                        and convert(datetime,'1 Apr 2013') 
                    THEN Trade_Qty ELSE 0 END) AS SellQty ,
          SUM(CASE WHEN Buy_sell = 1 
                        and scrip_code='ADANIENT' 
                   THEN Trade_Qty ELSE 0 END) 
              - SUM(CASE WHEN Buy_sell = 2 
                              and scrip_code='ADANIENT' 
                         THEN Trade_Qty ELSE 0 END) AS CarryForword 
from tradefile 
where scrip_code='ADANIENT' 
group by sauda_date, Buy_Qty,  SellQty, CarryForward

这不会为您折叠总数吗?

于 2013-04-04T06:44:11.717 回答
0

尝试:

GROUP BY DATEADD(dd, 0, DATEDIFF(dd, 0, sauda_date))

如果没有您的示例数据,我无法确定,但看起来您在表中的每一行都有不同的 TIME 部分。2013-04-04 08:24:13.790例如,按日期时间分组会有所不同2013-04-04 17:24:13.790。上面的查询将两者都转换为2013-04-04 00:00:00.000第一个。

编辑:您还必须用相同的结构替换 SELECT 列表中的 sauda_date。

SQLFiddle 演示

于 2013-04-04T06:47:43.170 回答
0

我的数据库也有时间。因此,由于时间不同,它采取了不同的日期。

所以我把查询写成:

select CONVERT(VARCHAR(11),sauda_date) AS sauda_date,SUM(CASE WHEN Buy_sell = 1 and scrip_code='ADANIENT' and Sauda_Date between convert(datetime,'1 Mar 2013') and convert(datetime,'1 Apr 2013') THEN Trade_Qty ELSE 0 END) AS BuyQty,SUM(CASE WHEN Buy_sell = 2 and scrip_code='ADANIENT' and Sauda_Date between convert(datetime,'1 Mar 2013') and convert(datetime,'1 Apr 2013') THEN Trade_Qty ELSE 0 END) AS SellQty ,     SUM(CASE WHEN Buy_sell = 1 and scrip_code='ADANIENT' THEN Trade_Qty ELSE 0 END) -SUM(CASE WHEN Buy_sell = 2 and scrip_code='ADANIENT' THEN Trade_Qty ELSE 0 END) AS CarryForword from tradefile where scrip_code='ADANIENT' group by CONVERT(VARCHAR(11),sauda_date)

这行得通。

于 2013-04-04T07:30:54.357 回答