加入子查询时,我的别名附近的语法不正确?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了加入子查询时,我的别名附近的语法不正确?相关的知识,希望对你有一定的参考价值。

我的代码如下。我收到错误“关键字'otb'附近的语法不正确”(第48行)和“关键字'otb_ly'附近的语法不正确”

我不知道这意味着什么!因为加入我的两个子查询(我将其命名为otb和otb_ly)对我来说是有意义的。

子查询本身就可以正常工作。

declare @snapLY table (hotel_id tinyint, import_date date) 
insert into @snapLY select hotel_id, max(import_date) from [UKRMC].[dbo].[block_res]
where import_date <= convert(date, DATEADD(day, 1, DATEADD(year, -1, GETDATE()) ) )
group by hotel_id 

select otb.sita,
       otb.month_TY,
       otb.year_TY,
       otb.Market_segment,
       otb.rn_TY as OTB_rn_TY,
       otb.rev_TY as OTB_rev_TY
       --otb_ly.rev_LY as OTB_rev_LY,
       --otb_ly.rn_LY as OTB_rn_LY 
(
select contacts.sita,
      data.hotel_id,
      data.month_TY
      ,data.year_TY
      ,Market_segment = seg.SEG
      ,rn_TY = ISNULL(datatwo.rn_TY,0)
      ,rev_TY = ISNULL(datatwo.rev_TY, 0)
from (
      SELECT hotel_id, datename(month, DATEARRIVED) as month_TY, datename(year, DATEARRIVED) year_TY
      FROM [UKRMC].[dbo].[revenue] rev
      JOIN [UKRMC].[dbo].[contacts] contacts on rev.hotel_id = contacts.ID
      WHERE DATEARRIVED BETWEEN  DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101') -- first day of the month
             AND DATEADD(MONTH, 5, DATEADD(DAY,-DAY(DATEADD(MONTH, 1, GETDATE())), DATEADD(MONTH, 1, GETDATE()))) -- 6 months later
             and sita not like '%GLARR%' 
      group by  hotel_id, datename(month, DATEARRIVED), datename(year, DATEARRIVED) 

     ) data
cross join (
      select SEG 
      from [UKRMC].[dbo].[Segmentation] 
      where SEG in ('RAC', 'BIT', 'BIQ', 'CBI', 'TOF', 'QOF', 'BAO', 'FIT', 'LYO', 'RER', 'OTH', 'NRG', 'XXX', 'CRW', 'BGR', 'BGO', 'LGR', 'LGS') 
     ) seg 
left join (
       SELECT hotel_id, market_segment, datename(month, DATEARRIVED) month_TY, datename(year, DATEARRIVED) year_TY, sum(AMTROOM) as rev_TY, sum(STAYDAYS) as rn_TY
       FROM [UKRMC].[dbo].[revenue] revtwo
       WHERE DATEARRIVED BETWEEN  DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101') -- first day of the month
             AND DATEADD(MONTH, 5, DATEADD(DAY,-DAY(DATEADD(MONTH, 1, GETDATE())), DATEADD(MONTH, 1, GETDATE()))) -- 6 months later 
             AND hotel_id != 61 --not GLARR, id taken from contacts table
       GROUP BY hotel_id, datename(month, DATEARRIVED), datename(year, DATEARRIVED), market_segment 
           ) datatwo on data.hotel_id = datatwo.hotel_id and seg.SEG = datatwo.market_segment and data.month_TY = datatwo.month_TY and data.year_TY = datatwo.year_TY 
join [UKRMC].[dbo].[contacts] contacts on contacts.id = data.hotel_id 
) otb
LEFT JOIN
(
SELECT sita, market_seg, month_LY, year_LY,  sum(rev_LY) as rev_LY, sum(rn_LY) as rn_LY
FROM (
SELECT block.hotel_id, SITA
      ,datename(month,stay_date) as month_LY
      ,datename(year, stay_date) as year_LY
      ,sum(rev_room) as rev_LY
      ,sum(quantity) as rn_LY

      ,market_seg
  FROM [UKRMC].[dbo].[block_res] block
  JOIN @snapLY spit on block.hotel_id = spit.hotel_id and block.import_date = spit.import_date
  JOIN [UKRMC].[dbo].[Contacts] contacts on block.hotel_id = contacts.ID
       WHERE stay_date >= DATEADD(year, -1, DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101'))
             --stay_date >= DATEADD(year, -1, getdate())  
         and stay_date <= DATEADD(year, -1, DATEADD(MONTH, 5, DATEADD(DAY,-DAY(DATEADD(MONTH, 1, GETDATE())), DATEADD(MONTH, 1, GETDATE())))) 
  group by sita, market_seg, block.hotel_id, datename(month,stay_date),datename(year, stay_date) 
 UNION ALL 
  SELECT revenue.HOTEL_ID, SITA
      ,datename(month,DATEARRIVED) as month_LY
      ,datename(year,DATEARRIVED) as year_LY
      ,sum(AMTROOM) as rev_LY
      ,sum(STAYDAYS) as rn_LY
      ,market_segment
 FROM [UKRMC].[dbo].[revenue] revenue
 JOIN [UKRMC].[dbo].[Contacts] contacts on revenue.HOTEL_ID = contacts.ID
 JOIN @snapLY spit on revenue.HOTEL_ID = spit.hotel_id 
 WHERE DATEARRIVED between DATEADD(year, -1, DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101')) 
                     and  DATEADD(day, -1, DATEADD(year, -1, getdate())) 
       and DATEARRIVED < spit.import_date
 GROUP BY sita, revenue.HOTEL_ID, datename(month,DATEARRIVED), datename(year,DATEARRIVED), market_segment
  ) union_LY
GROUP BY sita, market_seg, hotel_id, month_LY, year_LY
) otb_ly
on otb.sita = otb_ly.sita and otb.Market_segment = otb_ly.market_seg and otb.month_LY = otb_ly.month_LY and otb.year_LY = otb_ly.year_LY
答案

你错过了第14行的FROM

...
       --otb_ly.rn_LY as OTB_rn_LY 
FROM
(
...

老实说,这个问题需要大量的返工......这真的很难理解。

以上是关于加入子查询时,我的别名附近的语法不正确?的主要内容,如果未能解决你的问题,请参考以下文章

运行此查询时收到错误消息“'(' 附近的语法不正确”

存储过程错误附近的语法不正确

sql server 子查询连接错误

sql-server:“,”附近的语法不正确

SqlException:“nvarchar”附近的语法不正确

System.Data.SqlClient.SqlException(0x80131904):“。”附近的语法不正确