Solved

how did you hear about us?

Posted on 2013-11-02
3
443 Views
Last Modified: 2013-11-07
I think this question may be related to
http://www.experts-exchange.com/Database/MySQL/Q_28280747.html


I want either the field (profile_id)  or the row of the answer.

CREATE TABLE `a_messages2` (
       `a_messages_id` INT (11) auto_increment  primary key
     , `profile_id` VARCHAR(20)
     , `sender` VARCHAR(20)
     , `message_id` BIGINT (20)
     , `this_user` VARCHAR(20)
     );



INSERT INTO a_messages2
          (`profile_id`, `sender`, `message_id`, `this_user`)
    VALUES
('abl', 'staff3', 3662615602, 'staff3'),
('abl', 'abl', 3660687080, 'staff3'),
('abl', 'staff3', 3658803944, 'staff3'),
('abl', 'staff3', 3649952930, 'staff3'),
('abl', 'staff3', 3644250249, 'staff3'),
('abl', 'staff3', 3614235515, 'staff3'),
('abl', 'abl', 3614140442, 'staff3'),
('abl', 'staff3', 3613984751, 'staff3'),
('abl', 'staff3', 3613936332, 'staff3'),
('abl', 'abl', 3613469574, 'staff3'),
('abl', 'staff3', 3612970481, 'staff3'),
('abl', 'abl', 3609276901, 'staff3'),
('abl', 'abl', 3609276896, 'staff3'),
('abl', 'staff3', 3866086756, 'staff3'),
('abl', 'staff3', 3866079064, 'staff3'),
('abl', 'abl', 3856964132, 'staff3'),
('abl', 'staff3', 3844779912, 'staff3'),
('abl', 'staff3', 3916948647, 'staff3'),
('abl', 'staff3', 3880367425, 'staff3'),
('abl', 'abl', 3977088222, 'staff3'),
('abl', 'staff3', 3974513295, 'staff3'),
('abl', 'staff3', 3973285282, 'staff3'),
('abl', 'abl', 3960907806, 'staff3'),
('abl', 'staff3', 4003664468, 'staff3'),
('abl', 'abl', 4002246444, 'staff3'),
('abl', 'staff3', 3996089944, 'staff3'),
('abl', 'staff3', 4024726432, 'staff3'),
('abl', 'abl', 4021516823, 'staff3'),
('abl', 'staff3', 4018957318, 'staff3'),
('abl', 'abl', 4050609080, 'staff3'),
('abl', 'staff3', 4052368867, 'staff3'),
('abl', 'abl', 4052424820, 'staff3'),
('abl', 'staff3', 4053183569, 'staff3'),
  
('swa','swa','4153199171','staff3'),
('swa','staff3','4150323049','staff3'),
('swa','swa','4146362776','staff3'),
('swa','staff3','4142884979','staff3'),
('swa','staff3','4142884976','staff3')

Open in new window




Real world explanation:
find customers the who messaged us first

Ask them questions such as 'how did you hear about us: radio, tv, internet, friend'

Because these customers did not hear about us from us, because they sent us the first message.


select profile_id, sender,message_id,this_user from a_messages2 where profile_id='abl' order by message_id limit 1

select profile_id, sender,message_id,this_user from a_messages2 where profile_id='swa' order by message_id limit 1

 staff3 sent first message to swa  '4142884976'


abl sent first message to staff3 '3609276896'

there should be results for abl because abl sent first message
but there should be no results for swa, because we sent the first message
0
Comment
Question by:rgb192
  • 2
3 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39619770
Locate the minimum message_id per profile_id, match back to messages by message id and then only list those where the sender equals the profile_id
| SENDER | MESSAGE_ID |
|--------|------------|
|    abl | 3609276896 |

Open in new window

produced by the following query:
SELECT
        m.sender
      , m.message_id
FROM (
      SELECT
        profile_id
      , min(message_id) AS message_id
      FROM a_messages2
      GROUP BY
        profile_id
     ) AS mn
INNER JOIN a_messages2 AS m ON mn.message_id =  m.message_id
WHERE m.profile_id = m.sender
;

/* http://sqlfiddle.com/#!9/8efe9/1 */

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 39619858
You answered the question correctly with the data provided but with my data some profiles I have written first got returned.

So I would want to modify this query to show me more information so I can give you more data.
0
 

Author Comment

by:rgb192
ID: 39631021
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now