Query to check certain date & display one date values only - mysql

There is a table in that one date column having so many entry of totalamount at different-different time of same date but I don't worry about time. I need to find certain date all totalamount, along with username.
When I am using below query. It shows syntax error
select username,totalamount,date from bill where date = '2013-04-12'
In the above query date is column name
Please help me.

if you want to find all records on a given DATE when there is DATETIME saved in the db, use this method
SELECT username, totalamount, `date`
FROM bill
WHERE `date` BETWEEN '2013-04-12 00:00:00' AND '2013-04-12 23:59:59'
If you want sum of total amount for each user on a given date
SELECT username, SUM(totalamount)
FROM bill
WHERE `date` BETWEEN '2013-04-12 00:00:00' AND '2013-04-12 23:59:59'
GROUP BY 1
If you want sum of total amount for given date
SELECT DATE(`date`), SUM(totalamount)
FROM bill
WHERE `date` BETWEEN '2013-04-12 00:00:00' AND '2013-04-12 23:59:59'
GROUP BY 1

select username,SUM(amount), DATE_FORMAT(date, '%Y-%m-%d') date_only
from bill
where date = str_to_date('2013-04-12','%Y-%m-%d)
group by DATE_FORMAT(date, '%Y-%m-%d'), username
Try this.

Related

Avg function not returning proper value

I expect this query to give me the avg value from daily active users up to date and grouped by month (from Oct to December). But the result is 164K aprox when it should be 128K. Why avg is not working? Avg should be SUM of values / number of current month days up to today.
SELECT sq.month_year AS 'month_year', AVG(number)
FROM
(
SELECT CONCAT(MONTHNAME(date), "-", YEAR(DATE)) AS 'month_year', count(distinct id_user) AS number
FROM table1
WHERE date between '2020-10-01' and '2020-12-31 23:59:59'
GROUP BY EXTRACT(year_month FROM date)
) sq
GROUP BY 1
Ok guys thanks for your help. The problem was that on the subquery I was pulling the info by month and not by day. So I should pull the info by day there and group by month in the outer query. This finally worked:
SELECT sq.day_month, AVG(number)
FROM (SELECT date(date) AS day_month,
count(distinct id_user) AS number
FROM table_1
WHERE date >= '2020-10-01' AND
date < '2021-01-01'
GROUP BY 1
) sq
GROUP BY EXTRACT(year_month FROM day_month)
Do not use single quotes for column aliases!
SELECT sq.month_year, AVG(number)
FROM (SELECT CONCAT(MONTHNAME(date), '-', YEAR(DATE)) AS month_year,
count(distinct id_user) AS number
FROM table1
WHERE date >= '2020-10-01' AND
date < '2021-01-01'
GROUP BY month_year
) sq
GROUP BY 1;
Note the fixes to the query:
The GROUP BY uses the same columns as the SELECT. Your query should return an error (although it works in older versions of MySQL).
The date comparisons have been simplified.
No single quotes on column aliases.
Note that the outer query is not needed. I assume it is there just to illustrate the issue you are having.

Get SUM of values in a column after query is ran

This is my SQL statement which pulls back all the fills I've had in a certain time frame. Is there a way to get the list to come up and also pull the SUM of all of them?
SELECT customerName, date, gallons
FROM addFill
WHERE date >= CONVERT(datetime, '3-3-2015' )
ORDER BY CONVERT(DATE, date) ASC
First, I would write your query as:
SELECT customerName, date, gallons
FROM addFill
WHERE date >= '2015-03-03'
ORDER BY date ASC;
I see no value in ordering by the date and not the date/time component. Also, you might as well just use a recognizable date format for the comparison.
If you want the sum as well, then that is tricky. One method uses rollup:
SELECT customerName, date, SUM(gallons) as gallons
FROM addFill
WHERE date >= '2015-03-03'
GROUP BY customerName, date with rollup
sums of fills per customer since '3-3-2015'
SELECT customerName, date, sum(gallons)
FROM addFill
WHERE date >= CONVERT(datetime, '3-3-2015' )
GROUP BY customerName
sum of fills for all customers since '3-3-2015'
SELECT sum(gallons)
FROM addFill
WHERE date >= CONVERT(datetime, '3-3-2015' )

WHERE returning too many rows for data NOT BETWEEN two dates

I want to return data that is not between current date and the last 7 days.
My SELECT statement appears ok, but it is also returning the current day's data.
SELECT
customer.id AS id,
customer.customer_id AS customer_id,
customer.name AS name,
customer.phone1 AS phone1,
customer.location_area AS location_area,
sales.post_date AS post_date
FROM
sales
INNER JOIN
customer
ON
sales.customer_id = customer.customer_id
WHERE
post_date
NOT BETWEEN
CAST( DATE_SUB(NOW(), INTERVAL 7 DAY) AS DATE )
AND
CAST( NOW() AS DATE )
ORDER BY
sales.id
DESC
LIMIT 30
Please note the customer_id field used in the ON clause is not a primary key in any of the two referenced tables.
What might be missing in my query?
This problem is usually confusion about the different meanings of DATE datatypes on the one hand and TIMESTAMP or DATETIME data types on the other.
Let's say NOW() is 1-April-2017 09:35. And, let's say you have a row in your sales table with a post_date value of 1-April-2017 08:20. Let's say your post_date column has the data type DATETIME.
Then your WHERE clause looks like this after values are applied.
WHERE '2017-04-01 08:20' NOT BETWEEN CAST( '2017-03-25 09:35' AS DATE )
AND CAST( '2017-04-01 09:35' AS DATE )
Applying the CAST operations, we get.
WHERE '2017-04-01 08:20' NOT BETWEEN '2017-03-25'
AND '2017-04-01'
Finally, when comparing a DATETIME or TIMESTAMP to a DATE value, the DATE value is interpreted as having a time of midnight. So your query looks like this:
WHERE '2017-04-01 08:20' NOT BETWEEN '2017-03-25 00:00:00'
AND '2017-04-01 00:00:00'
And, guess what? '2017-04-01 08:20' is after '2017-04-01 00:00:00'.
What you need is this:
WHERE
NOT (
post_date >= CURDATE() - INTERVAL 7 DAY --on or after midnight 2016-3-25
AND post_date < CURDATE() + INTERVAL 1 DAY --before midnight 2016-04-02
)
Please notice that this expression encompasses eight days total.
You can't use BETWEEN for this kind of comparison because you need < for the end of the range, and BETWEEN uses <= for the ends of all its ranges.
Also, CURDATE() is much easier to read than CAST(NOW() AS DATE).

Select sum of payments in period where first payment was made in that period

I have table of payments 'user_id - created - price'.
I need to calculate sum of all payments made in period 14-16 of March where first payment was made in the same period
The best solution I came by is
SELECT user_id, SUM(price/100) FROM payment WHERE
(DATE(created) BETWEEN '2014-03-14' AND '2014-03-16')
AND (MIN(created) BETWEEN '2014-03-14 00:00:01' AND '2014-03-16 23:59:59')
GROUP BY user_id
but I got "Invalid use of group function"
Update:
This request solved my problem
SELECT p.user_id, SUM(p.price/100) FROM payment p WHERE
(DATE(created) BETWEEN '2014-03-14' AND '2014-03-16')
AND (SELECT MIN(DATE(created)) FROM payment WHERE user_id = p.user_id) BETWEEN '2014-03-14' AND '2014-03-16'
GROUP BY p.user_id
You can't use MIN() function in where clause,you need to use HAVING to filter on aggregate functions you can rewrite your query as below
SELECT user_id, SUM(price/100)
FROM payment
WHERE
created BETWEEN '2014-03-14 00:00:00' AND '2014-03-16 23:59:59'
GROUP BY user_id
HAVING MIN(created) BETWEEN '2014-03-14 00:00:00' AND '2014-03-16 23:59:59'

querying an int value and pulling a date range

I am trying to select all orders from this date 2013-12-13. The information is in integer value. The query will run but is not giving me any info.
SELECT FROM_UNIXTIME(modified)
FROM orders
WHERE (modified BETWEEN '2013-12-13 00:00:00' AND '2013-12-14 00:00:00')
SELECT FROM_UNIXTIME(modified)
FROM orders
WHERE modified BETWEEN UNIX_TIMESTAMP('2013-12-13 00:00:00') AND UNIX_TIMESTAMP('2013-12-14 00:00:00')