简体   繁体   English

在临时表中插入多个Select语句

[英]Inserting Multiple Select statement in a Temp Table

I have the query below: 我有以下查询:

Insert into #BidYTDRegions (Code,APAC,EMEA,NAMerica,LAMerica)
    select 'Payroll', Count(*) from DashboardData
    where DataType = 'Bid' and SMHQRegion = 'APAC'  
    and Services like '%Streamline Payroll%',
    Count(*) from DashboardData
    where DataType = 'Bid' and SMHQRegion = 'EMEA'  
    and Services like '%Streamline Payroll%',
    Count(*) from DashboardData
    where DataType = 'Bid' and SMHQRegion = 'N. America'    
    and Services like '%roll%'  ,
    Count(*) from DashboardData
    where DataType = 'Bid' and SMHQRegion = 'L. America'    
    and Services like '%roll%'

I am getting an error Incorrect syntax near ','. Incorrect syntax near ','.收到错误Incorrect syntax near ','.错误Incorrect syntax near ','.

All I am trying to do is insert some data into a temp table based on select statments. 我要做的就是根据选择的语句将一些数据插入到临时表中。 Below is my temp table 下面是我的临时表

Create table #BidYTDRegions
(   
  Code nvarchar(50), 
  APAC int, 
  APACRatio nvarchar(20),
  EMEA int, 
  EMEARatio nvarchar(20),
  NAMerica int, 
  NAMericaRatio nvarchar(20),
  LAmerica int, 
  LAmericaRatio nvarchar(20),  
)

It looks like you want subqueries, which would be done like so: 看起来您想要子查询,可以这样做:

Insert into #BidYTDRegions (Code,APAC,EMEA,NAMerica,LAMerica)
    select 'Payroll'
        ,(select Count(*) from DashboardData
          where DataType = 'Bid' and SMHQRegion = 'APAC'  
          and Services like '%Streamline Payroll%')
        ,(select Count(*) from DashboardData
          where DataType = 'Bid' and SMHQRegion = 'EMEA'  
          and Services like '%Streamline Payroll%')
        ,(select Count(*) from DashboardData
          where DataType = 'Bid' and SMHQRegion = 'N. America'    
          and Services like '%roll%')
        ,(select Count(*) from DashboardData
          where DataType = 'Bid' and SMHQRegion = 'L. America'    
          and Services like '%roll%')

I think you want conditional aggregation: 我认为您需要条件聚合:

Insert into #BidYTDRegions (Code, APAC, EMEA, NAMerica, LAMerica)
    select 'Payroll',
            sum(case when SMHQRegion = 'APAC' and Services like '%Streamline Payroll%' then 1 else 0 end),
            sum(case when SMHQRegion = 'EMEA' and Services like '%Streamline Payroll%' then 1 else 0 end),
            sum(case when SMHQRegion = 'N. America' and Services like '%roll%' then 1 else 0 end),
            sum(case when SMHQRegion = 'S. America' and Services like '%roll%' then 1 else 0 end)
    from DashboardData
    where DataType = 'Bid';

It is unclear to me why the Services has a different comparison for different regions. 我不清楚,为什么Services在不同地区的比较会有所不同。 If it were the same, then that condition could be factored out and moved to the WHERE clause along with DataType . 如果相同,则可以排除该条件,并将其与DataType一起移至WHERE子句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM