Вставка оператора множественного выбора в временную таблицу - PullRequest
0 голосов
/ 17 января 2019

У меня есть запрос ниже:

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 голосов
/ 17 января 2019

Я думаю, вы хотите условное агрегирование:

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.

0 голосов
/ 17 января 2019

Похоже, вы хотите подзапросы, которые были бы сделаны так:

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