Wikidata : How to count Ollympic medals per participant - wikidata-query-service

https://query.wikidata.org/
This query allows to get the list of olympians having won one medal or more:
SELECT DISTINCT ?pLabel (?p as ?WD) WHERE
{
{?p p:P1344 ?aw.?aw pq:P166 wd:Q15243387.}
UNION {?p p:P1344 ?aw.?aw pq:P166 wd:Q15889641.}
UNION {?p p:P1344 ?aw.?aw pq:P166 wd:Q15889643.}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY ?pLabel
But from that point I cant figure a way to count the number of medals awarded by each participant. Any help would be appreciated.

Related

How to perform Group by then use DISTINCT on other column in pig

I have just starting learning PIG and need small help with the question below . thanks in advance !
For eg: I have input like:
Occupation Category Name
Actress Acting Marion Cotillard
Actor Acting Liam Nelson
Tennis Plyr Athletics Roger Federer
Football Plyr Athletics Neymar
Actor Acting Tom Hanks
Actress Acting Elizabeth Banks
US Senator Politics Elizabeth Warren
Football Plyr Athletics Mesut Ozil
I want to know how many types are there in single category.
For eg:- Acting has two types one is Actress and other is Actor. Hence , result will be 2.
Problem facing : Not able to DISTINCT the output from 'group by Category' using 'Occupation' column. :(
Try this:
x= load '<data>' using PigStorage('\t') as (occupation:chararray,category:chararray,name:chararray);
x_grouped= group x by category;
x_grouped_distinct= foreach x_grouped { cat= distinct $1.occupation; generate $0, cat, COUNT(cat);};
dump x_grouped_distinct;
Distinct first and then Group By Category.Assuming you have already loaded the data into relation A.
Select the 2 columns after the load.
Distinct the relation
Group By category
Count Occupation for each Category
B = FOREACH A GENERATE Occupation as Occupation,Category as Category;
C = DISTINCT B;
D = GROUP C BY $1;
E = FOREACH D GENERATE group,COUNT(C.Occupation);
DUMP E;

LINQ how get max rating

I have a model with the following entities:
Game: Id
User: Id
UserRating: Id, UserId, GameId, Value
Users can give a rating for a game.
I want to have a query that returns the top 5 games with the highest rating. The highest rating should be based on the Value, but when there are 2 or more games with the same rating, the count should also be taken into account.
How do express this in a query, return the result list as Game entities, using lambda expressions ?
(using EF 4, MVC, SQL Server).
I assume that by "highest rating" you mean "highest average rating"? It sounds like you want something like:
var query = from rating in db.UserRating
group rating by rating.GameId into ratings
orderby ratings.Average(x => x.Value) descending,
ratings.Count() descending
join game in db.Game on ratings.Key equals game.Id
select game;
var top5Games = query.Take(5);
EDIT: In non-query-expression form, this would be more painful, though still feasible:
var top5Games = db.UserRating
.GroupBy(rating => rating.GameId)
.OrderByDescending(ratings => ratings.Average(x => x.Value))
.ThenByDescending(ratings => ratings.Count())
.Join(db.Game, r => r.Key, g => g.Id, (r, g) => g)
.Take(5);
In this case doing it in the lambda syntax isn't too bad, as you're only getting the game out of the join... but more generally, it becomes nastier. It's definitely worth understanding and using both forms, depending on which is the simpler approach for the query at hand. In particular, when you start doing multiple joins it becomes horrible in lambda syntax.
Note that this won't give the rating for that game... to get that, you'd probably want something like:
select new { Game = game,
RatingAverage = ratings.Average(x => x.Value),
RatingCount = ratings.Count() }
at the end of the query instead of just select game.
Also note that you may want to exclude games which currently don't have enough ratings to be meaningful yet - otherwise a game with a single rating which is "perfect" will always be at the top.
EDIT: Okay, with the final tie-break of "name of game", I'd definitely use the query expression form:
var query = from rating in db.UserRating
join game in db.Game on ratings.Key equals game.Id
select new { rating, game } into pair
group pair by pair.game into pairs
orderby pairs.Average(x => x.rating.Value) descending,
pairs.Count() descending,
pairs.Key.Name
select pairs.Key;
var top5Games = query.Take(5);

Finding k nearest neighbors with SPARQL query

I would like to write a single SPARQL query to find the k nearest neighbors for a set of vectors. To find the average label for the 100 nearest neighbors for a single vector I can use the following query:
PREFIX : <ml://>
PREFIX vector: <ml://vector/>
PREFIX feature: <ml://feature/>
SELECT (AVG(?label) as ?prediction)
WHERE {
{
SELECT ?other_vector (COUNT(?common_feature) as ?similarity)
WHERE { vector:0 :has ?common_feature .
?other_vector :has ?common_feature .
} GROUP BY ?other_vector ORDER BY DESC(?similarity) LIMIT 100
}
?other_vector :hasLabel ?label .
}
Is there a way to do this for multiple vectors in a single query?
Unless I'm overlooking something, you can do this by replacing the URI vector:0 with a variable, like so:
SELECT ?vector (AVG(?label) as ?prediction)
WHERE {
{
SELECT ?vector ?other_vector (COUNT(?common_feature) as ?similarity)
WHERE { ?vector :has ?common_feature .
?other_vector :has ?common_feature .
FILTER(?vector != ?other_vector)
} GROUP BY ?other_vector ORDER BY DESC(?similarity) LIMIT 100
}
?other_vector :hasLabel ?label .
}
I added a filter condition to check that ?vector and ?other_vector are not equal, whether that is necessary is up to you of course :)
If you need to restrict the list of vectors for which you want to find a match, you can use a VALUES clause to restrict possible bindings for ?vector:
VALUES ?vector { vector:0 vector:1 ... }

Nested Query or Sub query linq

I have two classes. Bills and Transactions. One bill is made up of many transactions. I am able to display bills and I am able to display transactions on their own. But I would like to display the last 10 bills (this part is done), but each bill should show all its transactions.
This part of the code is used to get all transactions of a bill
{ Bill bill = (Bill)Bills.Instance.GetBillsByCustomerID(id);
//get all transactions of bill
var transactions = from t in this._entities.Transactions
where t.Bill.bID == bill.bID
select new
{
t.Product.pName, t.tQty, t.tUnitPrice, t.Bill.bTotal, t.Bill.bTimestamp, t.Bill.bCustomerIDF
};
}
Now I would like that the following query below, would have some sort of nested query where all transactions OF EACH BILL are obtained: (at the moment, this only displays 10 bills - and no transactions
{
//returns top 10
var bills = (from b in this._entities.Bills
where b.bCustomerIDF == id
orderby b.bTimestamp descending
select new { b.bTotal, b.bTimestamp, b.Customer.cName}).Take(10);
return bills;
}
Can you please guide me to a simple solution? Thank you
I believe you want a join with an into
var bills = (from b in this._entities.Bills
join t in this._entities.Transactions on t.Bill.bID equals b.bID into tg
where b.bCustomerIDF == id
orderby b.bTimestamp descending
select new
{
b.bTotal,
b.bTimestamp,
b.Customer.cName,
Transactions = tg
}
).Take(10);
return bills;
I would have thought that you should just be able to add something like the following into your select:
transactions.Where(x=>x.Bill.bID == b.bID)`
That having been said I do also think it sounds like your object model is wrong. I'd have expected a Bill to have a collection of Transactions that are on that Bill.

multiple grouping, inner joning in Linq

I am trying to translate this into Linq and cannot figure it out:
SELECT
CustomerOrder.ShipState, MONTH(OrderFulfillment.OrderDate) AS Mnth,
YEAR(OrderFulfillment.OrderDate) AS Yer,
SUM(OrderFulfillment.Tax) AS TotalTax
FROM
OrderFulfillment INNER JOIN
CustomerOrder ONOrderFulfillment.OrderID =CustomerOrder.OrderID
WHERE
(OrderFulfillment.Tax > 0)
GROUP BY
CustomerOrder.ShipState, MONTH(OrderFulfillment.OrderDate),
YEAR(OrderFulfillment.OrderDate)
ORDER BY
YEAR(OrderFulfillment.OrderDate) DESC, CustomerOrder.ShipState,
MONTH(OrderFulfillment.OrderDate) DESC
I have Linqpad and have gone through a bunch of the examples but cannot figure this out.
I think you want to do something like this:
from c in CustomerOrder
join o in OrderFulfillment on c.OrderId equals o.OrderId
where
o.Tax > 0
group o by
new { c.ShipState, Mnth = of.OrderDate.Month, Yer = of.OrderDate.Year }
into g
orderby
g.Key.Yer descending, g.ShipState, g.Key.Mnth descending
select
new { g.Key.ShipState, g.Key.Mnth, g.Key.Yer,
TotalTax = g.Sum(i => i.Tax) };
I haven't tried to compile it, but I think this is something along the lines of what you want.
The idea is that first you perform your join to link the customers and orders. Then apply your filter condition.
At that point, you want to get all the orders that have a particular group, so the group operator is applied.
Finally, order the results, then select out all info from the keys for each group, and sum up the tax in each of the group.
First of all, it would be nice to know what exactly you can't figure out. If you're completely lost and don't know where to begin then you need to google around for linq joining and grouping.
Here's something I did recently that may (possibly) point you in the right direction:
// This groups items by category and lists all category ids and names
from ct in Categories
join cst in Category_Subtypes
on ct.Category_Id equals cst.Category_Id
join st in Subtypes
on cst.Subtype_Id equals st.Subtype_Id
where
st.Type_Id == new Guid(id)
group ct by new { ct.Category_Id, ct.Display_Text } into ctg
select new
{
Id = ctg.Key.Category_Id,
Name = ctg.Key.Display_Text
}

Resources