Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases Reporting with SQL Date and Time Functions Calculating Dates

Can someone help me on this challenge on SQL,CALCULATING DATES.

In an ecommerce database there's an orders table with the columns id, product_id, user_id, address_id, ordered_on, status and cost.Count the total number of orders that were ordered yesterday and have the status of 'shipped'. Alias it to ordered_yesterday_and_shipped.

Here is the solution: SELECT COUNT(*) AS ordered_yesterday_and_shipped FROM orders WHERE status ='shipped' AND ordered_on = DATE('now', '-1 day')

8 Answers

SELECT COUNT(status) AS "ordered_yesterday_and_shipped" FROM orders WHERE status = "shipped" AND ordered_on = DATE("now", "-1 day");

this worked for me. probly because of your aliashope that helped

SELECT COUNT(status) AS shipped_yesterday FROM orders WHERE status = "shipped" AND ordered_on = DATE("now", "-1 day"); //This was my code.

I have the same problem. Using the same code as you but not getting any output.

i recieve error

johnny lazo
johnny lazo
13,432 Points

SELECT COUNT(*) AS ordered_yesterday_and_shipped FROM orders WHERE status = 'shipped' AND ordered_on = DATE("now", "-1 day");

Nolusindiso Hleko
Nolusindiso Hleko
5,357 Points

Is there a reason why Jonny's solution is downvoted I wrote the same and it worked:

SELECT COUNT(*) AS ordered_yesterday_and_shipped FROM orders WHERE status = "shipped" AND ordered_on = DATE("now","-1 day");

Robbie Thomas
Robbie Thomas
31,093 Points

Walter Guererro is right. However, when I did my code, it was like this:

SELECT COUNT(status) AS ordered_yesterday_and_shipped FROM orders WHERE status = "shipped" AND ordered_on = DATE("now", "-1 day");

And it told me it was expecting a count of 14. If you do that code on the SQL playground that goes along with the previous video, the count is 14. Also, I could be wrong about this but do these AS statements not require the use of quotation marks unless they need a string? Doesn't seem right that they want you to put ordered_yesterday_and_shipped with quotes in it.

Marcos Treviño Rodriguez
Marcos Treviño Rodriguez
5,711 Points

I just did the same mistake by adding twice the "WHERE" =(, but when fixing it, it worked.

Kingsley Atuba
Kingsley Atuba
2,891 Points

SELECT COUNT(*) AS shipped_today FROM orders WHERE ordered_on = DATE() AND status = 'shipped';

I tried all the code above, and finally adding quotes around the alias worked for me:

SELECT COUNT(status) AS "ordered_yesterday_and_shipped" FROM orders WHERE status="shipped" AND ordered_on = DATE("now", "-1 day");