3

I am storing a list of Messages in my database (so that if I crash I will not lose them).

But when I pull them out, I need to pull them out in chunks and in order.

What I am looking for is a query that will give me the top x rows until column IsWeirdMessage = 1.

So, assuming I have a table that looks like this:

CREATE TABLE MyMessages(MessageId bigint IDENTITY(1,1),
    MyMessage varchar(100), IsWeirdMessage bit)

INSERT INTO MyMessages(MyMessage, IsWeirdMessage)
SELECT 'SomeValue1', 0 UNION ALL
SELECT 'SomeValue2', 0 UNION ALL
SELECT 'SomeValue3', 0 UNION ALL
SELECT 'SomeValue3', 0 UNION ALL
SELECT 'SomeValue4', 0 UNION ALL
SELECT 'SomeValue5', 0 UNION ALL
SELECT 'WeirdThingHere', 1 UNION ALL
SELECT 'SomeValue7', 0 UNION ALL
SELECT 'SomeValue8', 0 UNION ALL
SELECT 'SomeValue9', 0 UNION ALL
SELECT 'OtherWeirdStuff', 1 UNION ALL
SELECT 'SomeValue11', 0  
GO

How could I craft a query that will get me SomeValue1 through SomeValue5 (in order), keying off of IsWeirdMessage?

0

3 Answers 3

5

Something like this should do it:

select * from MyMessages
where messageid > @MessageID
and messageid <=
  (select min(messageid) from MyMessages
   where IsWeirdMessage = 1 and messageid > @MessageID)
order by messageid

Where the @MessageID is the value of the last weird message you retrieved. You would start with 0 to get the first batch, then 7 to get the second batch, etc.

Demo: http://www.sqlfiddle.com/#!3/10336/9

1
  • 1
    Thanks for the answer! Also thanks for showing me SQLFiddle. That is very nice!
    – Vaccano
    Mar 29, 2012 at 16:14
1
SELECT m.MessageID, m.MyMessage
FROM dbo.MyMessages AS m
WHERE NOT EXISTS 
(
  SELECT 1 FROM dbo.MyMessages AS m2
  WHERE IsWeirdMessage = 1 
  AND m2.MessageID < m.MessageID
)
ORDER BY MessageID;
3
  • Thanks for the answer! I went with Mellamokb's solution because it ended up doing index seeks in my actual tables. Your solution was great, but it was doing index scans.
    – Vaccano
    Mar 29, 2012 at 16:15
  • To be fair, you didn't tell us your index structure. Did the queries perform differently, or did you just see a scan and assume it was worse? Often a range scan is hiding behind what looks like an index seek. Mar 29, 2012 at 21:08
  • True, I did not. (I did not think it would matter) Both answers were very good (and really performed well). I had to find something (however very small) to pick one over the other... As for Range Scans behind Index Seeks... That makes me nervous. I have never heard of that. But the actual performance (time to run) of both was close enough in this case.
    – Vaccano
    Mar 29, 2012 at 21:17
0
select b.*
from
    (   select
            row_number() over (order by (select 1)) as PartitionId
            ,isnull((select max(t.MessageId) from MyMessages t where t.MessageId < tb.MessageId and t.IsWeirdMessage = 1), 0) as StartID
            ,tb.MessageId as EndId
        from MyMessages tb
        where IsWeirdMessage = 1
        union all
        select count(case when IsWeirdMessage = 1 then MessageId else null end) + 1
            ,max(case when IsWeirdMessage = 1 then MessageId else 0 end)
            ,max(messageId) + 1
        from MyMessages
    ) as a
    cross apply 
    (   select * from MyMessages b
        where
            b.MessageId > a.StartID
            and b.MessageId < a.EndID
    ) as b
where a.PartitionId = @PartID

where the @PartID is identifier of Partition that you want to choose

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.