I have three tables. customers, DVDs, Movies. This is the last report I need to produce, and it's tripping me up. I currently have a field: dvd.Due_Date, which is the date that the product is due back. I need to retrieve all files where the Due Date is 15 days past the current date.
This is what I have so far:
SELECT customer.customer_id,
customer.customer_fname,
customer.customer_lname,
customer.customer_phone,
customer.customer_email,
dvd.DVD_ID, movie.Movie_Title,
dvd.Rental_Date, dvd.Due_Date
FROM customer INNER JOIN dvd
ON customer.customer_id = dvd.customer_id
INNER JOIN movie ON dvd.Movie_ID = movie.Movie_ID
WHERE DATEDIFF(Due_Date, CURDATE() ) > 15
I'm not getting any errors, I'm just not getting any results back, even though I have multiple items listed as due date of Feb. 10th. I do get all of the information I want if I remove everything past the WHERE statement, so I know that is working at least.
For DATEDIFF if the first item is a smaller date than the second item then it returns a negative number (as such could never be larger than 16) and not a positive one. So flip them, you want the later date as the first argument:
... WHERE DATEDIFF( CURDATE(), Due_Date ) > 15
I'm not sure what you mean by "all files where the Due Date is 15 days past the current date." However, try using logic like this:
SELECT c.customer_id, c.customer_fname, c.customer_lname, c.customer_phone, c.customer_email, d.DVD_ID, m.Movie_Title, d.Rental_Date, d.Due_Date
FROM customer c INNER JOIN
dvd d
ON c.customer_id = d.customer_id INNER JOIN
movie m
ON d.Movie_ID = m.Movie_ID
WHERE due_date >= date_sub(curdate(), interval 15 day);
You might want date_add() instead.
Related
I'm tring to write a query, without a good resultset.
I would to retreive a Number of reservations in last 7 days.
Grouped by NameOfResource.
When i'm tring to set a WHERE clause like this prenotazioni.Data >= CURDATE() - INTERVAL 7 DAY
I get only NameOfResource with reservations in latest 7 days,
but not the rooms without reservations.
My Query was like that: (without WHERE the result is good)
SELECT count(*) as NReservationsFromWeek,Nomeroom FROM reservations
INNER JOIN room ON reservations.FKRoom = room.IDRoom
WHERE reservations.Data >= CURDATE() - INTERVAL 7 DAY
group by room.IDRoom
Thank you to explain me where I was wrong.
You can use a LEFT JOIN, if you want all rooms, even those with a count of 0:
SELECT ro.IDRoom, ro.Nomeroom, COUNT(re.FKRoom) as NReservationsFromWeek,
FROM room ro LEFT JOIN
reservations re
ON re.FKRoom = ro.IDRoom AND
re.Data >= CURDATE() - INTERVAL 7 DAY
GROUP BY ro.IDRoom, ro.Nomeroom; -- Both unaggregated keys should be in the GROUP BY
I want to only find accounts that have more than one instance of a closed_date within their own 30 day, 3 month, and 1 year period which ends at their own monthly, quarterly, and annual expiration date. However each account has its own expiration date range.
My WHERE clause is where I can’t seem to figure out how to implement the proper 30 day, quarterly, and yearly range.
I'm not sure if the BETWEEN clause is appropriate or if I should be using a greater than / less than.
SELECT a.acct, COUNT(d.closed_date) AS cd, a.billing_expiration_date
FROM docupaid d
INNER JOIN account a ON a.acct=d.acct
WHERE (d.closed_date) BETWEEN (a.bill_expiration_date minus 30 days) AND a.billing_expiration_date
GROUP BY acct desc
HAVING cd>1
You can use DATE_SUB function:
SELECT a.acct,
COUNT(d.closed_date) AS cd,
a.billing_expiration_date
FROM docupaid d
INNER JOIN account a ON a.acct=d.acct
WHERE d.closed_date BETWEEN DATE_SUB(a.bill_expiration_date, INTERVAL 30 DAY)
AND a.billing_expiration_date
GROUP BY acct desc
HAVING cd > 1
I've been Googling for a few hours... thought this would be easy, but clearly not for me :)
I've got sales data in two tables and I want to generate a weekly sales report for a specific item. For this purpose, I don't care about dollar values, just number of units. An a "week" is either a calendar week (whatever start day, I don't care) or just 7-day chunks back from current (so week 1 is the last 7 days, week 2 is 8 - 15 days ago, etc) - whichever is easier. I'm simply trying to monitor sales trends over time. Preferably it would span back over years so that if its the first week of January, for example, it wouldn't show just one record.
The data comes from ZenCart. The relevant tables/column structure is here:
Table "orders" has columns: orders_id, date_purchased
Table "orders_products" has columns: orders_id, products_id, product_quantity
Where I'm having trouble is with the joins and syntax.
This worked for my needs:
SELECT o.date_purchased, CONCAT(YEAR(o.date_purchased), LPAD(WEEK(o.date_purchased), 2, '0')) as weekyear, op.products_id, SUM( op.products_quantity )
FROM orders_products op
LEFT JOIN orders o ON op.orders_id = o.orders_id
WHERE op.products_id = 331
GROUP BY weekyear
ORDER BY weekyear
If you have some date/datetime/timestamp column, you can use the week function in your where clause
select week(now()) as week, sum(units) as total
from sales
where week(sales_date) = week(now());
or the previous week
select week(now()) - 1 as week, sum(units) as total
from sales
where week(sales_date) = week(now()) - 1;
You must take care for the year wrap around from week 52/53 to week 0/1.
SQLFiddle for testing.
In order to take care of the year end wrap. for instance, week(12/30/2018)=52 and week(12/31/2018)=52 both are considered week 52 of 2018. the first day of 2019 starts on a Tuesday. you can write a case statement as follows to move 12/30/2018 and 12/31/2018 to the first week of 2019. so that you will have a complete 7 days week to compare:
case when order_date in ( '2018-12-30', '2018-12-31')
then 0
else week(order_date)
end as order_week
I have a query which returns total number of users registered on a particular days in 7 days interval time. I also want to get the zero count data so i can plot it on my graph. How to fetch zero count values?
The query :
select date(update_timestamp) as date, count(*) users
from registered
where date(update_timestamp) between date_sub(sysdate(), interval 7 day) and sysdate()
group by date(update_timestamp)
I had few problems with my query earlier which was solved on this post http://bit.ly/12irdyf .The problem is solved, however i need modification in my query, now i need to show null values as well.
Thanks
best way to keep a calender_table which has got entries for each date for the year.
select date(update_timestamp) as date, count(*) users
from calender_table c
left join registered r
on date(update_timestamp)=c.date
where c.date between date_sub(sysdate(), interval 7 day) and sysdate()
group by c.date
What do you mean by zero count values? Maybe you need:
select date(update_timestamp) as date, count(*) users, '0' AS zero
Maybe you wanted to fetch what date it was when you had no users?
Simply locate the date before the first user registered and assign a zero to it like above.
I've got the following query:
$data = mysql_fetch_array(mysql_query("SELECT
c.`id` AS cid,
p.`id` AS pid,p.`email`
FROM `coupons` AS c
LEFT JOIN `coupons_partners` AS cp ON cp.`cid` = c.`id`
LEFT JOIN `partners` AS p ON p.`id` = cp.`pid`
LEFT JOIN `bills` AS b ON b.`pid` = p.`id`
WHERE
(
CURRENT_DATE() BETWEEN c.`expires` AND ADDDATE(c.`expires`, INTERVAL 1 MONTH)
)
OR
(
CURRENT_DATE() NOT BETWEEN c.`expires` AND ADDDATE(c.`expires`, INTERVAL 1 MONTH)
AND
CURRENT_DATE() BETWEEN b.`date` AND ADDDATE(b.`date`, INTERVAL 1 MONTH)
)
ORDER BY b.`id` DESC"));
It's kinda messy. I want to do a cronjob and create bills automatically. They should only be created after 1 month a coupon expires (c.expires is DATETIME) or if the LAST bill (b.date) was created 1 month ago.
The thing also is that I don't want to create any bill if the coupon didn't started yet. And this is possible, because I create coupons that'll start in maybe 3 months. So I guess the "Between-Solution" doesn't fit here?
I'm trying to figure it out to do it properly, but now I would appreciate any help.
I would be happy and thankful, if someone could help me out.
Best Regards,
Alex
Use DATEDIFF
SELECT DATEDIFF(month, '2000-02-10','2000-01-10');
yields 1 since 02-10 is one month after 01-10
month can be changed to day to get the number of days instead.
Reference: http://www.1keydata.com/sql/sql-datediff.html
UPDATE:
In MSQL you could use DATEDIFF because it can be passed month as an argument, the MySQL version only returns days. In MySQL you could instead use PERIOD_DIFF. Although it doesn't know about the exact number of days in each month.
PERIOD_DIFF(P1,P2)
"Returns the number of months between periods P1 and P2. P1 and P2 should be in the format YYMM or YYYYMM. Note that the period arguments P1 and P2 are not date values."
mysql> SELECT PERIOD_DIFF(200802,200703);
-> 11
If you need to know if exactly how many months differ you need to make a multistep calculation. To find out the number of days in a month you could use the LAST_DAY function and from the results extract the day part and use as the basis for further calculations.
LAST_DAY('2003-02-05');
-> '2003-02-28'