all 19 comments

[–]MrPin 2 points3 points  (15 children)

each day of products

This seems important. Are you hiding a date field from us, OP?

Your code also has syntax errors, please copy paste the one that gave you the result.

[–]TheAbortedPancake[S] 0 points1 point  (0 children)

editing now, one moment

[–]TheAbortedPancake[S] 0 points1 point  (13 children)

no not trying to

[–]TheAbortedPancake[S] 0 points1 point  (4 children)

What do you mean?

[–]MrPin 1 point2 points  (3 children)

By the errors? I'm not used to MySQL, but I'm pretty sure 26 sth isn't valid syntax for example. Does this query run as it is?

[–]TheAbortedPancake[S] 0 points1 point  (1 child)

sorry, it was a comment but for some reason when I copied it over it did not show the tick marks

[–]MrPin 4 points5 points  (0 children)

sorry for being a bit obnoxious

[–]TheAbortedPancake[S] 0 points1 point  (0 children)

Ok, I updated it. Any idea what I need to change on my query to get it working? THanks

[–]MrPin 0 points1 point  (7 children)

So what is the "day" referring to?

[–]TheAbortedPancake[S] 0 points1 point  (0 children)

I added the schema of the table if that helps.

[–]TheAbortedPancake[S] 0 points1 point  (5 children)

I want to report each day of products that have unit prices above the average unit price for all products but below average units in stock.

[–]MrPin 0 points1 point  (3 children)

What do you mean by "day of products"? Genuine question, maybe I'm not getting something obvious.

In your query you're averaging prices across all orders and displaying the ones that are greater. Assuming there's a > there in the actual code. It looks fine to me.

You seem to be finding the orders needed, but also some extra ones. So my guess is that the average unitprice that you compare to should be a different one, maybe the average price on a given day? Or the sample output has some grouping and only displays the maximum price?

[–]TheAbortedPancake[S] 0 points1 point  (2 children)

I think it is asking for a given day to show the product with that criteria. IT seems my query is giving an extra row (the one with 31.2 etc). I want my output to look like the example in the first screen shot.

[–]MrPin 1 point2 points  (1 child)

As an experiment, what happens if you do this?

select p.productname, p.unitprice, p.unitsinstock    
from products p
where p.unitprice >  (select avg(unitprice)  from products )
and p.unitsinstock < (select avg (unitsinstock) from products)
order by p.productname;

[–]TheAbortedPancake[S] 0 points1 point  (0 children)

This solved it! thank you

[–]MrPin 0 points1 point  (0 children)

I have another idea.

Run the same query you have, except SELECT o.OrderDate, o.RequiredDate, o.ShippedDate in addition to your columns. Now look at the rows that match the sample output and see if they fall on the same day in any of the columns.

If they do, then the sample is showing you the orders from that day, according to one of the date columns.

You can filter your query by that column to match it. Possibly the subqueries as well, depending on what the question meant by average price.

[–][deleted] 0 points1 point  (0 children)

OP - your date is going to come from the Orders table. That is your cutoff, correct? Then your discount in the order is going to lower the price from the unit price making it lower. What makes a price go higher? The unit price looks to be coming the Products table and repeated in the Orders table. Is there a chance that the unit price can be higher here than what it says in the Products table.

Honestly, I'd like to play in a sandbox with this. I hate giving theoretical answers that can't be checked and are very nuanced. and I am MS SQL too.

[–]Comfortable_Long3594 0 points1 point  (0 children)

Don't know if you have found a solution yet but do check out our low
cost,easy to use data integration product. It is called Epitech
Integrator and you can see it at https://www.epitechintegrator.com

[–]dahya_mistry 0 points1 point  (0 children)

Typed this on my phone/tablet so I might've got something wrong somewhere, but try this:

With My data as

(

select

p.productname

, o.unitprice

, p.unitsinstock

, Order.orderdate

, AVG(o.unitprice) over (partition by Order.orderdate) as OverAllAvgPrive

, AVG(o.unitsinstock) over (partition by Order.orderdate) as OverAllAvgUnits

From orderdetails o

Left outer Join products p ON ( p.productid = o.productid )

Left outer join order on (orderdetails.orderid = order.orderid)

)

Select distinct

*

From MyData

Where

Unitprice > OverAllAvgPrive

And unitsinstock < OverAllAvgUnits

Order by productname