1

I am trying to count the number of 'Shipped' orders with an amount greater than 1000 from two tables.

I have table Orders(Status column) and table Payments(Amount column)

select count(o.status, p.amount)
from orders o, payments p
where (o.status = 'Shipped' AND p.amount>1000); 

After getting an error I broke up the problem into two with the first finding all 'Shipped' status in Orders. This works just fine.

select count(o.status)
from orders o 
where o.status = 'Shipped'; 

But trying to count all payments above 1000 doesn't seem to work.

select count(p.amount)
from payments p 
where p.amount>=1000; 

In case anyone needs the file, I am using the sample DB from MySQl sample database http://www.mysqltutorial.org/mysql-sample-database.aspx

4
  • What does "doesn't seem to work," mean? Is there an error message? Commented Oct 20, 2019 at 16:37
  • "You have an error in your mysql syntax please consult the manual..." not a very helpful error message. Commented Oct 20, 2019 at 16:43
  • Does select count() as cnt from payments where amount >= 1000; work? Commented Oct 20, 2019 at 16:45
  • your table seems not related .. you have not condtion for join the orders table to the payments table .. Commented Oct 20, 2019 at 16:46

1 Answer 1

1

if you want know the number of order "shipped" with payment with (each) amount > 1000 the first you should join the table in proper way
looking to your schema the order table is realated to payments table via the customers on customerNumber

so you should

  select count(distinct o.id)
  from orders o
  INNER JOIN customers c ON c.customerNumber =  o.customerNumber
  INNER JOIN  payments p p.customerNumber = c.customerNumber
  where o.status = 'Shipped' 
  AND p.amount>1000); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the clarification, table above doesn't work but I worked something out with inner join.
what do you mean wih "table above doesn't work " .. you have an error .. show me the exact error message ... wrong result .. update your question add a clear data sample as tabular tex (not a link to a tutorial) and your expected result

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.