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

Sobin Sebastian
Sobin Sebastian
5,348 Points

I don't get it, whats wrong ?

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 have the status of shipped yesterday. Alias it to shipped_yesterday.

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

but its saying wrong can please someone help me understand what wrong am i doing here?

https://teamtreehouse.com/library/reporting-with-sql/date-and-time-functions/calculating-dates-2

7 Answers

Sobin Sebastian
Sobin Sebastian
5,348 Points

the correct answer is SELECT COUNT(*) AS shipped_yesterday FROM orders WHERE status = "shipped" AND ordered_on = DATE("now", "-1 day");

Looks like you figured it out. Sorry, I was looking at the wrong question. I save the questions and my answers so I can go back and review. Glad you figured it out!

This is exactly what I had. So glad I'm understanding SQL. Hoping that this can be the same momentum going into other languages. Hope everyone's progression goes well.

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

V. T van der Vlugt
V. T van der Vlugt
14,883 Points

can't you use the NOW() function?

for example(information from MYSQL DATE ref

SELECT COUNT(*) AS shipped_today 
FROM orders 
WHERE status = "shipped" AND ordered_on = NOW() - INTERVAL 1 DAY;

Hope this helps

Steven Parker
Steven Parker
231,261 Points

It looks like V.T. was referring to MySQL syntax. Treehouse uses Sqlite. And you may have given the wrong link. It looks like you're working on the Today's Report challenge.

:point_right: In Sqlite, the current date is represented by: DATE("now").

And since you're only interested in today, you won't need to perform any calculations on the date.

Rakeeb Jalaldeen
Rakeeb Jalaldeen
3,847 Points

This worked for me,

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

Sobin, You are correct. My mistake. I was looking at the wrong question. It is:

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

roberto lopez
PLUS
roberto lopez
Courses Plus Student 2,300 Points

SELECT COUNT(*) AS shipped_yesterday FROM orders WHERE status = "shipped" AND ordered_on = DATE("now", "-1 day"); this give me an error still why

This still gives me an error too!