Invalid column name 'warehouse_name' [duplicate] - sql

This question already has answers here:
Invalid column name error when adding to Group By
(3 answers)
How do I perform a GROUP BY on an aliased column in SQL Server?
(12 answers)
Closed last month.
Please assist me to resolve the issue with this query below. It keeps telling me I have an invalid column name. When I run the query without the subqueries and the CASEs and GROUP BY, it gives me a column name correctly.
`SELECT`
Warehouse.warehouse_id,
`CONCAT`(Warehouse.state, ' : ', Warehouse.Warehouse_alias) `AS` warehouse_name,
`COUNT`(Orders.order_id) AS number_of_orders,
(`SELECT`
`COUNT`(*)
`FROM` Warehouse_Orders_Orders Orders)
`AS` total_orders,
`CASE`
`WHEN` `COUNT`(Orders.order_id)/ (`SELECT` `COUNT`(*) `FROM` Warehouse_Orders_Orders Orders) <=0.2
`THEN` 'fulfilled 0 - 20% of orders'
`WHEN` COUNT(Orders.order_id)/ (`SELECT` `COUNT`(*) `FROM` Warehouse_Orders_Orders Orders) > 0.2
`AND` `COUNT`(Orders.order_id)/ (`SELECT` `COUNT`(*) `FROM` Warehouse_Orders_Orders Orders) <=0.6
`THEN` 'fulfilled 21 - 60% of orders'
`ELSE` 'fulfilled more than 60% of orders'
`END AS` fulfillment_summary
`FROM`
chuks1.dbo.Warehouse_Orders_Warehouse Warehouse
`LEFT JOIN` chuks1.dbo.Warehouse_Orders_Orders Orders
`ON` Orders.warehouse_id = Warehouse.warehouse_id
`GROUP BY`
Warehouse.warehouse_id,
Warehouse.warehouse_name
`HAVING`
`COUNT`(Orders.order_id) >0
I have tried removing the conditions one after the other. I still can't understand why it keeps saying:
Invalid column name 'Warehouse_name'
I was expecting to get orders that meet the conditions in the CASEs in the query. I don't know if the problem is the aliases.

Related

Column doesn't exist using CASE statement in PosgreSQL [duplicate]

This question already has answers here:
delete "column does not exist"
(1 answer)
Simple Postgresql Statement - column name does not exists
(2 answers)
Closed 10 months ago.
I am trying to calculate the payments for each age range in PostgreSQL.
select
case
when age < 50 then "0-50"
else "50+"
end as age_range,
SUM(payment_amount) as sum_payment_amount
from
(
select
age,
payment_amount
from
"002_DM_clients" dc
left join user_payment_log upl
on
dc.client_id = upl.client_id
) query1
group by
age_range;
But I get the following error:
Error executing query:
SQL Error [42703]: ERROR: column "0-50" does not exist
I have no idea what the problem could be.

Divide by zero error when using >, < or <> in Where statement. No division operator involved

I have two CTEs using the same table which holds receipts. Receipt type "a" says how much is billed and may or may not have an amount received. Receipt type "b"s have how much was received if it was received outside of the original receipt. These are matched up by mnth, cusnbr and job. Also on the receipt is how much is allocated for different expenses on the receipt.
I am trying to total up peoples hours if the receipt has been paid at least 99%. These records are based on cusnbr, jobnbr and mnth also. The code below works fine.
with billed as(Select cusnbr
,job
,mnth
,sum(bill_item_1) as 'Billed Item'
,sum(billed) as 'Billed'
From accounting
Where mytype in ('a','b')
Group by cusnbr
,job
,mnth)
paid as(Select cusnbr
,job
,mnth
,sum(rcpt_item_1) as 'Rcpt Item'
,sum(billed) as 'Paid'
From accounting
Where mytype in ('a','b')
Group by cusnbr
,job
,mnth)
Select b.cusnbr
,b.job
,b.mnth
,sum(g.hours) as 'Total Hours'
,b.[Billed Item]
,p.[Rcpt Item]
From billed b inner join paid p
on b.cusnbr = p.cusnbr
and b.job = p.job
and b.mnth = p.mnth
inner join guys g
on b.cusnbr = g.cusnbr
and b.job = g.job
and b.mnth = g.mnth
Where p.[Paid]/b.Billed > .99
The issue I'm having is if I try to add
and b.[Billed Item] <> 0
To the where clause.
I get "Divide by zero error encountered"
I have tried making the last query a CTE with
case when b.[Billed Item] = 0 then 1 else 0 end as flag
and then making another query which checks that flag <> 0
I have tried using isnull(b.[Billed Item],0) in the last query as well as isnull(bill_item_1,0) in the first CTE.
I can get around this issue by dumping the whole thing into a temp table and querying that, but just want to know why this is happening. Using ">","<" or "<>" against b.[Billed Item] results in a divide by zero error.
Use nullif():
Where p.[Paid] / nullif(b.Billed, 0) > 0.99
This will return null -- which does not meet the condition. You can also phrase this more simply without division as:
where p.paid > b.billed * 0.99
I can't answer you question specifically, but I can tell you that SQL does not process all commands if it does not need to. For example,
SELECT COUNT(1/0)
Happily returns 1. So it is quite possible the order of the conditions cause the optimizer to filter out an uncessary division by zero condition.

how to solve " ORA-00979: not a GROUP BY expression " Error [duplicate]

This question already has answers here:
ORA-00979 not a group by expression
(10 answers)
Closed 5 years ago.
I'm trying to run this query and I get "not a group by expression"
SELECT
PS.SEGMENT1,
PSSI.VENDOR_NAME,
PSSI.VENDOR_SITE_CODE,
AIPA.PAYMENT_NUM,
AIPA.AMOUNT,
AIPA.PAYMENT_CURRENCY_CODE,
AIA.INVOICE_AMOUNT,
AIA.INVOICE_NUM,
AIA.DESCRIPTION,
AIA.INVOICE_DATE
FROM
POZ_SUPPLIERS PS,
POZ_SUPPLIER_SITES_INT PSSI,
AP_INVOICE_PAYMENTS_ALL AIPA,
AP_INVOICES_ALL AIA
WHERE PSSI.VENDOR_ID=PS.VENDOR_ID
AND AIA.VENDOR_ID=PS.VENDOR_ID
AND AIPA.INVOICE_ID=AIA.INVOICE_ID
group by AIPA.AMOUNT
can anyone point me in the right direction ?
Group by will not auto sum for you...I'm kinda guessing this is what you want
SELECT
PS.SEGMENT1,
PSSI.VENDOR_NAME,
PSSI.VENDOR_SITE_CODE,
AIPA.PAYMENT_NUM,
sum(AIPA.AMOUNT) as totalamount,
AIPA.PAYMENT_CURRENCY_CODE,
AIA.INVOICE_AMOUNT,
AIA.INVOICE_NUM,
AIA.DESCRIPTION,
AIA.INVOICE_DATE
FROM
POZ_SUPPLIERS PS,
POZ_SUPPLIER_SITES_INT PSSI,
AP_INVOICE_PAYMENTS_ALL AIPA,
AP_INVOICES_ALL AIA
WHERE PSSI.VENDOR_ID=PS.VENDOR_ID
AND AIA.VENDOR_ID=PS.VENDOR_ID
AND AIPA.INVOICE_ID=AIA.INVOICE_ID
group by PS.SEGMENT1,
PSSI.VENDOR_NAME,
PSSI.VENDOR_SITE_CODE,
AIPA.PAYMENT_NUM,
AIPA.PAYMENT_CURRENCY_CODE,
AIA.INVOICE_AMOUNT,
AIA.INVOICE_NUM,
AIA.DESCRIPTION,
AIA.INVOICE_DATE
The sum(amount) in the select statement will do the sum for you. You then need to group by the rest of the static columns
Edit:
I accidentally left AIPA.AMOUNT in the group by. I editted my answer, make sure AIPA.AMOUNT is not in your group by clause

Need to find average and number of repetitions of column

I have an SQL sentence :
SELECT application.id,title,url,company.name AS company_name,package_name,ranking,date,platform,country.name AS country_name,collection.name AS collection_name,category.name AS category_name FROM application
JOIN application_history ON application_history.application_id = application.id
JOIN company ON application.company_id = company.id
JOIN country ON application_history.country_id = country.id
JOIN collection ON application_history.collection_id = collection.id
JOIN category ON application_history.category_id = category.id
WHERE application.platform=0
AND country.name ='CZ'
AND collection.name='topfreeapplications'
AND category.name='UTILITIES'
AND application_history.ranking <= 10
AND date::date BETWEEN date (CURRENT_DATE - INTERVAL '1 month') AND CURRENT_DATE
ORDER BY application_history.ranking ASC
It produces this result :
I'd like to add both a column average ranking for a given package, and a column number of appearances, which would count the number a package appears in the list. I'd also like to Group results by package_name, so that I don't have redundancies.
So far, I've tried to add a GROUP BY By clause before the ORDER BY :
GROUP BY package_name
But it returns me an error :
column "application.id" must appear in the GROUP BY clause or be used in an aggregate function
If I add each and every column it asks me for, it doesn't work.
I have also tried to count the number of package names, by adding after the SELECT :
COUNT(package_name) AS count
It produces a similar error.
How could I get the result I'm looking for ? Should I make two queries instead, or is it possible to get everything at once ?
I precise I have looked at other answers on S.O, but none of them tries to make the COUNT on a "produced" column.
Thank you for your help.
Edit :
Here is the result I expected at first :
Although Gordon's advice didn't give me the proper result it put me on the good track, when I read this :
From the docs : "Unlike regular aggregate functions, use of a window function does not cause rows to become grouped into a single output row."
So I came back to using COUNT and AVG alone. My problem was that I wanted to display the ranking column and date to check whether things were right. But putting these column into the Select prevented the GROUP BY to work as expected, as mentioned by Jarlh in the comments.
The working query :
SELECT application.id,title,url,company.name AS company_name,package_name,platform,country.name AS country_name,collection.name AS collection_name,category.name AS category_name,
COUNT(package_name) AS count, AVG(application_history.ranking) AS avg
FROM application
JOIN application_history ON application_history.application_id = application.id
JOIN company ON application.company_id = company.id
JOIN country ON application_history.country_id = country.id
JOIN collection ON application_history.collection_id = collection.id
JOIN category ON application_history.category_id = category.id
WHERE application.platform=0
AND country.name ='CZ'
AND collection.name='topfreeapplications'
AND category.name='UTILITIES'
AND application_history.ranking <= 10
AND date::date BETWEEN date (CURRENT_DATE - INTERVAL '1 month') AND CURRENT_DATE
GROUP BY package_name,application.id,company.name,country.name,collection.name,category.name
ORDER BY count DESC
I think you want window/analytic functions. The following adds two columns, one for the count of rows for each package and the other an average ranking for them:
SELECT application.id, title, url, company.name AS company_name, package_name,
ranking, date, platform, country.name AS country_name,
collection.name AS collection_name, category.name AS category_name,
count(*) over (partition by package_name) as count,
avg(ranking) over (partition by package_name) as avg_package_ranking
FROM application . . .

SQL Error - not a group by expression

With this SQL Query, I am attempting to list overdue book by patron. In addition, I want to group and order the books by patron. I understand you have to use some kind of aggregate function when doing a GROUP BY function. Even so, I am getting a "not a group by expression" error. Any help is appreciated.
Edit: updated code
What if I want to get the total fees per person and book? The bottom code is still resulting in the same error.
SELECT PATRON.LAST_NAME, PATRON.FIRST_NAME, PATRON.PHONE, BOOK.BOOK_TITLE, CHECKOUT.DUE_DATE, SUM((SYSDATE - CHECKOUT.DUE_DATE) * 1.00) AS FEE_BALANCE
FROM CHECKOUT
JOIN PATRON
ON CHECKOUT.PATRON_ID=PATRON.PATRON_ID
JOIN COPY
ON CHECKOUT.COPY_ID=COPY.COPY_ID
JOIN BOOK
ON COPY.BOOK_ID=BOOK.BOOK_ID
WHERE CHECKOUT.RETURN_DATE IS NULL
AND CHECKOUT.DUE_DATE > SYSDATE
GROUP BY PATRON.PATRON_ID
HAVING SUM((SYSDATE - CHECKOUT.DUE_DATE) > 0
ORDER BY PATRON.LAST_NAME, PATRON.FIRST_NAME;
You need to add these 3 columns to the GROUP BY clause...
PATRON.PHONE, BOOK.BOOK_TITLE, CHECKOUT.DUE_DATE
So it would be...
SELECT
PATRON.LAST_NAME,
PATRON.FIRST_NAME,
PATRON.PHONE,
BOOK.BOOK_TITLE,
CHECKOUT.DUE_DATE,
SUM((SYSDATE - CHECKOUT.DUE_DATE) * 1.00) AS BALANCE
FROM CHECKOUT
JOIN PATRON
ON CHECKOUT.PATRON_ID=PATRON.PATRON_ID
JOIN COPY
ON CHECKOUT.COPY_ID=COPY.COPY_ID
JOIN BOOK
ON COPY.BOOK_ID=BOOK.BOOK_ID
WHERE CHECKOUT.RETURN_DATE IS NULL
AND CHECKOUT.DUE_DATE > SYSDATE
GROUP BY PATRON.LAST_NAME, PATRON.FIRST_NAME, PATRON.PHONE, BOOK.BOOK_TITLE, CHECKOUT.DUE_DATE
ORDER BY PATRON.LAST_NAME, PATRON.FIRST_NAME
Bottom line is, if you have a GROUP BY clause, you can only select columns that are part of the GROUP BY unless you are using some kind of scalar value function on them, like COUNT, SUM, MAX, MIN, etc.
Otherwise if you aren't grouping or using a scalar-value function, multiple rows could have different values for that column, so what would the query results be?