Srijan Sharma
Srijan Sharma

Reputation: 693

Sql query with three conditions

I have a database with a table having content as below :

message_number  message_type   message_chat
     0                IN           Hi
     1                OB           Hello
     2                IN           Help
     3                IN           Want to find this thing
     4                OB           Sure
     5                OB           Please let me know

I have written 5 rows since i want to incorporate all possible cases that i want in my query in the example table that i showed.

Now in my query output, i want something like :

message_in                                 message_out
  Hi                                         Hello
  Help                                        NULL
  Want to find this string                   Sure
     NULL                                  Please let me know

So the cases that i want to consider are :

  1. suppose if message_number=0 and message_number=1 both have message_type value as IN then put message_chat_in as message_chat(at message_number=0) and message_chat out as NULL and the iterate over message_number=1

  2. if message_number =0 have message_type=IN and message_number =1 have message_type=OB, then show message_chat(at message_number=0) as message_chat_in and message_chat(at message_number=1) as message_out and dont iterate over message_number=1;

hope i have clarified the condition though i have included all three condition in the expected output.How should my sqlquery look like?

Edit : I am using mysql version 5.5.8

Upvotes: 1

Views: 1421

Answers (3)

Radim Bača
Radim Bača

Reputation: 10711

Ok, since there is no analytical functions in MySQL less than 8 the code may not be easy to follow:

with data_rn as
(
  -- this isolate consecutive rows with the same message_type
  select d1.*, count(d2.message_number) rn
  from data d1
  left join data d2 on d1.message_number > d2.message_number and d1.message_type != d2.message_type
  group by d1.message_number
),
data_rn2 as
(
    -- this marks the rows where new rows has to be added (i.e. when rn2 != 0)
    select d1.*, count(d2.message_number) rn2
    from data_rn d1
    left join data_rn d2 on d1.rn = d2.rn and d1.message_type = d2.message_type and d1.message_number > d2.message_number
    group by d1.message_number
), 
data_added as
(
    -- this add new rows
    select message_number, message_type, message_chat
    from data_rn2
    union all 
    select message_number - 0.5, 'OB', NULL from data_rn2 where message_type = 'IN' and rn2 != 0
    union all 
    select message_number - 0.5, 'IN', NULL from data_rn2 where message_type = 'OB' and rn2 != 0
    order by message_number
), data_added_rn as
(
    -- this compute new row numbering
    select d1.*, ceil((count(d2.message_number)+1)/2) rn 
    from data_added d1
    left join data_added d2 on d1.message_number > d2.message_number
    group by d1.message_number
)
-- this will do the final formating
select max(case when message_type = 'IN' then message_chat end) message_in,
       max(case when message_type = 'OB' then message_chat end) message_out
from data_added_rn
group by rn

demo

I have tried to comment each section appropriately.

Upvotes: 0

Sergey Menshov
Sergey Menshov

Reputation: 3906

Try the following query

SELECT
  q1.message_number in_num,
  q1.message_chat in_chat,
  q2.message_number out_num,
  q2.message_chat out_chat
FROM
  (
    SELECT *,@i1:=IFNULL(@i1,0)+1 num
    FROM Chat
    ORDER BY message_number
  ) q1
LEFT JOIN
  (
    SELECT *,@i2:=IFNULL(@i2,0)+1 num
    FROM Chat
    ORDER BY message_number
  ) q2
ON q2.num=q1.num+1 AND q2.message_type<>q1.message_type
WHERE q1.message_type='IN'

UNION ALL

SELECT
  q1.message_number in_num,
  q1.message_chat in_chat,
  q2.message_number out_num,
  q2.message_chat out_chat
FROM
  (
    SELECT *,@i3:=IFNULL(@i3,0)+1 num
    FROM Chat
    ORDER BY message_number
  ) q1
RIGHT JOIN
  (
    SELECT *,@i4:=IFNULL(@i4,0)+1 num
    FROM Chat
    ORDER BY message_number
  ) q2
ON q2.num=q1.num+1 AND q2.message_type<>q1.message_type
WHERE q2.message_type='OB'
  AND q1.message_type IS NULL

ORDER BY IFNULL(in_num,out_num)

SQL Fiddle - http://sqlfiddle.com/#!9/95a515/1

The second variant

SET @i1 = 0;
SET @i2 = 0;
SET @i3 = 0;
SET @i4 = 0;

-- the same query

SQL Fiddle - http://sqlfiddle.com/#!9/95a515/2

Or

SELECT 0,0,0,0 INTO @i1,@i2,@i3,@i4;

-- the same query

SQL Fiddle - http://sqlfiddle.com/#!9/95a515/5

Upvotes: 1

Tom
Tom

Reputation: 113

why not using a analytic function here? I would do it with Lead() like this:

with inc as ( 
--Do the incorporation in this block. could be subquery too 
--but its easier to read this way.

select 
case when message_type = 'IN' 
     then  message_chat 
     end as message_in

,case when LEAD(message_type) OVER (Order by message_number) = 'OB' --get the next message by number if it is type OB
    then LEAD(message_chat) OVER (order by message_number)   
    end as message_out
from input
)

select *
from inc 
where coalesce(message_in, message_out) is not null   --filter out rows where with in & out is null

Upvotes: 0

Related Questions