如何在没有CTE的情况下编写此查询?
with cte as(
select FK#GharardadeAsli,MAX(PK#Shenase) as PK#Shenase,Max(TarikheSabt) as TarikheSabt from TBL#Gharardad where FK#GharardadeAsli is not null group by FK#GharardadeAsli
)
select * from v#gharardad inner join cte on cte.PK#Shenase = v#gharardad.[Shenase]
答案 0 :(得分:2)
怎么样?
SELECT *
FROM (
select FK#GharardadeAsli
, MAX(PK#Shenase) as PK#Shenase
, Max(TarikheSabt) as TarikheSabt
from TBL#Gharardad
where FK#GharardadeAsli is not null
group by FK#GharardadeAsli
) v
INNER JOIN (
select FK#GharardadeAsli
, MAX(PK#Shenase) as PK#Shenase
, Max(TarikheSabt) as TarikheSabt
from TBL#Gharardad
where FK#GharardadeAsli is not null
group by FK#GharardadeAsli
) cte ON cte.Shenase = v.Shenase
答案 1 :(得分:1)
您可以将其编写为子查询。
select *
from v#gharardad
inner join (select FK#GharardadeAsli,
MAX(PK#Shenase) as PK#Shenase,
Max(TarikheSabt) as TarikheSabt
from TBL#Gharardad
where FK#GharardadeAsli is not null
group by FK#GharardadeAsli
) sub on sub.PK#Shenase = v#gharardad.[Shenase]
答案 2 :(得分:-1)
声明表变量并执行相同的操作