6

I have one table called datas in there I have id,start_date,end_date,day_count,day_type.
Where id is primary key,start and end_date is datetime, day_count is int and day_type is varchar. Now day_type store DAY,WEEK,MONTH as value when user made request and day field hold number of days like 1 to 60.

Now I want to user this count and type in date_add mysql built-in function.

I have tried to pass it as below but its show error.

SELECT
datas.day_count,
datas.day_type,
MIN( datas.start_date ) AS MinDate,
MAX( datas.end_date ) AS MaxDate, 
DATE_ADD(MaxDate,INTERVAL datas.day_count datas.day_type) AS ExactDate,
datas.trade_service_id
FROM datas

Erro is You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'datas.day_type)) AS ExactDate, datas' at line 13 Please little hint would be great.

2
  • 1
    Please edit the question and add the error you are getting. Commented Aug 22, 2012 at 7:10
  • 2
    Same as stackoverflow.com/questions/888971/… Commented Aug 22, 2012 at 7:11

1 Answer 1

15

This query works in MySQL but what do you exactly want to output without Group and so on?

SELECT
datas.day_count,
datas.day_type,
MIN( datas.start_date ) AS MinDate,
MAX( datas.end_date ) AS MaxDate,

start_date, min(start_date) as min_date,
case day_type 
  when 'MONTH' then DATE_ADD(MAX( datas.end_date ),INTERVAL datas.day_count MONTH) 
  when 'DAY' then DATE_ADD(MAX( datas.end_date ),INTERVAL datas.day_count DAY) 
end as ExactDate,

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

1 Comment

exactly what I needed to know, that I can't use a field as the interval type. CASE works fine as an alternative.

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.