简体   繁体   English

结束(分区)SQL-无结果

[英]Over (Partition) SQL - No Results

I'm trying to use the over (partition) clause to select aa record that selects the record with the max (noteid). 我正在尝试使用over(partition)子句来选择一个记录,该记录选择具有max(noteid)的记录。

First, when I run this query, I get the expected results which returns two records: 首先,当我运行此查询时,我得到了预期的结果,该结果返回两条记录:

select driveid, text, noteid from rpt_notesdetail where driveid='628678'
and Reason in (select codeid from rpt_QuickCodes where DescShort like N'Publicity')

And I get DriveID: 628678, Text: All donors will receiv a Free T-shirt, Two Improv tickets and Jersey Mike's Sub Coupon!, NoteID: 1410233 我得到的是DriveID: 628678, 文本:所有捐赠者都将获得一张免费的T恤,两张Improv门票和泽西·迈克的次级优惠券!,注释ID 1410233

DriveID : 628678, Text: All donors will receive a Free T-shirt, Two Improv tickets and Jersey Mike's Sub Coupon!, NoteID: 1410234 DriveID :628678, 文本:所有捐赠者将获得免费的T恤,两张Improv门票和Jersey Mike's的次级优惠券!, NoteID: 1410234

But when I try to use the over (partition) SQL: 但是,当我尝试使用过度(分区)SQL时:

(select text from (select (Text), noteid, max(noteid) over (partition by driveid) as 'max_note'
from rpt_NotesDetail 
where DriveID='628678' 
and createdate = (select max(createdate) from rpt_notesdetail where DriveID='628678') 
and Reason in (select codeid from rpt_QuickCodes where DescShort like N'Publicity')) AS A where A.noteid = A.max_note)

Which returns no results. 没有返回结果。 Any advice on what I am missing here? 关于我在这里缺少什么的任何建议?

I think you're doing it the hard way. 我认为您正在这样做。 See if you like this way better, and it also allows you to better find the problem. 看看您是否更喜欢这种方式,它还可以使您更好地发现问题。 I think it may be because you're not including "and noteid = ..." in your subselect for max(createdate). 我认为可能是因为您没有在max(createdate)的子选择中包括“ and noteid = ...”。

(select text from 
(
select (Text), noteid, row_number() over (partition by driveid order by noteid desc) as row_num
from rpt_NotesDetail 
where DriveID='628678' 
and createdate = (select max(createdate) from rpt_notesdetail where DriveID='628678') 
and Reason in (select codeid from rpt_QuickCodes where DescShort like N'Publicity')
) AS A 
-- comment out this line to see all rows, possibly showing the data problem
where A.row_num = 1
)

Based on a suggestion from Tim Lehner, I placed the Reason subquery inside the createdate subquery and it works: 根据Tim Lehner的建议,我将Reason子查询放在了createdate子查询中,并且可以正常工作:

(select text from (select (Text), noteid, max(noteid) over (partition by driveid) as 'max_note'
from rpt_NotesDetail where DriveID="DriveMaster"."DriveID" and createdate =
(select max(createdate) from rpt_notesdetail where
DriveID="DriveMaster"."DriveID" and Reason in
(select codeid from rpt_QuickCodes where DescShort like N'Publicity'))) AS A
where A.noteid = A.max_note)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM