Getting Distinct Values with LINQ - linq

i need to be able to get distinct values grouped by FileID and by SentToID
this is what i have now, and it only groups by SentToID, which is missing some records.
var sentByResults = from v in ctx.vEmailSents
where v.TypeDesc.Equals("Request")
group v by v.SentTo_ID into g
select g.OrderByDescending(x => x.DateSent).FirstOrDefault() into lastV
select new
{
ClaimID = lastV.Claim_ID,
SentToID= lastV.SentTo_ID,
};
so if i have 5 records
claim id fileid sentToID
1 15 27
1 16 27
1 15 26
1 15 26
1 15 47
right now i get 3 records back, one for each unique sentToID, but i need to get 4 records back, for each unique ID within each unique fileID

I suspect you just want to group by an anonymous type:
group v by new { v.SentTo_ID, v.FileID }
Also, given that you'll never get any empty groups, you should be able to use First instead of FirstOrDefault.

Related

Select and sum multiple columns for statistic purposes with Laravel query

I have one table scores where I have saving users scores. It's looks like this
table `scores`
id | points | user_id
1 5 1
2 2 1
3 4 1
4 1 3
5 10 2
I want to select each user, sum his points and show as a ranking. The result from above should be
user_id | points
1 11
2 10
3 1
The query with which I came up is
$sumPoints = Scores::select( \DB::raw("sum(points) as numberOfPoints"), \DB::raw("count(id) as numberId"))->groupBy("user_id")->first();
The problem is in ->first() because it's return only one result.. it is working as must. If I try to use ->get() instead I've got Undefined property error. How should I use this?
The query which is working in phpmyadmin
SELECT count(id) as numberId, sum(points) as numberOfPoints FROM `points` GROUP BY `user_id`
You can use something like this
$sumPoints = Scores::select( \DB::raw("sum(points) as numberOfPoints"), \DB::raw("count(id) as numberId"))->groupBy("user_id")->get();
foreach($sumPoints as $point){
dd($point); //OR dd($point->numberOfPoints)
}

Pig Script Based On Grouping

I have a data-set like this.
cus_ID BRAND AMOUNT
1 5 10
2 4 20
3 5 15
1 5 20
1 4 30
2 3 15
I want to find top 5 brands and top 10 customer id's of each of those top 5 brands using PIG.
For your first goal (find top 5 brands), here you go (code not tested):
mydata = LOAD ... <load your data from your file or other source>
grouped = GROUP mydata BY brand;
flattened = FOREACH grouped GENERATE
FLATTEN(group) AS brand,
SUM(mydata.amount) AS amount_per_brand;
topfivebrand = LIMIT (ORDER flattened by amount_per_brand DESC) 5;
dump topfivebrand;
That should get you started! :)

Sum multiple columns using PIG

I have multiple files with same columns and I am trying to aggregate the values in two columns using SUM.
The column structure is below
ID first_count second_count name desc
1 10 10 A A_Desc
1 25 45 A A_Desc
1 30 25 A A_Desc
2 20 20 B B_Desc
2 40 10 B B_Desc
How can I sum the first_count and second_count?
ID first_count second_count name desc
1 65 80 A A_Desc
2 60 30 B B_Desc
Below is the script I wrote but when I execute it I get an error "Could not infer matching function for SUM as multiple of none of them fit.Please use an explicit cast.
A = LOAD '/output/*/part*' AS (id:chararray,first_count:chararray,second_count:chararray,name:chararray,desc:chararray);
B = GROUP A BY id;
C = FOREACH B GENERATE group as id,
SUM(A.first_count) as first_count,
SUM(A.second_count) as second_count,
A.name as name,
A.desc as desc;
Your load statement is wrong. first_count, second_count is loaded as chararray. Sum can't add two strings. If you are sure that these columns will take numbers only then load them as int. Try this-
A = LOAD '/output/*/part*' AS (id:chararray,first_count:int,second_count:int,name:chararray,desc:chararray);
It should work.

not getting output in group by using linq query

My table is:
Student_answer_Master:
Stud_ans_id QuestionId OptionId iscorrect
1 25 1 1
2 26 1 0
3 27 2 1
4 27 3 1
5 27 4 1
6 28 1 1
8 29 1 0
9 29 2 1
10 29 3 1
(my options are single choice and multiple choice(checkbox)).
now what i want to get total count of only those question which are correct.
1-indicate correct
0-indicate incorrect.
i want output:3(for questionid 25,27,28)
this is my linq query:
var data = (from temp in context.Student_Answer_Master
where temp.isCorrect == '1'
group temp by temp.Question_Id into g
select new {g.Key}).Count();
but here count is coming 0.
can any one tell me what is wrong with my query???
Try this this should work.
var data = (from temp in s
group temp by temp.Question_Id into g
select new { questionid = g.Key, minIscorrect = g.Min(x => x.isCorrect) }).Where(y => y.minIscorrect != 0).Count();
There are 2 things that looks wrong to me in your Query.
comparing an int to a char temp.isCorrect == '1' if you make it temp.isCorrect == 1, you will still get 4 as the count because you are eliminating the row that has wrong answer for Question id 29.
once you get the group, you need to check if there are any wrong answers for the question
if yes eliminate that and then put a count

How can I get values from one table to another via similar values?

I have a table called excel that has 3 columns, name, id, and full_name. The name part is the only one I have and I need to fill id and full_name. The other table that contains the data is called tim_pismena and has 2 columns that I need, id and pismeno_name (the actual names are not important, but i'm writing them just for clarity). In pseudooracle code :) the select that gets me the values from the second table would be done something like this:
SELECT tp.id, tp.pismeno_name
FROM tim_pismena tp
WHERE upper(tp.pismeno_name) LIKE IN upper('%(SELECT name FROM excel)%')
and when used with an insert, the end result should be something like
name id full_name
Happy Joe 55 Very fun place Happy Joe, isn't it?
Use merge statement
1 MERGE
2 INTO excel tgt
3 USING tim_pismenae src
4 ON ( upper(src.naziv_pismena) LIKE '%'||upper(tgt.ime)||'%')
5 WHEN MATCHED
6 THEN
7 UPDATE
8 SET tgt.id = src.id
9 , tgt.full_name = src.naziv_pismena
10 WHEN NOT MATCHED
11 THEN
12 INSERT ( tgt.name
13 , tgt.id
14 , tgt.full_name )
15 VALUES ( src.naziv_pismena
16 , src.id
17 , src.naziv_pismena )
18 WHERE (1 <> 1);

Resources