When querying my MySQL database, I use the line
SELECT `playlistDate` from `tracks` GROUP BY `playlistDate`
The JSON-ified result statement is this
[{"playlistDate":"2020-09-14T05:00:00.000Z"},{"playlistDate":"2020-09-21T05:00:00.000Z"},
{"playlistDate":"2020-09-28T05:00:00.000Z"},{"playlistDate":"2020-10-05T05:00:00.000Z"},
{"playlistDate":"2020-10-12T05:00:00.000Z"},{"playlistDate":"2020-10-19T05:00:00.000Z"},
{"playlistDate":"2020-10-26T05:00:00.000Z"},{"playlistDate":"2020-11-02T06:00:00.000Z"}
I want it to look like this, without the timestamps
[{"playlistDate":"2020-09-14"},{"playlistDate":"2020-09-21"},
{"playlistDate":"2020-09-28"},{"playlistDate":"2020-10-05"},
{"playlistDate":"2020-10-12"},{"playlistDate":"2020-10-19"},
{"playlistDate":"2020-10-26"},{"playlistDate":"2020-11-02"}
I've tried changing my query to either of these with no luck
SELECT DATE(`playlistDate` from `tracks` GROUP BY `playlistDate`)
SELECT DATE(`playlistDate`) from `tracks` GROUP BY `playlistDate`
How can I use the DATE() function to get the results I am expecting?
The ugly way to do it is using substr(playlistDate, 1, 10).
You also can use DATE_FORMAT(playlistDate,'%y-%m-%d')
Assuming playlistDate is a string, you can use STR_TO_DATE.
SELECT distinct(STR_TO_DATE(playlistDate,'%Y/%m/%d')) from tracks
If its a date:
SELECT distinct(DATE_FORMAT(playlistDate,'%Y/%m/%d')) from tracks
Related
I have a table as follows:
log (log_id, log_success (bool), log_created)
I would like to SELECT and return 3 columns date success and no_success, where the former does not exist in table and finally aggregate them by day.
I have created this query:
SELECT
log_created as 'date'
COUNT(*) AS 'count',
SUM(log_success) AS 'success'
SUM('count' - 'success') AS 'no_success'
FROM send_log
GROUP BY DATE_FORMAT(log_created, '%Y-%m-%d');
Would I be able to achieve it with this query? Is my syntax correct?
Thanks.
You can't reuse an alias defined in the select within the same select clause. The reason for this is that it might not even have been defined when you go to access it. But, you easily enough can repeat the logic:
SELECT
log_created AS date,
SUM(log_success) AS success,
COUNT(*) - SUM(log_success) AS no_success,
FROM send_log
GROUP BY
log_created;
I don't know why you are calling DATE_FORMAT in the group by clause of your query. DATE_FORMAT is usually a presentation layer function, which you call because you want to view a date formatted a certain way. Since it appears that log_created is already a date, there is no need to call DATE_FORMAT on it when aggregating. You also should not even need in the select clause, because the default format for a MySQL date is already Y-m-d.
You must select DATE_FORMAT(log_created, '%Y-%m-%d') if you want to group by this.
Also you can get the no_success counter with SUM(abs(log_success - 1))
SELECT
DATE_FORMAT(log_created, '%Y-%m-%d') date,
SUM(log_success) log_success,
SUM(abs(log_success - 1)) no_success
FROM send_log
GROUP BY date;
See the demo
I received wonderful help in answering a question yesterday that lead me to successfully use SELECT COALESCE in a MySQL database to replace a nested excel formula. What i'm now struggling with is using the SELECT COALESCE in conjunction with using the SELECT command in the same query. All my columns are coming from the same table (best_estimate) but what I would like to do is:
SELECT 'OFS_ID' from best_estimate
and also perform the following:
SELECT COALESCE(FINAL_TOTAL, INITIAL_TOTAL, CDSI_TOTAL, JCE_TOTAL, 0 ) AS my_value
FROM best_estimate
so the end result will result in two columns -
OFS_ID and MY_VALUE
I'm just not sure how to join these two different commands - I spent three hours working on it yesterday and I keep getting syntax errors saying that I can't use SELECT in that place in the query.
Thanks in advance for the help!
Have you tried
SELECT OFS_ID,COALESCE(FINAL_TOTAL, INITIAL_TOTAL, CDSI_TOTAL, JCE_TOTAL, 0 ) AS my_value FROM best_estimate
Try this:-
SELECT `OFS_ID`, COALESCE(FINAL_TOTAL, INITIAL_TOTAL, CDSI_TOTAL, JCE_TOTAL, 0 ) AS my_value
FROM best_estimate
Trying to get a check sum of results of a SELECT statement, tried this
SELECT sum(crc32(column_one))
FROM database.table;
Which worked, but this did not work:
SELECT CONCAT(sum(crc32(column_one)),sum(crc32(column_two)))
FROM database.table;
Open to suggestions, main idea is to get a valid checksum for the SUM of the results of rows and columns from a SELECT statement.
The problem is that CONCAT and SUM are not compatible in this format.
CONCAT is designed to run once per row in your result set on the arguments as defined by that row.
SUM is an aggregate function, designed to run on a full result set.
CRC32 is of the same class of functions as CONCAT.
So, you've got functions nested in a way that just don't play nicely together.
You could try:
SELECT CONCAT(
(SELECT sum(crc32(column_one)) FROM database.table),
(SELECT sum(crc32(column_two)) FROM database.table)
);
or
SELECT sum(crc32(column_one)), sum(crc32(column_two))
FROM database.table;
and concatenate them with your client language.
SELECT SUM(CRC32(CONCAT(column_one, column_two)))
FROM database.table;
or
SELECT SUM(CRC32(column_one) + CRC32(column_two))
FROM database.table;
So I have a data with format like ;1;;2; and then I need to use this number in a query so I thought I'd convert it to 1,2 and use that in a IN condition. In my table, the result should return 2 rows but instead it is returning only 1 row.
My query is like this. The subquery return 1,2 with no problem but only 1 row is retrieve.
select *
from wt_lists
where id IN ((select replace (replace(sendto, ';;',','),';','')
from wt_stats where statsid IN (1)))
But when I try it with this. It returns the correct result, which in my case is 2 rows.
select *
from wt_lists
where id IN (1,2)
What am I missing here?
Comma delimited strings need to be explicitly defined in the query in order to be used in the IN clause - there's countless examples on SO where people need to use dynamic SQL to incorporate user submitted comma delimited strings.
That said, I have a solution using the FIND_IN_SET function:
SELECT DISTINCT wl.*
FROM WT_LISTS wl
JOIN (SELECT REPLACE(REPLACE(ws.sendto, ';;',','),';','') AS ids
FROM WT_STATS ws
WHERE ws.statsid = 1) x ON FIND_IN_SET(wl.id, x.ids) > 0
You are replacing the string:
';1;;2;'
To:
'1,2'
So, you SQL query looks like:
select * from wt_lists where id IN ('1,2') from wt_stats where statsid IN (1)
To use IN clause you need select different values in different rows.
I found this store procedure that does exactly what you need.
http://kedar.nitty-witty.com/blog/mysql-stored-procedure-split-delimited-string-into-rows/
I have not tested, but it is the way.
Obs: Like David said in the comments above, parsing the data in your application is a better way to do this.
I have a mysql query, something like this:
SELECT users*100 as totalusers, totalusers*90 AS totalsalerys FROM table
As you can see I want to reuse the totalusers when calculating totalsalerys so I son't have to write the same code again. That dosen't seem to work in mysql, is there another easy way to do this?
My query is just an example, change the *100 and *90 to some very long formula and you might see what i mean..
SELECT (#r := users * 100) as totalusers,
#r * 90 AS totalsalerys
FROM table
You can also use a subquery as #Tom Ritter advices, but it's not friendly to ORDER BY ... LIMIT ... clause.
In MySQL, all results of the inner subquery will be fetched before applying any filters in the outer subquery.
I believe you would have to copy/paste the formula or use a subquery. The below would work in T-SQL, but I imagine it'd work in MySQL as well since it's pretty simple.
SELECT
x.totalusers, x.totalusers*90 AS totalsalerys
FROM (
SELECT users*100 as totalusers FROM table
) x