添加计算项目 - SQL Server 2008R2

时间:2015-03-12 22:16:08

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

我需要在同一列中添加计算列项。请查看我的SQL代码,了解现有数据和所需结果。

根据以下内容添加产品O:

  • for 201501产品O = en_count
  • 时产品Y,W,N的yrmnth = 201501之和
  • for 201502产品O = en_count
  • 时产品Y,W,N的yrmnth = 20150之和

谢谢,

希拉勒

SQL:

--Existing Data                                               
--===== If the test table already exists, drop it             
IF OBJECT_ID('TempDB..#Table1') IS NOT NULL DROP TABLE #Table1

--===== Create the test table with                            
CREATE TABLE #Table1
(                                                 
    product char(100),                          
    yrmnth varchar(6),                         
    en_count int,                                
    date date,                                   
)                                                 

INSERT INTO #Table1 (product, yrmnth, en_count, date)                  
   SELECT 'Y', '201501', 5000 , '01/01/2015' union all
   SELECT 'Y', '201502', 6000 , '02/01/2015' union all
   SELECT 'Z', '201501', 7000 , '01/01/2015' union all
   SELECT 'Z', '201502', 8000 , '02/01/2015' union all
   SELECT 'W', '201501', 9000 , '01/01/2015' union all          
   SELECT 'W', '201502', 10000   , '02/01/2015' union all        
   SELECT 'N', '201501', 11000   , '01/01/2015' union all        
   SELECT 'N', '201502', 12000   , '02/01/2015'                  

--Desired Outcome                                             
IF OBJECT_ID('TempDB..#Table2') IS NOT NULL DROP TABLE #Table2

--===== Create the test table with                            
CREATE TABLE #Table2                                          
(                                                 
    product char(100),                          
    yrmnth varchar(6),                         
    en_count int,                                
    date date,                                   
)                                                 

INSERT INTO #Table2 (product, yrmnth, en_count, date)                  
    SELECT 'Y', '201501', 5000 , '01/01/2015' union all          
    SELECT 'Y', '201502', 6000 , '02/01/2015' union all          
    SELECT 'Z', '201501', 7000 , '01/01/2015' union all          
    SELECT 'Z', '201502', 8000 , '02/01/2015' union all          
    SELECT 'W', '201501', 9000 , '01/01/2015' union all          
    SELECT 'W', '201502', 10000 , '02/01/2015' union all          
    SELECT 'N', '201501', 11000 , '01/01/2015' union all          
    SELECT 'N', '201502', 12000 , '02/01/2015' union all          
    SELECT 'O', '201501', 32000 , '01/01/2015' union all          
    SELECT 'O', '201502', 36000 , '02/01/2015'                    

select *                                                      
from #Table2

1 个答案:

答案 0 :(得分:2)

这看起来像一个简单的insert ... select ...声明:

insert #Table1 (product, yrmnth, en_count,date)
select 'O', yrmnth, SUM(en_count), date
from #Table1 
group by yrmnth, date

我假设您的意思是对所有产品(Y,Z,W,N)求和,而不仅仅是(Y,W,N),因为前者给出指示的总和,而后者不同(缺少N值)。如果不是疏忽,则在where product in ('Y','W','N')条款之后添加from