0

WHat i want is concate the static string to dataset i want to add string as min, avg, max to the fetched data. when i m trying give some error?

my query

select m.medTitle, 
min(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime
from tbldoctormediatracktest s
left join tblmedia m on s.dmtMedId = m.medId
group by m.medTitle
union all
select m.medTitle, 
avg(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime
from tbldoctormediatracktest s
left join tblmedia m on s.dmtMedId = m.medId
group by m.medTitle
union all
select m.medTitle, 
  max(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime
from tbldoctormediatracktest s
left join tblmedia m
  on s.dmtMedId = m.medId
group by m.medTitle
order by medTitle,  playtime;

what i am trying.

select concat(m.medTitle,'minimum'), 
  min(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime
from tbldoctormediatracktest s
left join tblmedia m
  on s.MedId = m.MedId
group by m.medTitle
union all

select concat(m.medTitle,'average'), 
  avg(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime
from tbldoctormediatracktest s
left join tblmedia m
  on s.MedId = m.MedId
group by m.medTitle
union all

select concat(m.medTitle,'maximum'),  
  max(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime
from tbldoctormediatracktest s
left join tblmedia m
  on s.MedId = m.MedId
group by m.medTitle
order by medTitle,  playtime
4
  • Both your queries doesn't compile. Either post a valid query and state what you want to change, or show us the tables structure and the requested output. Commented Sep 18, 2012 at 5:18
  • Can you tell us the error you got? Commented Sep 18, 2012 at 5:20
  • see my this post stackoverflow.com/questions/12465825/… and sqlfiddle.com/#!2/cf37a/9 in this example first records for casfung are there i just want to add min, avg , max to casfung result date like casfung(min) casfung(avg) casfung(max) Commented Sep 18, 2012 at 5:21
  • Error Unknown column 'medTitle' in 'order clause': select concat(m.medTitle,'minimum'), min(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime from tbldoctormediatracktest s left join tblmedia m on s.MedId = m.MedId group by m.medTitle union all select concat(m.medTitle,'average'), avg(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime from tbldoctormediatracktest s left Commented Sep 18, 2012 at 5:27

2 Answers 2

2

try this:

select playtime,type 
from(
select m.MedId, 
  min(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime,
  concat(m.medTitle,' (minimum)') as type
from tbldoctormediatracktest s
left join tblmedia m
  on s.MedId = m.MedId
group by m.MedId
union all

select m.MedId, 
  avg(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime,
  concat(m.medTitle,' (average)') as type
from tbldoctormediatracktest s
left join tblmedia m
  on s.MedId = m.MedId
group by m.MedId
union all

select m.MedId,  
  max(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime,
  concat(m.medTitle,' (maximum)') as type 
from tbldoctormediatracktest s
left join tblmedia m
  on s.MedId = m.MedId
group by m.MedId)a
order by MedId,  playtime


SQL Fiddle domo 1

SQl Fiddle Demo 2

SQL Fiddle Demo 3

Sign up to request clarification or add additional context in comments.

2 Comments

can we group by and order by m.MedId?
you want 78 Casfung (maximum) in the ouptput?
0
select concat(m.medTitle, 'min') medTitle,
  min(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime
from tbldoctormediatracktest s
left join tblmedia m
  on s.MedId = m.MedId
group by m.medTitle

union all

select concat(m.medTitle, 'avg') medTitle, 
  avg(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime
from tbldoctormediatracktest s
left join tblmedia m
  on s.MedId = m.MedId
group by m.medTitle

union all

select m.medTitle || 'max' medTitle, 
  max(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime
from tbldoctormediatracktest s
left join tblmedia m
  on s.MedId = m.MedId
group by m.medTitle
order by medTitle, playtime

Updated your query from the link. I noticed that you need to give the alias for m.medTitle after concatenating.

Any of concat(m.medTitle, 'min') or m.medTitle || 'min' can be used for concatenation.

4 Comments

this min max avg should add as suffix. suppose first record comes as 'boxer' then min should be added to boxer like boxermin. sqlfiddle.com/#!2/27a4b/8
some how you got my problem but not returning last query value max
You can update the concatenated string. I did 'min' for all the three queries. Anyway, updated my answer again.
can we group by and order by m.MedId?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.