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

时间:2019-01-17 20:32:41

标签: sql sql-server sql-server-2012

我有以下查询:

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%'

我遇到错误Incorrect syntax near ','.

我要做的就是根据选择的语句将一些数据插入到临时表中。下面是我的临时表

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),  
)

2 个答案:

答案 0 :(得分:3)

您似乎想要子查询,可以这样做:

Exception ex = Newtonsoft.Json.JsonConvert.DeserializeObject<Exception>(upResponse.ExceptionJSONString);

答案 1 :(得分:1)

我认为您需要条件聚合:

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';

我不清楚Services为何在不同地区具有不同的比较。如果相同,则可以排除该条件,并将其与WHERE一起移至DataType子句。