Creating Oracle View with using a Composite key table - oracle

I am looking into creating a view that holds details for movies. I have to select The category name and id's, Number of films in each category and the average rental rate of each category.
The problem is that I am uncertain how to to this using the composite table in the middle. I have problem matching the average "rental_rate" with the category.name. I have used allot of code to make this work but this is my my version I came closest to, aswell as the table layout I used:
SELECT category_id, category.name, COUNT(category.film), AVG(rental_rate)
FROM film_category
FULL OUTER JOIN category USING (film_category.category_id)
FULL OUTER JOIN film USING (film_category.film_id)
GROUP BY category_id;
Error I am currently Getting:
(category.name) is not a group function.

Add category.name into GROUP BY clause:
SELECT category_id, category.name, COUNT(category.film), AVG(rental_rate)
FROM film_category
FULL OUTER JOIN category USING (film_category.category_id)
FULL OUTER JOIN film USING (film_category.film_id)
GROUP BY category_id, category.name;

Related

how to get the count of customers and display them against product?

I am able to get the count of customers against the product but for me it is difficult to display like how i have mentioned in the above image.
You could use a left join and count function:
select p.product,
count(c.name) as cust_count
from product p
left join customer c
on p.product = c.product
group by p.product;
This is assuming you need zero count for the products that don't have any customers.
If that's not the case, you can use simple aggregation:
select product,
count(*) as cust_count
from customer
group by product;

ORACLE Select list inconsistant with group by

I can't create my view and I don't know how to fix it.
when trying to create the view it tries to add all the columns from select to the group by, which isn't what I want. and doesn't work anyways
CREATE OR REPLACE FORCE VIEW CustomerSaleHistoryView AS
Select SALE.SaleID,
SALE.SaleDate,
CUSTOMER.LastName,
CUSTOMER.FirstName,
SALE_ITEM.SaleItemID,
SALE_ITEM.ItemID,
ITEM.ItemPrice,
SUM(SALE_ITEM.ITEMPRICE),
AVG(SALE_ITEM.ITEMPRICE)
from customer
join sale on customer.CUSTOMERID = Sale.CUSTOMERID
join sale_item on sale_item.saleid = sale.saleID
join item on sale_item.itemID = item.itemID
group by CUSTOMER.LastName, CUSTOMER.FirstName, SALE.SaleID;
In order for you query to work you MUST group by any columns that are not being aggregated with a formula like SUM() or AVG(). In your case GROUP BY SALE.SaleID, SALE.SaleDate, CUSTOMER.LastName, CUSTOMER.FirstName, SALE_ITEM.SaleItemID, SALE_ITEM.ItemID, ITEM.ItemPrice
What are you trying to accomplish that you don't believe this GROUP BY is appropriate?
When using group by clause the list of columns that you are selecting should be either part of the group by clause or they should be part of an aggregate function. In your case SALE.SaleDate, SALE_ITEM.SaleItemID, SALE_ITEM.ItemID and ITEM.ItemPrice doesn't satisfy that rule. You need to include these to your group by clause. Once you fix the select statement and when it returns the desired output convert that into a view.

Linq query for one to many

My question involves MVC + Linq query. I will try to make it simple without going into the details of the Model, View, etc.. Say I have 2 tables T1 & T2. T1 holds restaurants details & T2 holds restaurants image paths. T2 rows contain restaurantID. Now if T2 has more than one rows of image paths for a Restaurant and I only need the first image path from T2 in the linq query how would I form such query? I tried to simplify the question as in fact I have 6 table joins related to the Restaurants in the query. I formed a view model which only contains the fields I want to display. I am trying to populate the view model in the controller & the query is in the controller obviously.
When I join T2 to the query, I get all the Restaurants details together with the images. But the view repeats the same Restaurant as many times as the number of table rows in T2 which is not what I want. This is the problem from the way I set the query. The query uses joins. I only need the first row from T2 while I get all from the Restaurant details. I failed to find an example for such requirement on the web so far. Your directions will be much appreciated.
Serhat Albayoglu
On your join you can use an into and then in the select you can select the FirstOrDefault
var query = from t in context.T1
join t2 in context.T2 on t.Id equals t2.RestaurantID into tgroup
select
{
t2.FirstOrDefault().path
};

Active Record Join with most recent association object attribute

I have a Contact model which has many Notes. On one page of my app, I show several attributes of a table of contacts (name, email, latest note created_at).
For the note column, I'm trying to write a joins statement that grabs all contacts along with just their latest note (or even just the created_at of it
What I've come up with is incorrect as it limits and orders the contacts, not their notes:
current_user.contacts.joins(:notes).limit(1).order('created_at DESC')
If you just want the created_at value for the most recent note for each contact, you can first create a query to find the max value and then join with that query:
max_times = Note.group(:contact_id).select("contact_id, MAX(created_at) AS note_created_at").to_sql
current_user.contacts.select("contacts.*, note_created_at").joins("LEFT JOIN (#{max_times}) max_times ON contacts.id = max_times.contact_id")
If you want to work with the Note object for the most recent notes, one option would be to select the notes and group them by the contact_id. Then you can read them out of the hash as you work with each Contact.
max_times = Note.group(:contact_id).select("contact_id, MAX(created_at) AS note_created_at").to_sql
max_notes = Note.select("DISTINCT ON (notes.contact_id) notes.*").joins("INNER JOIN (#{max_times}) max_times ON notes.contact_id = max_times.contact_id AND notes.created_at = note_created_at").where(contact_id: current_user.contact_ids)
max_notes.group_by(&:contact_id)
This uses DISTINCT ON to drop dups in case two notes have exactly the same contact_id and created_at values. If you aren't using PostgreSQL you'll need another way to deal with dups.

Retrieving a list of rows with only a single item from other table in Oracle

I have two tables in Oracle database (10g express)
product
product_image
One product can have multiple images. Hence, there a one-to-many relationship from product to product_image and the product_image table has a foreign key that refers to the primary key of the product table.
I need to fetch a list of products with only a single image name in each row of the result set being retrieved regardless of the images being in the product_image table (even though there are no images for some of products).
The image name to be retrieved from the product_image table is generally the first image name in the product_image table after sorting each set of images for each product in ascending order. Something like the following.
prod_id prod_name prod_image
1 aaa aaa.jpg //The first image name in the product_image table after sorting images for prod_id in ascending order.
2 bbb bbb.jpg //Similar to the first case.
3 ccc - //No image(s) found in the product_image table
4 ddd - //Similar to the previous case.
The general join statement for these two tables would be something similar to the following.
SELECT p.prod_id, p.prod_name, pi.prod_image
FROM product p
INNER JOIN product_image pi
ON p.prod_id=pi.prod_id;
Is this possible using a single SQL statement?
If I understood your question correctly I think the following query will work. I have not tested it.
SELECT p.prod_id, p.prod_name, MIN(DBMS_LOB.substr(pi.prod_image, 1))
FROM product p LEFT JOIN product_image pi
ON p.prod_id=pi.prod_id
GROUP BY p.prod_id, p.prod_name
ORDER BY p.prod_name;
Try this, you can generate row number for each image per prod_id and use that. To return null or blank from missing product image you should use left outer join
WITH CTE AS
(
SELECT prod_id, DBMS_LOB.substr(prod_image,1,4000) prod_image,
row_number() over (partition by prod_id order by prod_image) rn
FROM product_image
)
SELECT p.prod_id, p.prod_name, nvl(pi.prod_image,'-') prod_image
FROM product p
LEFT OUTER JOIN CTE pi
ON p.prod_id=pi.prod_id and pi.rn = 1;
The following SQL worked as mentioned in the question.
SELECT
p.prod_id,
p.prod_name,
t.prod_image
FROM
product p
LEFT OUTER JOIN(
SELECT
pi.prod_image_id,
pi.prod_id,
pi.prod_image
FROM
product_image pi
INNER JOIN (
SELECT
MIN(pi.prod_image_id) AS prod_image_id
FROM
product_image pi
GROUP BY
pi.prod_id
) prod_image
ON pi.prod_image_id=prod_image.prod_image_id
)t ON p.prod_id=t.prod_id ORDER BY p.prod_id DESC;
Reference:
SQL Select only rows with Max Value on a Column

Resources