Group by datatable using integer range using Linq - linq

I'm trying to group a set of data based on the range of an age(interger) using linq,
e.g. I have datatable -
id name age
1 abc 20
2 pqr 45
3 jkl 34
5 xyz 39
6 lmn 65
I want result as -
age range count
18-29 1
30-39 2
40-49 1
50-59 0
60-69 1
.
.
.
I would like to group datatable based on the age with appropriate age range and display the 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!!

Oracle reduce result set on field duplication

I have a result set of a select in Oracle (12c) as the following:
GROUP_ID NAME ORDERING
1 AA 0
1 AA 1
1 AB 2
1 AC 3
2 BA 1
2 BA 2
2 BB 3
2 BC 4
I do not know how I could reduce the result set to remove rows based on one column while keeping the other fields. The expected outcome looks like the following:
GROUP_ID NAME ORDERING
1 AA 1
1 AB 2
1 AC 3
2 BA 2
2 BB 3
2 BC 4
I tried to solve it using group by but it got rid of the required field ordering. I am not an expert on window functions but I think it could be a valid attempt to use one.
From your data, it seems that you only need:
select group_id, name, max(ordering)
from yourTable
group by group_id, name

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.

linq group by First()

I have the following list
ID Counter SrvID FirstName
-- ------ ----- ---------
1 34 66M James
5 34 66M Keith
3 55 45Q Jason
2 45 75W Mike
4 33 77U Will
What I like to do is to order by ID by ascending and then get the first value of Counter, SrvID which are identical (if any).
So the output would be something like:
ID Counter SrvID FirstName
-- ------ ----- ---------
1 34 66M James
2 45 75W Mike
3 55 45Q Jason
4 33 77U Will
Note how ID of 5 is removed from the list as Counter and SrvID was identical to what I had for ID 1 but as ID 1 came first I removed 5.
I tried the following but not working:
var query = from record in list1
group record by new {record.Counter, record.SrvID }
into g
let winner = (from groupedItem in g
order by groupedItem.ID
select groupedItem ).First()
select winner;
I get the followng message:
The method 'First' can only be used as a final query operation.
The funny thing is the full error message is:
"NotSupportedException: The method 'First' can only be used as a final query operation. Consider using the method 'FirstOrDefault' in this instance instead."
I have had a problem with using First in Entity Framework, have you tried changing to FirstOrDefault ?

Resources