Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8904681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:11:43+00:00 2026-06-15T02:11:43+00:00

I’m currently having to run a query like the below for a one off

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:11:44+00:00Added an answer on June 15, 2026 at 2:11 am

    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)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to run a str_replace or preg_replace which looks for certain words
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.