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

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;

Related

Transforming SQL query for product recommendation to Cypher query

I have a recommendation system that uses SQL query similar to this one:
select B.* from user User1
join rating Rating1 on User1.user_id = Rating1.id and Rating1.value = 5
join product A on A.id = Rating1.product_id
join rating Rating2 on Rating2.product_id = A.id and Rating2.value = 5
join user User2 on User2.id = Rating2.user_id and User2.id <> User1.id
join rating RatingB on RatingB.user_id = User2.id and RatingB.value =5
join product B on B.id = RatingB.product_id
WHERE User1.id = 1;
This system recommends a product to a user if they didn’t buy that product yet. The recommendation is based on the rating of the product that they gave the best rating and it looks which other users also rated that product with the same rating.
How would this query look like in Cypher?
The mentioned SQL query would look something like this if it was written using Cypher:
MATCH (pA:PRODUCT)<-[r1:Rated {"rating":5}]-(n1:USER)-[r2:Rated {"rating":5}]->(pB:PRODUCT)
MATCH (n2:USER {id:1})-[r3:Rated {"rating":5}]->(pb)
WHERE n1.id != n2.id
RETURN pB;

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)

Creating Oracle View with using a Composite key table

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;

How can i perform this query in NHibernate for getting child count

select name,
(select count(*) from products where products.category_Id=categories.Id) as productCount
from categories
session.CreateCriteria<Category>()
but whats next?
i don't even know how to search it in Google?
think of your query like
SELECT categories.Id, count(categories.Id)
FROM categories inner join products on products.category_Id=categories.Id
group by categories.Id
I think they will produce the same result.
search google for
nhibernate criteria join
and
CreateAlias

LINQ - join the highest rank picture of a product that has many pictures

The query below gets all customers and the products they order. There are multiple pictures per product, so the query returns duplicate rows. I only want one of the pictures to prevent this duplication. The pictures have a rank order. So, I need to sort the pictures and take the one with highest rank and join that to products. How can I do this?
from c in Customers
join o in OnlineOrders on c.CustomerID equals o.CustomerID into ords
from co in ords.DefaultIfEmpty()
join pro in Products on co.OnlineOrderID equals pro.OnlineOrderID into oprods
from op in oprods.DefaultIfEmpty()
join pict in Pictures on op.ProductID equals pict.ProductID into picpros
from ppro in picpros.DefaultIfEmpty()
select new {
c.CustomerName, co.OnlineOrderTitle,
op.ProductTitle, ppro.PictureFilename }
I figured it out.
from ppro in picpros.DefaultIfEmpty().Take(1)
var result=db.OnlineOrders.Select(p=>
new {
CustomerName=p.Customer.CustomerName,
OnlineOrderTitel=p.OnlineOrderTitel,
ProductTitel=p.Protuct.ProductTitel,
PictuteFileName=p.Procuct.Pictures.OrderByDescending(u=>u.Rank).First()) });
I guess Product has one to many OnlineOrder relation, if not(namy to many) I have to know your structure. Do you have "duplicates" in OnlineOrder table or OnlineOrderProduct table?

Resources