0

I'm currently having to run a query like the below for a one off report process at work.

However for each item in the table there are multiple associated "messages" that are all saved, this means each item is returned multiple times. I'd like to only show each item once, as per the examples and further explanation below.

I realize this is (at least in my opinion) a poor structure, but the report needs to be done and this is how the data is stored :-(

SQL Fiddle: http://sqlfiddle.com/#!6/76fce/8

Query:

SELECT messageId, receiver, createdDate, itemId from messages_0,items WHERE
    itemId IN (1, 2, 3)
    AND (receiver = '100' OR receiver = '200')
    AND messages_0.description LIKE '%'+items.name+'%'
union all
SELECT messageId, receiver, createdDate, itemId  from messages_1,items WHERE
    itemId IN (1, 2, 3)
    AND (receiver = '100' OR receiver = '200')
    AND messages_1.description LIKE '%'+items.name+'%'

Note: there are two message tables, hence the union all

Example messages:

messageId | receiver | createdDate       | description
--------------
1         | 100      | 2012/11/27 12:00  | The Dog is awesome
2         | 100      | 2012/11/27 13:00  | Now the Dog is boring
4         | 200      | 2012/11/27 11:30  | I have Wood :-)

Example items:

itemID | name 
--------------
1      | Dave
2      | Dog
3      | Wood

Result:

messageId | receiver | createdDate      | itemId 
1         | 100      | 2012/11/27 12:00 | 2
2         | 100      | 2012/11/27 13:00 | 2
4         | 200      | 2012/11/27 11:00 | 3

However, I need to only show each item once. Where only the oldest row (by the createdDate) is shown.

Target Result:

messageId | receiver | createdDate      | itemId 
1         | 100      | 2012/11/27 12:00 | 2
4         | 200      | 2012/11/27 11:00 | 3

How can I do this in SQL (Sybase)? So far I have been looking at both group by (which would only return an id) and some sort of sub query, but have been unable to get anything to work!

SQL Fiddle: http://sqlfiddle.com/#!6/76fce/8

3
  • A bug in my example, read 3, have updated the question!
    – Pez Cuckow
    Nov 27, 2012 at 21:37
  • you show 12 :00 in target result, should be not 13:00 ?
    – Justin
    Nov 27, 2012 at 21:39
  • @Justin no, I want to see the oldest item, not the most recent
    – Pez Cuckow
    Nov 27, 2012 at 21:46

1 Answer 1

1

If I understood you right, something like this could be a start.

SELECT
  t, messageId, receiver, createdDate, itemId 
FROM
  (
    SELECT 
      m.messageId, m.receiver, m.createdDate, m.t,
      i.itemId 
    FROM
      items i
      INNER JOIN (
        SELECT description, messageId, receiver, createdDate, 0 t FROM messages_0 
        UNION 
        SELECT description, messageId, receiver, createdDate, 1 t FROM messages_1
      ) m ON m.description LIKE '%' + i.name + '%' 
             AND m.receiver IN ('100', '200')
    WHERE
      i.itemId IN (1, 2, 3)
  ) data
WHERE
  createdDate = (
    SELECT MIN(createdDate) FROM (
      SELECT createdDate FROM messages_0 WHERE messageId = data.messageId AND data.t = 0
      UNION
      SELECT createdDate FROM messages_1 WHERE messageId = data.messageId AND data.t = 1
    )
  )

I would put indexes on

  • messages_0 / messages_1
    • (messageId, createdDate)
    • (receiver, messageId, createdDate, description)
  • items
    • (itemId, name)
4
  • This looks great, I only have one concern, what if there are two messages with the same created date?
    – Pez Cuckow
    Nov 27, 2012 at 23:09
  • @Pez: With the same message ID and the same created date? The sub-query in the outermost WHERE clause just returns the messages with the matching ID. Is that likely for your data?
    – Tomalak
    Nov 27, 2012 at 23:13
  • No you're right, I was reading it as from the entire DB, rather than the subset
    – Pez Cuckow
    Nov 28, 2012 at 9:26
  • @Pez On second thought, was is a logical mistake in the above. There's a good chance that the same messageId is in both messages_0 and messages_1 but refers to completely different items. A connection to the right messages_x table was missing in the outermost where clause. I've added that connection.
    – Tomalak
    Nov 28, 2012 at 10:45

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.