簡體   English   中英

在臨時表中插入多個Select語句

[英]Inserting Multiple Select statement in a Temp Table

我有以下查詢:

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 ','.收到錯誤Incorrect syntax near ','.錯誤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),  
)

看起來您想要子查詢,可以這樣做:

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

我認為您需要條件聚合:

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在不同地區的比較會有所不同。 如果相同,則可以排除該條件,並將其與DataType一起移至WHERE子句。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM