Sql Insert into select 创建临时表插入自增列

USE [20151102]
GO
=============================================
-- Author:        PPL
-- Create date: 2015-11-23
-- Description:    获取列表数据分析
-- =============================================
ALTER Procedure [dbo].[P_GetPayDetailedStatisProvinces]
(
    @beginTime VARCHAR(50),    --激活开始时间
    @endTime VARCHAR(50)    --激活结束时间
)
AS
BEGIN
    DECLARE    @Where nVARCHAR(3000);        --where 条件
    DECLARE @Sql NVARCHAR(3000);        --sql
set @Where=' where 1=1 '
    --检查临时表是否存在,否则删除临时表
    IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[#temptable]') AND type in (N'U'))
    BEGIN
        DROP TABLE [dbo].[#tmpOnlineRefundNumber]
        DROP TABLE [dbo].[#tmpOnlineRefundNumber]
        DROP TABLE [dbo].[#tmpOnlinTeachingNumber]
        DROP TABLE [dbo].[#tmpLinePayNumber]
        DROP TABLE [dbo].[#tmpLineRefundNumber]
        DROP TABLE [dbo].[#tmpLineTeachingNumber]
    END
    
    --创建临时表省份
    CREATE TABLE #tmpProvince(
        Id INT IDENTITY (1,1) not NULL PRIMARY KEY,--创建列id,自动增长
        Code VARCHAR(20)    -- ProvinceCode 省Code
    )
    
    --创建临时表线上缴费人数
    CREATE TABLE #tmpOnlinPpayNumber(
        Province VARCHAR(20),    -- ProvinceCode 省Code
        OnlinPpayNumber INT            -- 线上缴费人数
    )
    
    --创建临时表线上退费人数
    CREATE TABLE #tmpOnlineRefundNumber(    
        Province VARCHAR(20),       -- ProvinceCode 省Code
        OnlineRefundNumber INT            -- 线上退费人数
    )
    
    --创建临时表线上教材数
    CREATE TABLE #tmpOnlinTeachingNumber(    
        Province VARCHAR(20),      -- ProvinceCode 省Code
        OnlinTeachingNumber INT            -- 线上教材数
    )
    
    --创建临时表线下缴费人数
    CREATE TABLE #tmpLinePayNumber(    
        Province VARCHAR(20), -- ProvinceCode 省Code
        LinePayNumber INT              -- 线下缴费人数
    )
    
    --创建临时表线下退费人数
    CREATE TABLE #tmpLineRefundNumber(    
        Province VARCHAR(20),      -- ProvinceCode 省Code
        LineRefundNumber INT            -- 线下退费人数
    )
    
    --创建临时表线下教材数
    CREATE TABLE #tmpLineTeachingNumber(    
        Province VARCHAR(20),      -- ProvinceCode 省Code
        LineTeachingNumber INT            -- 线下教材数
    )
    
     --报名开始时间
     IF(@beginTime!='')
         BEGIN
           SET @Where+=' AND  CONVERT(varchar(100), c_project.OpenTime, 23)>=''' + @beginTime + ''' ';
         END
                
     --报名结束时间
     IF(@endTime!='') 
         BEGIN
             SET @Where+=' AND CONVERT(varchar(100), c_project.OpenTime, 23)<=''' + @endTime + ''' ';
         END
            
    --省份
    SET @Sql ='select Province FROM c_project where province is not null GROUP BY Province'
    INSERT INTO #tmpProvince EXEC(@Sql)        
            
    --线上缴费人数
    SET @Sql ='    SELECT Province,COUNT(c_project.OpenTime)as OnlinPpayNumber   from c_project ' + @Where + ' AND workid<3  and c_project.province is not null and  c_project.DepartCode is not null  and (PaySource=1 OR PaySource=2 OR PaySource=4 ) AND ispay= 1   GROUP BY Province'
    INSERT INTO #tmpOnlinPpayNumber EXEC(@Sql)
    PRINT @Sql
    
    --线上退费人数
    SET @Sql ='SELECT Province,0 as tuifei  from c_project '+@Where+'  AND ispay= 1 GROUP BY Province'
    INSERT INTO #tmpOnlineRefundNumber EXEC(@Sql)
    
    --线上教材数
    SET @Sql ='SELECT Province,COUNT(c_project.OpenTime)as countUsername  from c_project join o_LineItem ON c_project.id    =o_LineItem.ProjectId ' + @Where + ' AND c_project.workid<3  and c_project.province is not null and  c_project.DepartCode is not null   and (PaySource=1 OR     PaySource=2 OR PaySource=4 )    AND o_LineItem.BookCost!=0 AND ispay= 1 GROUP BY Province'
    INSERT INTO #tmpOnlinTeachingNumber EXEC(@Sql)
    --线下缴费人数
    SET @Sql =' SELECT Province,COUNT(c_project.OpenTime)as username from c_project '+@Where+' AND c_project.workid<3 and c_project.province is not null and  c_project.DepartCode is not null   and (PaySource=0 OR PaySource=3  ) AND ispay= 1   GROUP BY Province'
    INSERT INTO #tmpLinePayNumber EXEC(@Sql)
    
    --线下退费人数
    SET @Sql ='SELECT Province,0 as tuifei from c_project '+@Where+' AND c_project.workid<3 AND ispay= 1  GROUP BY Province'
    INSERT INTO #tmpLineRefundNumber EXEC(@Sql)
    
    --线下教材数
    SET @Sql ='SELECT Province,COUNT(c_project.OpenTime)as username  from c_project join o_LineItem ON c_project.id=    o_LineItem.ProjectId ' + @Where + '  AND c_project.workid<3 and c_project.province is not null and  c_project.DepartCode is not null    and (PaySource=0 OR PaySource=3)    AND o_LineItem.BookCost!=0 AND ispay= 1 GROUP BY Province '
    INSERT INTO #tmpLineTeachingNumber EXEC(@Sql)
    
    print (1)
    select t1.Id,t1.Code,ISNULL(t2.OnlinPpayNumber,0)AS OnlinPpayNumber,ISNULL(t3.OnlineRefundNumber,0) as OnlineRefundNumber,ISNULL(t4.OnlinTeachingNumber,0) OnlinTeachingNumber,ISNULL(t5.LinePayNumber,0) LinePayNumber,ISNULL(t6.LineRefundNumber,0) LineRefundNumber,ISNULL(t7.LineTeachingNumber,0) LineTeachingNumber
    FROM  #tmpProvince as t1
    left join #tmpOnlinPpayNumber     as t2 on t2.Province=t1.Code
    left join #tmpOnlineRefundNumber  as t3 on t3.Province=t1.Code
    left join #tmpOnlinTeachingNumber as t4 on t4.Province=t1.Code
    left join #tmpLinePayNumber       as t5 on t5.Province=t1.Code
    left join #tmpLineRefundNumber    as t6 on t6.Province=t1.Code
    left join #tmpLineTeachingNumber  as t7 on t7.Province=t1.Code
    order by t5.LinePayNumber desc
    
    --检查临时表是否存在,否则删除临时表
    IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[#temptable]') AND type in (N'U'))
    BEGIN
        DROP TABLE [dbo].[#tmpOnlinPpayNumber]
        DROP TABLE [dbo].[#tmpOnlineRefundNumber]
        DROP TABLE [dbo].[#tmpOnlinTeachingNumber]
        DROP TABLE [dbo].[#tmpLinePayNumber]
        DROP TABLE [dbo].[#tmpOnlineRefundNumber]
    END
END
GO

 

posted @ 2018-06-04 15:37  天豪  阅读(5644)  评论(0编辑  收藏  举报