0

#Temp テーブルまたはテーブル変数を使用せずに、次のことを行う必要があります。GROUP BY では CTE を実行できません。これを行う方法はありますか?

select Title, count(*) as PlayCount
into #Temp
from LC l 
join Play p on p.lcID = l.lcID
group by Title

declare @Max int 
select @Max = max(PlayCount) from #Temp

select Title, PlayCount, cast(PlayCount as float) / cast(@Max as float) as Weight
from #Temp
4

3 に答える 3

3

サブクエリ/cte と を使用しますMAX() OVER()

SELECT Title, PlayCount, PlayCount*1.0 / Max_CT AS WEIGHT
FROM (SELECT Title
           , COUNT(*) AS PlayCount
           , MAX(COUNT(*)) OVER() AS Max_CT
      FROM LC l 
      JOIN PLAY P ON P.LCID = L.LCID
      GROUP BY Title
     )sub

(MS SQL 2005 以降を想定)

于 2013-09-18T16:13:48.953 に答える
1

サブクエリ?おそらく最もきれいではない

select Title, count(*) as PlayCount , count(*) / tot
from LC l join Play p on p.lcID = l.lcID ,
(select max(a) as tot from (select count(*)  as a from LC group by lcid))
group by Title , tot

どのテーブルが何をしているのか完全にはわかりませんが、上記でアイデアが得られるはずです

于 2013-09-18T16:10:16.747 に答える
0
select Title, count(*) as PlayCount, cast(count(*) as float) / cast(
(
select max(PlayCount) from (select count(*) as PlayCount
from LC l 
join Play p on p.lcID = l.lcID
group by Title
) q) as float) as Weight
from LC l
join Play p on p.lcID = l.lcID
group by title

これは少なくとも近いと思います。しかし、「CTEなし」の要件は人為的なようです-これは大学またはstのラボテストですか?

于 2013-09-18T16:16:02.007 に答える