ORDER BY lower case of output column - sql

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.

Related

CakePHP: The Code was working perfectly but just start bringing this error

Error: SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'app.Tasks.task_completed' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
If you are using SQL keywords as table column names, you can enable identifier quoting for your database connection in config/app.php.
SQL Query:
SELECT
COUNT(TasksEmployees.task_id) AS `count`,
TasksEmployees.employee_id AS `employee_id`,
Tasks.task_completed AS `task_completed`
FROM
tasks_employees TasksEmployees
LEFT JOIN tasks Tasks ON (
Tasks.task_completed = :c0 AND
Tasks.id = (TasksEmployees.task_id)
)
WHERE
YEAR(TasksEmployees.created) = :c1
GROUP BY
TasksEmployees.employee_id
If you want to customize this error message, create src/Template/Error/pdo_error.ctp
you have 2 options to resolve this:
1 - If you have sql root access and permissions (it could be from phpmyadmin), so remove only_full_group_by from sql_mode var
Click in edit and remove that var.
2 - Include all fields selected in SELECT clause into the GROUP BY clause:
SELECT
COUNT(TasksEmployees.task_id) AS `count`,
TasksEmployees.employee_id AS `employee_id`,
Tasks.task_completed AS `task_completed`
FROM
tasks_employees TasksEmployees
LEFT JOIN tasks Tasks ON (
Tasks.task_completed = :c0 AND
Tasks.id = (TasksEmployees.task_id)
)
WHERE
YEAR(TasksEmployees.created) = :c1
GROUP BY
employee_id, count, task_completed

TSQL / SQL - Error: Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

I'm hoping someone can point me in the right direction here.
DB #1 NAME: "Wayin_Integration_SFMC_AD"
And I'm trying to merge with this Master table (DB #2): "Master_Users_SVOC_test_vt"
Assuming everyone in "Wayin_Integration_SFMC_AD" already has a primary key and record in "Master_Users_SVOC_test_vt", now I want to MERGE INTO the remaining data for that record.
Problem is, most of my fields aren't included in the GROUP BY clause.
Do I need to use a subquery here? I am very new to SQL. Here is the snippet that I am unsure of. Without the GROUP BY this would work, but it's the grouping that is confusing me.
ERROR MESSAGE: "Column 'Wayin_Integration_SFMC_AD.First_Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
"
which is the first field not included in the GROUP BY clause.
THIS IS THE SQL THAT ISN'T WORKING:
MERGE INTO [bo_marketing_capability_dev].[dbo].[Master_Users_SVOC_test_vt] AS m
USING (
SELECT
m.subscriber_key,
m.email_address,
w.[First_Name] AS first_name,
w.[Last_Name] AS last_name,
, MAX (w.DateAdded) AS wayin_DateAdded
FROM Wayin_Integration_SFMC_AD w
INNER JOIN Master_Users_SVOC_test_vt m
ON w.Email = m.email_address
GROUP BY subscriber_key, w.[Email], m.email_address
) AS SRC
ON ([SRC].[email_address] = [m].[email_address])
MERGE INTO [bo_marketing_capability_dev].[dbo].[Master_Users_SVOC_test_vt] AS m
USING (
SELECT
m.subscriber_key,
m.email_address,
w.[First_Name] AS first_name,
w.[Last_Name] AS last_name,
, MAX (w.DateAdded) AS wayin_DateAdded
FROM Wayin_Integration_SFMC_AD w
INNER JOIN Master_Users_SVOC_test_vt m
ON w.Email = m.email_address
GROUP BY m.subscriber_key, m.email_address, w.[First_Name], w.[Last_Name]
) AS SRC
ON ([SRC].[email_address] = [m].[email_address])
Try above code.
GROUP BY should have all non aggregate columns.

Order table being read as ORDER command by the compiler

I have this sql statment stored in a string variable
SELECT
o.id_order,
o.registerdate,
i.latest_invoice,
d.latest_delivery
FROM
order o
JOIN
(
SELECT
id_address,
max(registerdate) as latest_invoice
FROM
invoice
GROUP BY
id_address
) i
ON o.id_address = i.id_address
JOIN
(
SELECT
id_address,
max(registerdate) as latest_delivery
FROM
delivery
GROUP BY
id_address
) d
ON o.id_address = d.id_address
WHERE
o.id_address = '189'
When I execute the query I get an error at the position of order o, because the compiler thinks that it is the ORDER BY command. Do you know how can I avoid this?
You shouldn't be using reserved keywords as table names, but if you insist you have to use double quotes:
from "order" o
Note that with double quotes the name is case-sensitive. Depending on how you created that table, you might need "ORDER" or "Order". In psql you should check the correct case using the \d command.

Sql - Error with expression of non-boolean

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.

OODB Update with Aggregate Function

I'm trying to update a column in my table "InventoryTable" with the SUM of values returned from Querying my other table "OrderTable"
I found several other questions like this here, and composed this statement:
UPDATE InventoryTable
SET SalesPerMonth = foo.ASPM
FROM InventoryTable
INNER JOIN (
SELECT InventoryID, SUM(Quantity) AS ASPM
FROM OrderTable
GROUP BY InventoryID
) AS foo ON foo.InventoryID = InventoryTable.InventoryID
I'm using this on OpenOffice Base SQL Edit and I keep getting a syntax error:
Syntax Error in SQL Expression
with these details:
SQL Status: HY000 Error code: 1000
syntax error, unexpected $end, expecting BETWEEN or IN or SQL_TOKEN_LIKE
I cannot figure out what I am doing wrong.
Thank you.