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

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?

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;

join and group-by in Hadoop Pig

Often see people are using group by and join for the same problem, suppose I have a student table and score table, want to find student name with related course score. It seems we can resolve this problem by either using join, or using group by? Wondering pros and cons for the two solutions. Post data structure and code below. Thanks.
table students:
student ID, student name, student email address
score table:
student ID, course ID, score
student_scores = group students by (studentId) inner, scores by (studentId);
student_scores = join students by student Id, scores by studentId;
In the Pig Latin Manuall about Join it says:
Note the following about the GROUP/COGROUP and JOIN operators:
The GROUP and JOIN operators perform similar functions. GROUP creates a nested set of output tuples while JOIN creates a flat set of output tuples.
The GROUP/COGROUP and JOIN operators handle null values differently (see Nulls and JOIN Operator).
Not sure if it pros & cons , but they are diffrent

Creating database view in migration laravel 5.2

I'm trying to create a database view via a migration in Laravel 5.2 as I need to hand a fairly complex query to a view. I have models / tables for leagues, teams, players, and points. Each one has a hasMany / belongsTo relationship with the one before it. The goal is to create a table where each row is a league name, a sum of all remaining points for the league, and a count of points.remnants where the value of points.remnants > 4.
Major Edit:
What I have so far is
DB::statement( 'CREATE VIEW wones AS
SELECT
leagues.name as name,
sum(points.remnants) as trem,
count(case when points.remnants < 4 then 1 end) as crem
FROM leauges
JOIN teams ON (teams.league_id = leagues.id)
JOIN players ON (players.team_id = teams.id)
JOIN points ON (points.player_id = players.id);
' );
This does not throw any errors, but it only returns one row and the sum is for all points in all leagues.
What I'm looking for is to create a table where there is a row for each league, that has league name, total remaining points for that league, and total points with less than 4 remaining per league.
Marked as solved. See the accepted answer for most of this issues. The one row problem was because I wasn't using GROUP BY with the count().
It looks to me like the problem is your SQL syntax. Here's what you wrote:
CREATE VIEW wones AS SELECT (name from leagues) AS name
join teams where (leagues.id = team.country_id)
join players where (teams.id = players.team_id)
join points where (players.id = points.player_id)
sum(points.remnants) AS trem
count(points.remnants where points.remnants < 4) AS crem
The problem is with the way you've mixed FROM and JOIN clauses with column specifications. Try this:
CREATE VIEW wones AS
SELECT
leagues.name,
sum(points.remnants) AS trem
sum(IF(points.remnants<4, 1, 0)) AS crem
FROM leagues
JOIN teams ON (leagues.id = team.country_id)
JOIN players ON (teams.id = players.team_id)
JOIN points ON (players.id = points.player_id);
I've reformatted it a bit to make it a little clearer. The SQL keywords are capitalized and the various clauses are separated onto their own lines. What we're doing here is specifying the columns, followed by the table specifications - first the leagues table, then the other tables joined to that one.

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
};

Linq to SQL: order by value in related table

I have 2 tables which in simplified form look like this:
Products(
id: int,
name: varchar
);
ProductSpecs(
product_id: int,
spec_name: varchar,
spec_value: int
);
Now I need to sort products (in linq to sql) by value of some specification item (eg. "price"). So I do something like this
var products = from p in db.Products
from ps in p.ProductsSpecs
where ps.spec_name == "price"
orderby ps.spec_value
select p;
The problem is that if there's no such ProductSpec with spec_name "price" the product is not included at all. I can add these products with Union or Concat but this way the sorting of the first part is not preserved.
What is the best way to deal with this?
Thanks.
First, I would recommend that you either do this in pure SQL as a function or Stored Procedure and then access this through linq, or add a price column to your product table. It seems like price would be a normal attribute to add to all of your products even if that price is NULL.
SQL:
select p.*
from products p
left outer join productspecs ps on
p.id = ps.product_id
and ps.spec_name = 'Price'
order by ps.spec_value
With that said, here's the weird bit of LINQ that should work on your table (I might have some of the column names spelled incorrectly):
var products = from p in db.Products
join ps in (from pss in db.ProductSpecs
where pss.spec_name== "Price"
select pss
) on p.id equals ps.product_id into temp
from t in temp.DefaultIfEmpty()
orderby t.spec_value
select p;
I tested this on some tables setup like above and created 5 products, three with prices in different value orders and this LINQ ordered them just like the SQL above and returned the null result rows as well.
Hope this works!
In ordinary SQL, you'd use an LEFT OUTER JOIN. This preserves rows that appear in the left-hand table (the one listed first), even when there's no matching row in the right-hand table (the second one listed, and the one that is outer joined). You end up with nulls for the values that should be, but weren't, present in the right-hand table. So, the price for those items missing a price would appear as NULL.
What that translates to in LINQ to SQL is another matter.
You might care to think about whether it is reasonable to have products that do not have a price. You're emulating something called EAV - Entity, Attribute, Value - tables, and they are generally regarded as 'not a good thing'.
Can you not just do a simple join?
var products =
from p in db.Products
join ps in db.ProductSpecs on p.id equals ps.product_id
where ps.spec_name == "price"
orderby ps.spec_value
select p;

Resources