Invalid identifier when using subset query - oracle

I have the following query:
SELECT (SELECT SUM(adults+children) as qty from reservation where id = 11407) as qty,
SUM((price * qty) - (price * nvl(qty_excluded,0)))
FROM reservation_product
WHERE id = 11407
I get the following message: qty invalid identifier.

Your query looks strange. There is some ID in the tables, and you say it's neither their ID, nor the reservation ID. But you have it in both tables, so a reservation can refer to a different as its reservation products do.
And in your query you don't care whether the reservation products you select belong to the reservations you select.
Anyway, your query simply transfered is:
SELECT MAX(r.qty),
SUM((rp.price * r.qty) - (rp.price * nvl(rp.qty_excluded,0)))
FROM reservation_product rp
CROSS JOIN
(
SELECT SUM(adults+children) as qty
FROM reservation
WHERE id = 11407
) r
WHERE id = 11407;

Related

can i set up an SSRS report where users input parameters to a table

I have an oracle query that uses a created table as part of the code. Every time I need to run a report I delete current data and import the new data I receive. This is one column of id's. I need to create a report on SSRS in which the user can input this data into said table as a parameter. I have designed a simple report that they can enter some of the id's into a parameter, but there may be times when they need to enter in a few thousand id's, and the report already runs long. Here is what the SSRS code currently says:
select distinct n.id, n.notes
from notes n
join (
select max(seq_num) as seqnum, id from notes group by id) maxresults
on n.id = maxresults.ID
where n.seq_num = maxresults.seqnum
and n.id in (#MyParam)
Is there a way to have MyParam insert data into a table I would join called My_ID, joining as Join My_Id id on n.id = id.id
I do not have permissions to create functions or procedures in the database.
Thank you
You may try the trick with MATERIALIZE hint which normally forces Oracle to create a temporary table :
WITH cte1 AS
( SELECT /*+ MATERIALIZE */ 1 as id FROM DUAL
UNION ALL
SELECT 2 DUAL
)
SELECT a.*
FROM table1 a
INNER JOIN cte1 b ON b.id = a.id

ORA-00904: invalid column name but I am using the correct column name

Can someone see where I am going wrong in the below query? I am getting the error message that the GROUP BY column doesn't exist, but it clearly does as I see that column name in the output when I don't use the GROUP BY.
SELECT
(SELECT customer_address.post_code FROM customer_address WHERE customer_address.address_type = 0 AND customer_address.customer_no = orders.customer_no) postcode, SUM(orders.order_no) orders
FROM
orders, customer_address
WHERE
orders.delivery_date = '27-MAY-15'
GROUP BY
postcode;
The answer is: You cannot use an alias name in GROUP BY.
So:
GROUP BY (SELECT customer_address.post_code ...);
Or:
select postcode, sum(order_no)
from
(
SELECT
(SELECT customer_address.post_code FROM customer_address WHERE customer_address.address_type = 0 AND customer_address.customer_no = orders.customer_no) postcode,
orders.order_no
FROM orders, customer_address
WHERE orders.delivery_date = '27-MAY-15'
)
GROUP BY postcode;
EDIT:
However, your query seems wrong. Why do you cross-join orders and customer_address? By mistake I guess. Use explicit joins (INNER JOIN customer_address ON ...), when using joins to avoid such errors. But here I guess you'd just have to remove , customer_address.
Then why do you add order numbers? That doesn't seem to make sense.

Calculated field returning same value SQL

I have an issue with the below sub-query:
(select AVG(retail)
from STOCK
where category = 'TOYOTA' or category = 'HONDA') as AVERAGE_SALE_PRICE
Entire query:
select
d.name, s.category,(select AVG(retail)
where category = 'TOYOTA' or category = 'HONDA') as AVERAGE_SALE_PRICE
from dealer d join stock s using (dealerID)
The issue is that this calculated field returns the same value for all the rows in the query, I understand that I may have add a GROUP BY but I am quite confused where...
Thanks for any help
Try this query:
select AVG(retail) as AVERAGE_SALE_PRICE, category
from STOCK
where category='TOYOTA' or category='HONDA'
group by category
Update.This should give you the desired results:
select
d.name,
s.category,
(select AVG(s.retail)
from stock s1
where s1.dealerID = s.dealerID
and (s1.category = 'TOYOTA' or s1.category = 'HONDA') as AVERAGE_SALE_PRICE
from dealer d
join stock s using (dealerID)

Display only the largest count in a group by statment

I'm trying to display only the largest group in this group by statement;
SELECT COUNT(type) AS booking, type FROM booking b, room r WHERE r.rno = b.rno AND r.hno = b.hno GROUP BY type;
I modified it so we get this query response now you can see group double is larger then family.
BOOKING TYPE
5 double
2 family
I know there is a HAVING keyword you can add in order display only a count compared to a number so I could do COUNT(type) HAVING > 2 or similar but that's not very dynamic and that would only work in this instance because I know the two amounts.
ORDER BY COUNT(type) DESC LIMIT 1
There isn't a having statement that does this. But you can use rownum with a subquery:
select t.*
from (SELECT COUNT(type) AS booking, type
FROM booking b join
room r
on r.rno = b.rno AND r.hno = b.hno
GROUP BY type
order by count(type) desc
) t
where rownum = 1;
Just order your query..
order by booking desc
regards
TRY this
SELECT COUNT(type) AS booking, type FROM booking b, room r WHERE r.rno = b.rno AND r.hno = b.hno ORDER BY type DESC LIMIT 1

Oracle/SQL - Join one table multiple times to the same table

I'm trying to wrap my brain around a query and hoping you can help. I have two tables: a customer_table and a product_table that look like this
name sku_num1 sku_num2 sku_num3 sku_num4
----------------------------------------------------
Bob A B C D
Frank E A
Tom G
Shelly G E
Justin E G A
sku_num widget_name
-------------------
A widget_a
B widget_b
C widget_c
D widget_d
So what I want to do is return a list of all the customers who have at least 1 product whose SKU appears in the product table. So with the above information I would receive back the records for
Bob
Frank
Justin
Any ideas how to do this?
There is no need to join one table twice:
SELECT *
FROM customers
WHERE sku_num1 IN (SELECT sku_num
FROM product_table)
OR sku_num2 IN (SELECT sku_num
FROM product_table)
....
The reason why you need multiple conditions that do a full table scan on the product_table is your wrong database design. The products a customer bought should not go into multiple columns in the customer table. Instead you should have a third table that is a N:M relation between customer and products.
Does something like this not work?
select name
from customer c, products p
where ( c.sku_num1 = p.sku)
or ( c.sku_num2 = p.sku)
or ( c.sku_num3 = p.sku)
or ( c.sku_num4 = p.sku)
Christopher,
Does this mean that you are just looking for any customers where sku_num1 or sku_num2 or sku_num3 or sku_num4 is not null?
If that's the case, you could do it two different ways.
select * from customers
where sku_num1 is not null
or sku_num2 is not null
or sku_num3 is not null
or sku_num4 is not null
If you are trying to see if they have ordered something from a specific product list, you could modify this to:
select * from customers
where sku_num1 in (select sku_num from skus)
or sku_num2 in (select sku_num from skus)
or sku_num3 in (select sku_num from skus)
or sku_num4 in (select sku_num from skus)
Incidentally, this is why people normally don't structure tables like this. There should be another join table, for lack of anything better to call it, customer_skus which would tie together customers and skus and just have a customerID and a sku_num.
This would make the query easier to write, read, and maintain. The query would look something like:
select distinct name from customers, customer_skus, skus
where customers.id = customer_skus.id
and customer_skus.sku_num = skus.sku_num
You can join a table with itself by using an alias:
select * from mytable a, mytable b where condition

Resources