我想我需要一些关于我的查询中有什么问题的指导。我正在努力
Watched_Gladiator=CASE WHEN FilmName IN (CASE WHEN FilmName LIKE '%Gladiator%' THEN 1 END) then OVER(PARTITION BY Cust_Nr) THEN 1 ELSE 0 END
也试过这个:
Watched_Gladiator=CASE WHEN FilmName IN (CASE WHEN FilmName LIKE '%Gladiator%' THEN Filmnamne END) then OVER(PARTITION BY Cust_Nr) THEN 1 ELSE 0 END
我目前得到的错误是:
Incorrect syntax near the keyword 'OVER'.
这基本上就是我的数据的样子
Cust_Nr Date FilmName Watched Gladiator
157649306 20150430 Gladiator 1
158470722 20150504 Nick Cave: 20,000 Days On Earth 0
158467945 20150504 Out Of The Furnace 0
158470531 20150504 FilmA 0
157649306 20150510 Gladiator 1
158470722 20150515 Gladiator 1
我想创建一个列(1或0),显示客户是否已经看过Gladiator然后是1 ELSE 0.我该怎么做?
我创建了一个测试专栏,尝试使用简单的LIKE'%Gladiator%'然后1 ELSE 0.此解决方案的问题在于,如果客户多次观看,它将不止一次显示1(一)。我只需要1或零。
我觉得我非常接近找到解决方案。我很擅长使用OVER()和CASE WHEN,但享受刺激:=)
答案 0 :(得分:3)
所以你说的是:
SELECT Cust_Nr, Date, FilmName,
CASE WHEN FilmName LIKE '%Gladiator%' THEN 1 ELSE 0 END as WatchedGladiator
FROM YourTable
WHERE YourColumn = @somevalue
不起作用吗?因为根据您提供的数据,它应该。
编辑:
根据Tim的评论,我只想将这一点添加到查询中。
SELECT Cust_Nr, Date, FilmName, WatchedGladiator
FROM
(
SELECT Cust_Nr, Date, FilmName,
CASE WHEN FilmName LIKE '%Gladiator%' THEN 1 ELSE 0 END as WatchedGladiator
FROM YourTable
WHERE YourColumn = @somevalue
) as wg
WHERE WatchedGladiator = 1
答案 1 :(得分:3)
以下是你想要的所有电影:
dijit/Menu
对于角斗士来说:
select r.*,
(case when row_number() over (partition by filmname order by date) = 1
then 1 else 0
end) as IsWatchedFirstAndGladiator
from results r;
答案 2 :(得分:1)
尝试分组到cust / film级别:
select
cust_nbr,
case when film_name like '%Gladiator%' then 1 else 0 end
from
(
select
cust_nbr,
film_name
from
<your table>
group by
cust_nbr,
film_name
) t
或者,作为替代方案:
select distinct cust_nbr
from
<your table>
where
filmname = 'Gladiator'
答案 3 :(得分:1)
因此,如果该客户观看了特定的电影,您希望按客户分组并添加一列吗?
你可以这样做:
SELECT Cust_Nr, MAX(Watched_Gladiator)
FROM( SELECT Cust_Nr,
Watched_Gladiator = CASE WHEN EXISTS
(
SELECT 1 FROM CustomerFilm c2
WHERE c2.Cust_Nr = c1.Cust_Nr
AND c2.FilmName LIKE '%Gladiator%'
) THEN 1 ELSE 0 END
FROM CustomerFilm c1 ) X
GROUP BY Cust_Nr
但如果您使用客户表而不是此表会更容易,那么您就不需要分组。