I have a mysql table like this with these sample data
I write this query
SELECT SUM(amount),DATE(date) FROM outgoings WHERE outgoings_type_id = '1'GROUP BY amount
I got the output like this
I want it not like this but like this(I did some photoshop edit to above one!)
The only change is I want the summation of amounts which are on same date..Others are normal way..Is it possible or not with some changes to my query..?
Try this, you do your sum(amount) and group by date.
SELECT SUM(amount),DATE(date) FROM outgoings WHERE outgoings_type_id = '1'GROUP BY DATE(date)
Your aggregate (sum or whatever) will be grouped by the date field then.
Also, I don't think you need to do DATE(date)
SELECT SUM(amount),date FROM outgoings WHERE outgoings_type_id = '1'GROUP BY date
Related
I wish to sort my table with date order so that recently added data will be on the top of the table.
I have used query for sorting as:
select date from register_table order by date desc.
Currently table display data as:
date
02.04.2019
05.04.2019
09.04.2019
10.04.2019
06.02.2019
23.01.2019
11.01.2019
I expect my table to display as:
date
10.04.2019
09.04.2019
05.04.2019
02.04.2019
06.02.2019
23.01.2019
11.01.2019
How to display data in date order?
Your fundamental problem is not storing the date as a date. You should fix that.
For the query to work, use:
order by str_to_date(date, '%m.%d.%Y')
To fix the data, you can do:
update register_table
set date = str_to_date(date, '%m.%d.%Y');
alter table register_table
modify date date;
You can see how this works here.
I don't know why your date is stored like that but here, give this a try:
SELECT date FROM date ORDER BY STR_TO_DATE(REPLACE(date,'.','-'),'%d-%m-%Y') DESC;
If you want to see what exactly happen, run this query:
SELECT date,STR_TO_DATE(REPLACE(date,'.','-'),'%d-%m-%Y') FROM date;
In case you still don't quite understand, refer to MySQL STR_TO_DATE function and MySQL REPLACE function.
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 have a MySql table with name customer and in this, i have a column with name callback_date. It is a varchar field. And I want to get its data as DATE.
Here is how I make my query.
SELECT callback_date AS calld
FROM customer
WHERE callback_date<>''
AND STR_TO_DATE('calld', '%d/%m/%Y')
Why my data is not returning in date format of this particular column? Any specific query I need to run?
don't know what exactly you want but maybe this is what I understand, query should be:
SELECT temp.*
FROM
(SELECT callback_date AS calld,
DATE_FORMAT(callback_date, '%d/%m/%Y') AS dt
FROM customer
WHERE callback_date <> '') AS temp
WHERE temp.dt = '18/03/2016'
I am trying to Sum() the column Status where Status = 'operational'. I am having trouble figuring out how to sum the actual word "operational".
I have tried multiple different variations of the statement below (the one I posted is the most basic form) but I get the error: data type varchar is invalid for sum operator.
Can anybody help?
SELECT SUM(status) As 'TotalOperationalSTIDevices'
FROM netinfo_device_details
WHERE LoopBackAddress LIKE '10.12%' AND Status = 'Operational'
Try
Select COUNT(*) As 'TotalOperationalSTIDevices' from netinfo_device_details where LoopBackAddress Like '10.12%' and Status = 'Operational'
You need to use COUNT:
SELECT COUNT(*) As TotalOperationalSTIDevices
FROM netinfo_device_details
WHERE LoopBackAddress LIKE '10.12%' AND Status = 'Operational';
The SUM aggregation function really does a SUM of a set of numbers. COUNT just counts the number of rows.
Since the actual content of the row is not relevant, you can use COUNT(*) instead of COUNT(status) if you want.
So i have this query that is supposed to return some information like price, open, close, etc.. for the LATEST entry.
SELECT * FROM History WHERE symbol = $symbol Having max(`date`);
But for whatever reason its not returning the correct information. It for some reason believes that the max date is 2013-02-14, but if i look at all the data the most recent is actually 2013-02-27.
in this particular case
$symbol = "AGNC"
Does the max function not work on date?
I think you want to have something like this. I'm not fan of doing this using ORDER BY clause because there are some possibilities (although much little) that records may have the same latest date entry.
SELECT *
FROM History
WHERE symbol = $symbol AND
date = (SELECT MAX(date) FROM History)
SQLFiddle Demo
My suggestion would be not using MAX at all; if what you want to do is only fetching the latest entry in the table, you could try:
SELECT * FROM History WHERE symbol = $symbol ORDER BY date DESC LIMIT 1;
This works perfectly as long as your date field is actually declared as a date-related MySQL field type, like date, datetime, etc.
As JW pointed out in his answer, you may want to add more fields in your ORDER BY statement to disambiguate records that have the exact same date.
Also, you may want to put $symbol in quotes if that's a string field (char, varchar, etc):
SELECT * FROM History WHERE symbol = '$symbol' ORDER BY date DESC LIMIT 1;
Cheers!
The following should do what you want:
SELECT *
FROM History h
WHERE symbol = $symbol and
`date` = (select max(`date`) from History where symbol = $symbol)
You may use alias table name:
SELECT *
FROM History
WHERE symbol = $symbol AND
date = (SELECT MAX(date) FROM History);