MEGHA_cs
(MEGHA cs)
March 1, 2024, 8:11am
1
Hi, I have a Data table with multiple columns
Col1 Col2 Col3
1 AAA 100
1 BBB 100
1 AAA 100
2 BBB 300
2 CCC 200
3 AAA 300
3 AAA 100
I need to get the data for each distict values of first two columns and get sum of the values in 3rd column in a new datatable. i.e
Col1 Col2 Col3
1 AAA 200
1 BBB 100
2 BBB 300
2 CCC 200
3 AAA 400
How to implement without using more for loops
Thanks
1 Like
vrdabberu
(Varunraj Dabberu)
March 1, 2024, 8:20am
2
Hi @MEGHA_cs
=> Read Range Workbook
Output-> dt
=> Use below syntax in Assign:
dt_Output = (From row In dt.AsEnumerable()
Group row By Key = New With {Key .Col1 = row.Field(Of Double)("Col1"), Key .Col2 = row.Field(Of String)("Col2")}
Into Group
Select dt.Clone().LoadDataRow({Key.Col1, Key.Col2, CDbl(Group.Sum(Function(g) g.Field(Of Double)("Col3")))}, False)).CopyToDataTable()
=> Write Range Workbook dt_Output
Regards
MEGHA_cs
(MEGHA cs)
March 1, 2024, 8:46am
3
Thanks, but am getting this error
vrdabberu
(Varunraj Dabberu)
March 1, 2024, 8:47am
4
Hi @MEGHA_cs
dt_Output is of DataType System.Data.DataTable
If you have specific columns names specify that according to that I will modify the linq query
Regards
rlgandu
(Rajyalakshmi Gandu)
March 1, 2024, 8:49am
5
@MEGHA_cs
groupedDataTable = (From row In yourDataTable.AsEnumerable()
Group row By Key = New With { Key .Col1 = row.Field(Of Double)("Col1"), Key .Col2 = row.Field(Of String)("Col2") } Into Group
Let sumCol3 = Group.Sum(Function(r) Convert.ToInt32(r("Col3")))
Select yourDataTable.Clone().Rows.Add(Key.Col1, Key.Col2, sumCol3)).CopyToDataTable()
Sequence4.zip (1.9 KB)
vrdabberu
(Varunraj Dabberu)
March 1, 2024, 8:51am
6
Hi @MEGHA_cs
According to the error you have faced below is the modified linq query:
dt_Output = (From row In dt.AsEnumerable()
Group row By Key = New With {Key .Country = row.Field(Of String)("Country"), Key .PMonth = row.Field(Of String)("PMonth"), Key .Dist = row.Field(Of String)("Dist")}
Into Group
Select dt.Clone().LoadDataRow({Key.Country, Key.PMonth, Key.Dist, CDbl(Group.Sum(Function(g) Convert.ToDouble(g.Field(Of String)("NAIJI"))))}, False)).CopyToDataTable()
Regards
MEGHA_cs
(MEGHA cs)
March 5, 2024, 4:48am
7
Thanks, still am facing the error
vrdabberu
(Varunraj Dabberu)
March 5, 2024, 4:49am
8
Hi @MEGHA_cs
If possible can you share the excel file. I will help you with required changes
Regards
vrdabberu
(Varunraj Dabberu)
March 5, 2024, 5:00am
10
Hi @MEGHA_cs
If possible atleast screenshot of the excel will help me correct the code.
Regards
MEGHA_cs
(MEGHA cs)
March 5, 2024, 5:04am
11
Can you please help to filter based on distinct values in 1st 3 columns
vrdabberu
(Varunraj Dabberu)
March 5, 2024, 5:16am
12
Hi @MEGHA_cs
Try this:
dt_Output = (From row In dt.AsEnumerable()
Group row By Key = New With {Key .Country = row.Field(Of String)("Country"), Key .PMonth = row.Field(Of String)("PMonth"), Key .Dist = row.Field(Of String)("Dist")}
Into Group
Select dt.Clone().LoadDataRow({Key.Country, Key.PMonth, Key.Dist, CDbl(Group.Sum(Function(g) Double.Parse(g.Field(Of String)("NAIJI"))))}, False)).CopyToDataTable()
Regards
MEGHA_cs
(MEGHA cs)
March 5, 2024, 5:18am
13
Thanks, but still facing issue
vrdabberu
(Varunraj Dabberu)
March 5, 2024, 5:20am
14
Hi @MEGHA_cs
I will check and let you know. Give me some time.
Regards
vrdabberu
(Varunraj Dabberu)
March 5, 2024, 10:07am
15
Hi @MEGHA_cs
Try this query:
dt_Output = (From row In dt.AsEnumerable()
Group row By Key = New With {
Key .Country = row.Field(Of String)("Country"),
Key .PMonth = row.Field(Of String)("PMonth"),
Key .Dist = row.Field(Of String)("Dist")
} Into Group
Let Area = Group.First().Field(Of String)("Area")
Let AAA = Group.First().Field(Of String)("AAA")
Let BBB = Group.First().Field(Of String)("BBB")
Let CCC = Group.First().Field(Of String)("CCC")
Let DDD = Group.First().Field(Of String)("DDD")
Let EEE = Group.First().Field(Of String)("EEE")
Select dt.Clone().Rows.Add(Key.PMonth, Area, Key.Dist, Key.Country, AAA, BBB, CCC, DDD, EEE, Group.Sum(Function(g) CDbl(g.Field(Of String)("NAIJI"))))).CopyToDataTable()
Regards
MEGHA_cs
(MEGHA cs)
March 8, 2024, 4:00am
16
Hi, Can we use index instead of column names while filtering?
vrdabberu
(Varunraj Dabberu)
March 8, 2024, 4:01am
17
Hi @MEGHA_cs
Yes we can use the column index number’s also.
Regards
system
(system)
Closed
March 11, 2024, 4:01am
18
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.