0

Can anyone help me find this issue?

This is my code

SELECT 
    MSC_Customer.cust_number, cust_name, 
    COUNT(ord_number) AS number_of_orders
FROM 
    MSC_Customer, MSC_Order
WHERE 
    MSC_Customer.cust_number = MSC_Order.cust_number
HAVING 
    MSC_Customer.cust_number
GROUP BY 
    cust_city LIKE 'Pennsylvania';

I get this error

Msg 4145, Level 15, State 1, Line 5
An expression of non-boolean type specified in a context where a condition is expected, near 'GROUP'.

I am trying to join the two tables, and use a COUNT aggregate and a GROUP BY clause in the SELECT statement

1
  • I have removed the MySQL tag, as the error message look like SQL Server. Please add some sample data to your question which demonstrates what you are trying to here. Commented Oct 28, 2018 at 2:13

3 Answers 3

3

I'm going to suggest the following corrected query:

SELECT
    c.cust_number,
    c.cust_name,
    COUNT(ord_number) AS number_of_orders
FROM MSC_Customer c
INNER JOIN MSC_Order o
    ON c.MSC_Customer = o.cust_number
WHERE
    cust_city LIKE '%Pennsylvania%'    -- or maybe just cust_city = 'Pennsylvania'
GROUP BY
    c.cust_number,
    c.cust_name;

I am assuming that you want to aggregate by customer name/number. The check on the customer city would seem to belong in a WHERE clause, not in the GROUP BY clause. Of note, I rephrased your query to use an explicit inner join, instead of an old school implicit join. This is the preferred way of writing joins now.

Sign up to request clarification or add additional context in comments.

2 Comments

One small correction - qualify the column in the where clause with the appropriate alias And perhaps add an order by clause with explanation about why.
@SMor Fixing the alias assumes I know to which table the cust_city column belongs.
0

Use condition in Where not Group By. Try below Script

SELECT MSC_Customer.cust_number, cust_name, count(ord_number) AS 
number_of_orders
FROM MSC_Customer
JOIN MSC_Order ON
    MSC_Customer.cust_number = MSC_Order.cust_number 
WHERE cust_city like '%Pennsylvania%'
GROUP BY cust_city;

Comments

0

This type of error occurs generally when you write keyword which is used in expression or condition but you have not passed the value of the condition. For example -

select * from table where

this will give the same error as you have not passed here the value for where

You have missed to write the condition in having clause. You need to write the value with operator in having clause which may be one of the following.

HAVING 
    MSC_Customer.cust_number = '0120008024'
OR 
HAVING 
    MSC_Customer.cust_number <> '0120008024'
OR 
HAVING 
    MSC_Customer.cust_number like '%0120008024%'

You have to only specify the values in having clause you have missed as per your requirement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.