I am getting output in different format, here is my query:
SELECT
COUNT(lead_id) AS `leads`,
Month(FROM_UNIXTIME(`created_at`)) AS `Month`
FROM `lead`
WHERE YEAR(FROM_UNIXTIME(`created_at`)) = YEAR(CURDATE())
GROUP BY Month(FROM_UNIXTIME(`created_at`));
Output:
Month
312c31
322c31
332c31
342c31
352c31
362c31
372c31
382c31
392c31
31302c31
31312c31
31322c31
Required Output:
month
1,1
2,23
3,4
5,6
6,34
7,76
8,2
9,3
10,5
11,4
12,1
You can try this:
SELECT
CAST(
CONCAT(
MONTH(FROM_UNIXTIME(`created_at`)),
',',
COUNT(lead_ID)
) AS CHAR
) AS 'Month'
FROM lead
WHERE `created_at` BETWEEN UNIX_TIMESTAMP(DATE_FORMAT(NOW(), "%Y-01-01")) AND UNIX_TIMESTAMP(DATE_FORMAT(NOW(), "%Y-12-31"))
GROUP BY MONTH(FROM_UNIXTIME(`created_at`))
i got an answer thankyou for all you replies ur replies also an usefull information here is my query
SELECT
*,
concat(leads, ",", month)as leadss
FROM (
SELECT
COUNT(lead_id) as 'leads,
Month(FROM_UNIXTIME(`created_at`)) AS `Month`
FROM `lead`
WHERE YEAR(FROM_UNIXTIME(`created_at`)) = YEAR(CURDATE())
GROUP BY Month(FROM_UNIXTIME(`created_at`))
)AS ab
Related
I want to create a stats page for my website. However I have the problem that if I run my query it will not be merged if the sting is the same. Any idea how to fix this?
My query:
SELECT * FROM (
SELECT dj_1, count(*) AS uren
FROM dj_rooster
WHERE week = '28' GROUP BY id_1
ORDER BY count(*) DESC
) x
UNION ALL
SELECT * FROM (
SELECT dj_1, count(*) AS uren
FROM djpaneel_shows_uren
WHERE week = '28' AND active = '1'
GROUP BY dj_1
ORDER BY count(*) DESC
) x
My results:
Jack - 7
Jeremy - 5
Jack - 1
Thanks in advance for your help
There is no need to aggregate twice or more.
Use UNION ALL to get all the rows from the 2 tables and then aggregate:
SELECT t.dj_1, COUNT(*) AS uren
FROM (
SELECT dj_1 FROM dj_rooster
WHERE week = '28'
UNION ALL
SELECT dj_1 FROM djpaneel_shows_uren
WHERE week = '28' AND active = '1'
) t
GROUP BY t.dj_1
ORDER BY uren DESC
The below mentioned query is returning null but my inner query is fetching values like
SELECT CONCAT(
FLOOR(HOUR(time_milis) / 24), ' ',
MOD(HOUR(time_milis), 24), ':',
MINUTE(time_milis),':',
SECOND(time_milis))
FROM (
SELECT SUM(TIMEDIFF(logout_timestamp,login_timestamp)) AS time_milis FROM t_access_log WHERE logout_timestamp!='0000-00-00 00:00:00' GROUP BY login_id
)se
what am i missing here
Why not using DATE_FORMAT() for this? So my guess would be something like:
SELECT login_id ,
DATE_FORMAT(
SUM(
TIMEDIFF(
logout_timestamp,login_timestamp)
)
) as some_time
FROM t_access_log
WHERE logout_timestamp!='0000-00-00 00:00:00'
GROUP BY login_id;
Your time_milis are all seconds, but hour need a time like '10:05:03', you can just use sec_to_time. Like this:
SELECT concat(hour(sec_to_time(time_milis)), ' ', sec_to_time(time_milis))
FROM (
SELECT SUM(TIMEDIFF(logout_timestamp,login_timestamp)) AS time_milis FROM t_access_log WHERE logout_timestamp!='0000-00-00 00:00:00' GROUP BY login_id
)se
I'm trying to get a date value with:
- Current year
- Current month
- Day number from one select
the query
SELECT rat_data FROM rate_unita WHERE rateid = 1
as a result one INT value.
When I try to execute the following:
SELECT DATE(CONCAT(YEAR(NOW())),(MONTH(NOW())), (SELECT rat_data FROM rate_unita WHERE rateid = 1))
Mysql mark my syntax as incorrect near ' ( MONTH( NOW( ) ) ) , ( SELECT rat_data FROM rate_unita WHERE r
I think that there is something wrong with the way I'm calling the SELECT in the CONCAT, what is the correct query to reach my goal?
Thanks
L
You're closing the concat function too early. Remove some extra parentheis
SELECT DATE(CONCAT(YEAR(NOW()),
MONTH(NOW()),
(SELECT rat_data FROM rate_unita WHERE rateid = 1)
))
But you need to add some hyphens to make it a true date value
SELECT DATE(
CONCAT(
YEAR(NOW()),
'-',
MONTH(NOW()),
'-',
(SELECT rat_data FROM rate_unita WHERE rateid = 1)
)
)
This should result in a date of YEAR-MONTH-rat_data
Here's a working SQL Fiddle
Use DATE_FORMAT function. Here are a few examples:
http://www.w3schools.com/sql/func_date_format.asp
http://www.mysqltutorial.org/mysql-date_format/
Ok, so I have the following query:
SELECT MIN(`date`), `player_name`
FROM `player_playtime`
GROUP BY `player_name`
I then need to use this result inside the following query:
SELECT DATE(`date`) , COUNT(DISTINCT `player_name`)
FROM `player_playtime /*Use previous query result here*/`
GROUP BY DATE( `date`) DESC LIMIT 60
How would I go about doing this?
You just need to write the first query as a subquery (derived table), inside parentheses, pick an alias for it (t below) and alias the columns as well.
The DISTINCT can also be safely removed as the internal GROUP BY makes it redundant:
SELECT DATE(`date`) AS `date` , COUNT(`player_name`) AS `player_count`
FROM (
SELECT MIN(`date`) AS `date`, `player_name`
FROM `player_playtime`
GROUP BY `player_name`
) AS t
GROUP BY DATE( `date`) DESC LIMIT 60 ;
Since the COUNT is now obvious that is only counting rows of the derived table, you can replace it with COUNT(*) and further simplify the query:
SELECT t.date , COUNT(*) AS player_count
FROM (
SELECT DATE(MIN(`date`)) AS date
FROM player_playtime
GROUP BY player_name
) AS t
GROUP BY t.date DESC LIMIT 60 ;
The following query is working for me and returns the data in the format '1755|1755.jpg' :
SELECT (CAST(GROUP_CONCAT(CONCAT(photoId, '|', photoFileName)) AS CHAR(10000) CHARACTER SET utf8)) AS recentPhotoList
FROM
(
SELECT photoId, photoFileName
FROM photo
WHERE photoAlbumId = _albumId
AND photoPublishDate >= DATE_ADD(tStartDate, INTERVAL -20 MINUTE)
AND photoPublishDate <= tStartDate
ORDER BY photoPublishDate DESC
LIMIT 0,1
) as subQuery
);
However, by simply appending another column ('photoCaption') to the query, it then returns nothing?!? Why is this? Why is adding an additional column causing it to do this?
Here is the modified query that causes issues:
SELECT (CAST(GROUP_CONCAT(CONCAT(photoId, '|', photoFileName, '|', photoCaption)) AS CHAR(10000) CHARACTER SET utf8)) AS recentPhotoList
FROM
(
SELECT photoId, photoFileName, photoCaption
FROM photo
WHERE photoAlbumId = _albumId
AND photoPublishDate >= DATE_ADD(tStartDate, INTERVAL -20 MINUTE)
AND photoPublishDate <= tStartDate
ORDER BY photoPublishDate DESC
LIMIT 0,1
) as subQuery
);
I think that if photoCaption is null then the concat returns null. Use coalesce around photoCaption to solve this.
photoCaption is NULL i think.
Maybe you can check the results of your subquery to see what is going on.
SELECT CONCAT('bla', '|','-' , '|','|', 'bla', NULL) as foobar;
returns NULL
SELECT CONCAT('bla', '|','-' , '|','|', 'bla') as foobar;
returns bla|-||bla
Good luck