SQL计算组的记录,然后将结果附加到表中

时间:2017-09-20 09:24:17

标签: sql-server

我有这张桌子:

tbl_Trans
--------------------------------
|Instrument|Trans_Type|Quantity|
|----------+----------+--------|
|HFT 123   |DEB       |50000   |
|HFT 123   |DEB       |20000   | 
|HFT 123   |CRE       |10000   | 
|ZGS 425   |DEB       |40000   |
|ZGS 425   |DEB       |30000   |
--------------------------------

然后,我必须根据Trans_Type列获取仪器的总数量。 DEB 表示添加, CRE 表示减去。

Example:
For HFT 123, Quantity=50000+20000-10000
             Quantity=60000

然后,应使用Trans_Type列中的 RES 将总数量附加到表格中。 决赛桌应如下所示:

--------------------------------
|Instrument|Trans_Type|Quantity|
|----------+----------+--------|
|HFT 123   |DEB       |50000   |
|HFT 123   |DEB       |20000   | 
|HFT 123   |CRE       |10000   | 
|ZGS 425   |DEB       |40000   |
|ZGS 425   |DEB       |30000   |
|HFT 123   |RES       |60000   |
|ZGS 425   |RES       |70000   |
--------------------------------

请帮忙。提前谢谢。

4 个答案:

答案 0 :(得分:4)

1. 9/6/2017
2. 9/5/2017
3. 9/7/2017
4. 9/4/2017
5. 9/8/2017

Try it your self here

这将为您提供所需:

WITH CTE
AS 
(
  SELECT Instrument,
  Trans_Type,
  CASE WHEN Trans_Type = 'DEB' THEN Quantity ELSE Quantity * -1 END AS Quantity
  FROM Table1
)
SELECT Instrument, Trans_type, Quantity FROM Table1
UNION ALL
SELECT Instrument, 'RES',
  SUM(Quantity) AS Total
FROM CTE AS c
GROUP BY Instrument

答案 1 :(得分:0)

您可以使用分组选择获取总计,并将该选择的结果附加到表格。

那将是:

insert into tbl_Trans (Instrument, Trans_Type, Quantity)
       select Instrument, 'RES', 
              sum(case Trans_Type when 'DEB' then Quantity 
                                  when 'CRE' then - 1 * Quantity)
       from tbl_Trans
       where Trans_Type in ('DEB', 'CRE')
       group by Instrument    

表格现在具有所需的内容:

|Instrument|Trans_Type|Quantity|
--------------------------------
|HFT 123   |DEB       |50000   |
|HFT 123   |DEB       |20000   | 
|HFT 123   |CRE       |10000   | 
|ZGS 425   |DEB       |40000   |
|ZGS 425   |DEB       |30000   |
|HFT 123   |RES       |60000   |
|ZGS 425   |RES       |70000   |

答案 2 :(得分:0)

在MSSQL Server中

- 根据Trans_Type

获取乐器的总数量

SELECT Instrument,        SUM(IIF(Trans_Type ='CRE', - 1 *数量,数量))AS [总数量] FROM tbl_Trans WHERE Trans_Type IN('DEB','CRE') GROUP BY仪器

- 要在表格中附加总数量

INSERT INTO tbl_Trans(乐器,Trans_Type,数量) 选择乐器,        'RES'AS [Trans_Type],        SUM(IIF(Trans_Type ='CRE', - 1 *数量,数量))AS [数量] 来自tbl_Trans Trans_Type IN('DEB','CRE') GROUP BY仪器

Try in fiddle

答案 3 :(得分:0)

create table  InstrumentTable  ( Instrument varchar(10) , Trans_Type char(3) ,  Quantity int)
go

insert into InstrumentTable
select 'HFT 123'   ,'DEB'       ,50000  
union all select 'HFT 123'   ,'DEB'       ,20000   
union all select 'HFT 123'   ,'CRE'       ,10000   
union all select 'ZGS 425'   ,'DEB'       ,40000    
union all select 'ZGS 425'   ,'DEB'       ,30000   

select Instrument  , Trans_Type  , Quantity from InstrumentTable
union all 
select Instrument  , 'RES'  ,  sum ( case when Trans_Type = 'DEB' then 
Quantity else (Quantity*-1) end )   
from InstrumentTable 
group by Instrument 
order by 1 ,2