Sql - Error with expression of non-boolean - sql

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

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.

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;

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.

Related

REPLACE with JOIN - SQL

I need help to understand what I did wrong ... I'm a beginner so excuse me the simple question!
I have two tables in which I want to do a JOIN where, in one of the columns I had to use REPLACE to remove the text 'RIxRE' that does not interest me.
In table 1, this is the original text of the column id_notification: RIxRE-1787216-BSB and this is the text that returns when using REPLACE: 1787216-BSB
In column 2, this is the text that exists: 1787216-BSB
However, I get the following error:
# 1054 - Unknown column 'a.id_not' in 'on clause'
SELECT *, REPLACE(a.id_notificacao,'RIxRE','') AS id_not
FROM robo_qualinet_cadastro_remedy a
JOIN (SELECT * FROM painel_monitoracao) b ON a.id_not = b.id_notificacao
You cannot use a column alias again in the FROM clause or the WHERE clause after the SELECT (and possibly not other clauses as well, depending on the database).
So, repeat the expression:
SELECT *, REPLACE(a.id_notificacao, 'RIxRE', '') AS id_not
FROM robo_qualinet_cadastro_remedy rqcr JOIN
painel_monitoracao pm
ON REPLACE(rqcr.id_notificacao, 'RIxRE', '') = pm.id_notificacao;
Notes:
Use table aliases the mean something, such as abbreviations for the able names.
The subquery is not necessary in the FROM clause.
I suspect that you have a problem with your data model if you need a REPLACE() for the JOIN condition, but that is a different issue from this question.

Issue using MINUS clause in SQL

While using minus clause in between two statements giving some error. Can someone help me with this?
Error is Msg 102, Level 15, State 1, Line 101
Incorrect syntax near 'MINUS'.
SELECT a from (SELECT DISTINCT(name) as a FROM hack WHERE name LIKE '%') a
MINUS
SELECT b from (SELECT DISTINCT(name) as b FROM hack WHERE name LIKE '[aeiou]%[aeiou]') b
MINUS is exist in Oracle. By seeing your error message, I hope you are looking in SQL Server.
In SQL Server, EXCEPT is the correct replacement for MINUS.
SELECT DISTINCT name
FROM hack
WHERE name LIKE '%'
EXCEPT
SELECT DISTINCT name
FROM hack
WHERE name LIKE '[aeiou]%[aeiou]'
You can simplify the logic to:
SELECT DISTINCT name
FROM hac
WHERE name NOT LIKE '[aeiou]%[aeiou]'
A simple comparison should be much more efficient that multiple comparisons along with set operators.

SQL Msg 209, Level 16, State 1, Line 498 Ambiguous column name 'SupplyCode'

I can't seem to solve the error no matter what I do.
Msg 209, Level 16, State 1, Line 498
Ambiguous column name 'SupplyCode'.
--h. Select the supply code and description of the supplies that have never been used on a job.(2 marks)
select SupplyCode,Description, count(*) from JobSupply
inner join Supply
on Supply.SupplyCode = JobSupply.SupplyCode
group by Supply.SupplyCode
Msg 8120, Level 16, State 1, Line 498
Column 'Supply.Description' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
This is what i had orignallay done
select Supply.SupplyCode,Description,count(jobNumber) from Supply
inner join JobSupply
on Supply.SupplyCode = JobSupply.SupplyCode
group by Supply.SupplyCode
having Count(JobNumber) = 0
You need to qualify SupplyCode in the selected columns as it cannot disambiguate from the SupplyCode in Supply and the SupplyCode in JobSupply. Try:
select S.SupplyCode,Description, count(*) from JobSupply
inner join Supply S on S.SupplyCode = JobSupply.SupplyCode
group by S.SupplyCode
Since the column SupplyCode exists in both referenced tables you have to qualify the name in the select statement and specify which table it should be retrieved from.
You might consider using aliases too:
select s.SupplyCode, Description, count(*)
from JobSupply js
join Supply s on s.SupplyCode = js.SupplyCode
group by s.SupplyCode, description
On a side note, the query you provided probably won't answer the question you are trying to solve. (Select the supply code and description of the supplies that have never been used on a job)
You probably want to use a left join and filter out the missing rows as those should be the ones that have never been used. Or you could use a correlated subquery with the exists predicate:
Something like this:
select SupplyCode, Description
from Supply s
where not exists (select 1 from jobsupply js where s.SupplyCode = js.SupplyCode)

ORDER BY lower case of output column

I'm able to run this Postgres query without any issue:
select
(select product_types.name from product_types
where product_types.id = products.product_type_id) AS product_type_name
from products
order by product_type_name
But when I tried to order by lower case it doesn't work:
select
(select product_types.name from product_types
where product_types.id = products.product_type_id) AS product_type_name
from products
order by lower(product_type_name)
I get this error:
ERROR: column "product_type_name" does not exist
LINE 4: order by lower(product_type_name)
^
********** Error **********
ERROR: column "product_type_name" does not exist
SQL state: 42703
Character: 156
Can someone please shed me some light on this?
At first sight, your first query could be rewritten just this way:
select pt.name product_type_name from product_types pt
join products p on pt.id = p.product_type_id
order by pt.name
Then, ordering with the lower function would mean just changing the order by to:
order by lower(pt.name)
Quoting the manual page on SELECT:
Each expression can be the name or ordinal number of an output column
(SELECT list item), or it can be an arbitrary expression formed from
input-column values.
You were trying to order by an expression formed from an output-column, which is not possible.

How do I execute different SELECT statements based on a CASE

I am facing a problem in executing queries with CASE statement.
Based on my condition,(for eg. length), I want to execute different SQL statement.
Problematic sample query is as follows:
select case
when char_length('19480821') = 8
then select count(1) from Patient
when char_length('19480821')=10
then select count(1) from Doctor
end
Exception:
[Error] Script lines: 1-5 --------------------------
Incorrect syntax near the keyword 'select'.
Msg: 156, Level: 15, State: 2
Server: sunsrv4z7, Line: 2
I am not able to correct the syntax. I am getting the string for char_length as input from the user.
How can I fire queries based on certain condition?
Is CASE the right choice ? Or do I have to use any other thing.
Just put opening and closing bracket around select statement resolve you problem
select
case when
char_length('19480821')=8 then
(select count(1) from Patient )
when
char_length('19480821')=10 then
(select count(1) from Doctor )
end
select
case when char_length('19480821')=8 then (select count(1) from Patient)
when char_length('19480821')=10 then (select count(1) from Doctor)
end
The problem is that you are missing opening and closing brackets in your nested 'Select' statements :)
Please do note that it is not a case STATEMENT, it is a case EXPRESSION. By enclosing the queries in parentheses, you are converting them (syntactically) to values.
This is similar in principle to a subquery, such as
" select name from Doctor where salary = (select max(salary) from Doctor)"
select
case when
LEN('1948082100')=8 then
(select 'HELLO' )
when
LEN('194808210')=10 then
(select 'GOODBYE')
end
Change the values to test results.