not getting output in group by using linq query - linq

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

Related

Count rows from 1 table that have more than x rows in another table

I have 3 tables - Folders, Documents & Versions
FolderID
Folder Name
1
Folder 1
2
Folder 2
3
Folder 3
Documents looks like this:
DocID
Doc Name
FolderID
1000
Doc 1
1
1001
Doc 2
1
1002
Doc 3
2
1003
Doc 4
2
1004
Doc 5
3
Versions looks like this:
VersionID
DocID
1
1000
2
1001
3
1001
4
1002
5
1003
6
1003
7
1004
So Doc 1, 3 & 5 have 1 version each, and Doc 2 & 4 have 2 versions.
I would like to count the documents that have more than 1 version. In this example Folder 1 & 2 both have 1 document with more than 1 version, and Folder 3 has none.
I'd like some DAX that will accomplish that. I'm managing to confuse myself because the filter is based on a count of a related table.
This is what I came up with, but I know I'm off
Count Docs =
VAR VersionsMin = 2
RETURN
CALCULATE (
COUNT ( 'Documents'[DocID] ),
FILTER ( 'Versions', COUNT ( 'Versions'[VersionID] ) >= VersionsMin )
)
Try this out:
In the first step add a new column to your Documents table
Version Count =
COUNTROWS(RELATEDTABLE(Versions))
In the second step you can use this column for filtering
Docs with multiple versions =
CALCULATE(
COUNT(Documents[DocID]),
Documents[Version Count] > 1
)
This allows you to create the following table visual:

Get Total count

I want to merge two columns(Sender and Receiver) and get the Transaction Type count then merge another table with using Sender_Receiver primary id.
Sender Receiver Type Amount Date
773787639 777611388 1 300 2/1/2019
773631898 776806843 4 450 8/20/2019
773761571 777019819 6 369 2/11/2019
774295511 777084440 34 1000 1/22/2019
774263079 776816905 45 678 6/27/2019
774386894 777202863 12 2678 2/10/2019
773671537 777545555 14 38934 9/29/2019
774288117 777035194 18 21 4/22/2019
774242382 777132939 21 1275 9/30/2019
774144715 777049859 30 6309 7/4/2019
773911674 776938987 10 3528 5/1/2019
773397863 777548054 15 35892 7/6/2019
776816905 772345091 6 1234 7/7/2019
777035194 775623065 4 453454 7/20/2019
Second Table
Mobile_number Age
773787639 34
773787632 23
774288117 65
I am try to get like this kind of table
Sender/Receiver Type_1 Type_4 Type_12...... Type_45 Age
773787639 3 2 0 0 23
773631898 1 0 1 2 56
773397863 2 2 0 0 65
772345091 1 1 0 3 32
Ok, I have seen your old question and you just need inner join in sub-query as following:
SELECT
SenderReceiver,
COUNT(CASE WHEN Type = 1 THEN 1 END) AS Type_1,
COUNT(CASE WHEN Type = 2 THEN 1 END) AS Type_2,
COUNT(CASE WHEN Type = 3 THEN 1 END) AS Type_3,
...
COUNT(CASE WHEN Type = 45 THEN 1 END) AS Type_45,
Age -- changes here
FROM
( SELECT sr.SenderReceiver, sr.Type, st.Age from -- changes here
(SELECT Sender AS SenderReceiver, Type FROM yourTable
UNION ALL
SELECT Receiver, Type FROM yourTable) sr
join <second_table> st on st.Mobile_number = sr.SenderReceiver -- changes here
) t
GROUP BY
SenderReceiver,
Age; -- changes here
Changes done in your previous query are marked with comments -- changes here.
Please replace the name of the <second_table> with the original name of the table.
Cheers!!

calculate the time difference for same column in Spotfire

I am a beginner for Spotfire. I have a problem about the difference calculation for the some column value. A sample table could be like this:
id timestamp state
1 7/1/2016 12:00:01 AM 1
2 7/1/2016 12:00:03 AM 0
3 7/1/2016 12:00:04 AM 1
4 7/1/2016 12:00:06 AM 0
5 7/1/2016 12:00:09 AM 1
6 7/1/2016 12:00:10 AM 0
7 7/1/2016 12:00:12 AM 1
I want to calculate the time difference for the timestamp when the state is 1,
the final table I want to have is:
id timestamp state time_diffence
3 7/1/2016 12:00:04 AM 1 3
5 7/1/2016 12:00:09 AM 1 5
7 7/1/2016 12:00:12 AM 1 3
it seems that I should identify an expression for the calculation, but I have not idea for the calculation just for one parameter :(. somebody could help me ?
still one more small question: what if the timestamp column value is just number value, how can i calculate the difference, is there any related function like DateDiff() here? for example:
id times state
1 12 1
2 7 0
3 10 1
4 11 0
5 6 1
6 9 0
7 7 1
the result could be :
id times state diffence
3 10 1 -2
5 6 1 -4
7 7 1 1
after running the code: i have the error as below:
for the row if it has the same time stamp as the last previous row, the difference will keep same as before, but actually the difference for the rows which have same time stamp would be as 0
thanks for your help :)
Assuming your data is sorted in ascending order by [timestamp] before you import it, you can partition using the Previous function with Over where the [state]=1.
Insert a calculated column with this expression:
If([state]=1,DateDiff("ss",Min([timestamp]) OVER (Previous([timestamp])),[timestamp]))
You will see it populated in your table like the below:
Then if you ONLY want to see the rows that have the difference you mentioned, on your table you can...
Right Click > Properties > Data > Limit data using expression >
And insert the expression: [time_difference] > 1
This will result in this table:

How to return unique column with the max version of another column in Linq to SQL?

I need return a query where all the unique page numbers are returned with the max version number of each page.
Here is the an example of the data that I'd query
DocumentID PageNumber Version
1 1 1
1 2 1
1 2 2
1 3 1
1 3 2
1 3 3
And here is what I would need to get returned in my query
DocumentID PageNumber Version
1 1 1
1 2 2
1 3 3
Not sure how to finish this:
var pages = from p in dc.Pages where p.DocumentID == 1 && ...
I think this is what you're trying to achieve:
var results =
from p in dc.Pages
where p.DocumentID == 1
group p by p.PageNumber into g
select new
{
PageNumber = g.Key,
MaxVersion = g.Max(x => x.Version)
};
This query may help you:
Select DocumentID ,Distinct PageNumber, max(version) from table
group by DocumentID, Distinct PageNumber

Getting Distinct Values with 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.

Resources