how to get the results from two tables in mysql - mysql

SELECT month,Avg(rain_fall) FROM rain_fall WHERE
STR_TO_DATE( CONCAT('01','-',month,'-',year) , '%d-%b-%Y')
between
STR_TO_DATE('01-Jan-1991','%d-%b-%Y') and
STR_TO_DATE('01-Dec-1993','%d-%b-%Y')
group by month
order by MONTH(STR_TO_DATE( CONCAT('01','-',month,'-',year) , '%d-%b-%Y'))
UNION
SELECT month,Avg(data_value) FROM temprature_data WHERE
STR_TO_DATE( CONCAT('01','-',month,'-',year) , '%d-%b-%Y')
between
STR_TO_DATE('01-Jan-1991','%d-%b-%Y') and
STR_TO_DATE('01-Dec-1993','%d-%b-%Y')
group by month
order by MONTH(STR_TO_DATE( CONCAT('01','-',month,'-',year) , '%d-%b-%Y')) ;
The above is my sql query. I want to get the average value against all months in a year of temprature_data table and rain_fall table for the selected years.
The error message received:
#1221 - Incorrect usage of UNION and ORDER BY

Use Parenthesis on each select query and UNION ALL:
P.S.: month is a reserved word. If here, you're using it a column name, try to change it to something else so it may not make a conflict in your later queries. Check this list of reserved words in MySQL
(SELECT
month,
Avg(rain_fall)
FROM
rain_fall
WHERE
STR_TO_DATE( CONCAT('01','-',month,'-',year) , '%d-%b-%Y')
between
STR_TO_DATE('01-Jan-1991','%d-%b-%Y')
and
STR_TO_DATE('01-Dec-1993','%d-%b-%Y')
group by
month
order by
MONTH(STR_TO_DATE( CONCAT('01','-',month,'-',year) , '%d-%b-%Y'))
)
UNION ALL
(
SELECT
month,
Avg(data_value)
FROM
temprature_data
WHERE
STR_TO_DATE( CONCAT('01','-',month,'-',year) , '%d-%b-%Y')
between
STR_TO_DATE('01-Jan-1991','%d-%b-%Y')
and
STR_TO_DATE('01-Dec-1993','%d-%b-%Y')
group by
month
order by
MONTH(STR_TO_DATE( CONCAT('01','-',month,'-',year) , '%d-%b-%Y'))
);

Related

Select last n record within group by year month

I have a table where I need to get the last 12 records which is grouped by year() month() ASC. I try to use the query below but the result is not as expected.
SELECT * FROM
(
SELECT
id,
tanggal,
date_format(tanggal,'%b-%Y') as bulan,
sum(sisa_pokok) as jumlah
FROM transaksi_detail
GROUP BY date_format(tanggal,'%b-%Y')
ORDER BY id DESC LIMIT 12
) sub
ORDER BY id ASC
the query result is as below
My expected result is sort by bulan column order by year(), month() as follows
Bulan jumlah
Mar-2018 26600000
Oct-2017 1000000
Sept-2017 4500000
and so on....
EXTRACT(YEAR FROM bulan) as year
SELECT EXTRACT(YEAR FROM tanggal) as year , EXTRACT(MONTH FROM tanggal) as month, id FROM table_name group by year order by month
you can get year same like you can get month after that put group by and order i hope it will help you
This works for my situation
SELECT * FROM
(
SELECT
id,
tanggal,
month(tanggal),
year(tanggal),
date_format(tanggal,'%b-%Y') as bulan,
sum(sisa_pokok) as jumlah
FROM transaksi_detail
GROUP BY date_format(tanggal,'%b-%Y')
ORDER BY id DESC LIMIT 12
) sub
ORDER BY year(tanggal), month(tanggal) ASC

Simple query to find the employee id, date and minimum_time and maximum_time order by id,date,time

How can i find the employee id, date and minimum_time and maximum_time order by id,date,time from this table?
SELECT emp_id,
date,
MIN(time) AS in_time,
MAX(time) AS out_time
FROM yourTable
GROUP BY emp_id,
date
ORDER BY emp_id, date
The MIN() and MAX() functions will find the earliest and latest time of each employee, on each date.
It's simple your query syntax would be like
SELECT atr1, atr2,.. FROM table_name ORDER BY atr1, atr2,..
Try:
SELECT `emp_id` , `date` , MIN( `time` ) as in_time , MAX( `time` ) as out_time
FROM `Table`
WHERE 1
GROUP BY `date`
ORDER BY `time` DESC
LIMIT 0 , 30
As i can see there are same emp_id for many dates, so group by date will get all data for each date.
You can just use the SELECT Query
SELECT employee.id, employee.date, employee.minimum_time, employee.maximum_time
From table_name
ORDER BY employee.id

sql table: sum up over intervals of 10 seconds

I'm looking for a solution for my statistical problem:
I have a table with one date column in it. Now what I want to do is count the number of rows in a certain time interval of 10 seconds:
SELECT count(id), ... AS interval WHERE ... GROUP BY interval
Anybody an idea how I can perform this?
Thanks,
Markus
SELECT
CONCATENATE(
SUBSTR(
DATE_FORMAT(yourtimestamp, '%Y%m%d%H%i%s')
, 1, -1)
, 0) AS time_bin,
COUNT(*)
FROM yourtable
WHERE ...
GROUP BY CONCATENATE(
SUBSTR(
DATE_FORMAT(yourtimestamp, '%Y%m%d%H%i%s')
, 1, -1)
, 0)
(use str_to_date() to convert back to a timestamp)
or...
SELECT FROM_UNIXTIME(
10*FLOOR(UNIX_TIMESTAMP(yourtimestamp)/10)
) AS time_bin,
COUNT(*)
FROM yourtable
WHERE ...
GROUP BY FROM_UNIXTIME(
10*FLOOR(UNIX_TIMESTAMP(yourtimestamp)/10)
)

MYSQL group by day, week and month with php timestamp [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MySQL Query GROUP BY day / month / year
I have php timestamps (e.g. 1307362819) stored in a column in my database. I want to group count(*) in days, weeks and months the data.
For example I want to find out how many entries there are per day, per week, and per month etc.
How can this be achieved?
You can subtract day, week and month value from a timestamp, and group the subtracted values.
grouping by day value:
select count(*) from table group by from_unixtime(timeStampColumn, '%Y%m%d')
grouping by week value:
select count(*) from table group by from_unixtime(timeStampColumn, '%Y%m%u')
grouping by monthvalue:
select count(*) from table group by from_unixtime(timeStampColumn, '%Y%m')
For more information, have a look at this page: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
Why don't you use these: <>
SELECT COUNT(*) FROM yourTable WHERE yourTimestampField > someTimestampAWeekPast;
SELECT COUNT(*) FROM yourTable WHERE yourTimestampField > someTimestampADayPast;
Use the mysql date/time functions http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html to calculate the desired date in the past.
Use UNIX_TIMESTAMP if you stored them as int or string or whatever.
here a good exemple for you
to group by week
EDIT:
try this
select
id_user,
year(time) as AYear, week(time) as AWeek, day(time) as Aday ,
count(week(time)) as TotalPerWeek , count(day(time)) as TotalPerDay ,
count(year(time)) as TotalPerYear
from yourtable
where id_user = 16 //// the user u want to check
group by id_user, AYear, AWeek , Aday
order by AYear, AWeek , Aday
SELECT MONTH( FROM_UNIXTIME( `timeStamp` ) ) , COUNT( `id` )
FROM `discusComments`
GROUP BY MONTH( FROM_UNIXTIME( `timeStamp` ) )

MySQL select only 1 per match

I have a MySQL table with 5 rows:
email
message_id
date
time
And I would like to count the number of emails there is per days.
So far I have this
SELECT Date,COUNT(*) AS Num FROM mail2_mailing_log WHERE Id_message=#Id GROUP BY Date ORDER BY Date DESC
But we found out that there was a problem with a script and many data got multiplied (Which have since been fixed), but I would like to be able to use the data I have.
So basically I would want to "merge" all the rows that match per email, date and time and then group by date and count the number of items.
SELECT q.date, COUNT(*)
FROM (SELECT DISTINCT email, date, time
FROM mail2_mailing_log
WHERE Id_Message = #Id) q
GROUP BY q.date
ORDER BY q.date DESC
Use the distinct keyword:
SELECT DISTINCT Date,COUNT(*) AS Num FROM mail2_mailing_log WHERE Id_message=#Id GROUP BY Date ORDER BY Date DESC
If the Date column contains the time too, then you will need to format the Date column using the DATE_FORMAT() function.
SELECT DATE_FORMAT(Date, '%W %M %Y'),COUNT(*) AS Num FROM mail2_mailing_log
WHERE Id_message=#Id GROUP BY DATE_FORMAT(Date, '%W %M %Y')
ORDER BY DATE_FORMAT(Date, '%W %M %Y') DESC