LINQ grouping with count issue - asp.net

I'm having trouble trying to group a LINQ query with a count correctly. As an example I'm trying to query a datatable with the following data:
PC EC CC
ABC XXX US
ABC XXX CA
ABC XXX UK
DEF YYY US
DEF YYY CA
DEF YYY UK
DEF ZZZ US
DEF ZZZ CA
DEF ZZZ UK
HIJ AAA US
HIJ AAA CA
HIJ AAA UK
I want to use the count function to show the number of different values of EC for each value of PC, removing the values in CC. In other words the result for the above dataset should be:
PC Count EC
ABC 1
DEF 2
HIJ 1
I've tried several different ways of getting to this result but I keep hitting a wall. My last attempt looked like this:
Dim test = From r In ( _
From s In Base _
Group By s.Base_PC, _
s.Base_EC _
Into g = Group) _
Group r By key = New With {r.Base_PC} _
Into Group _
Select key.Base_PC, Group.Count
Any ideas where I'm going wrong? Thanks!

Here the method syntax since it's too much for query syntax:
Dim counts = base.AsEnumerable().
GroupBy(Function(r) r.Field(Of String)("PC")).
Select(Function(g) New With {
.PC = g.Key,
.CountEC = g.Select(Function(r) r.Field(Of String)("EC")).
Distinct().
Count()
})
For Each x In counts
Console.WriteLine("PC={0} Count of distinct EC's={1}", x.PC, x.CountEC)
Next
First you need to group by the "PC" column, then you select an anonymous type with the group's key and the count of distinct values of the "EC" column.

Related

Updating Fields sequentially based on conditions

I have this current example data set
NEW_ID Name OLD_ID New_Name
123 Hello XYZ
124 How XYZ
125 Are XYZ
126 My ABC
127 Name ABC
128 Is ABC
129 Alex ABC
My objective is to amend the Name field to a new naming convention to be stored in New_Name- ie Hello_Part_1, How_Part_2, Are_Part_3 where all these records share an OLD_ID - in this case XYZ. Similarly, with My_Part_1, Name_Part_2, Is_Part_3, Alex_Part_4 etc with IDs that equal ABC.
I'm using SQL Lite with an Import of .CSV File.
The naming convention is as follows - NAME_PART_X where X increments on the number of records within that 'Group' of OLD_IDs.
SQL does not work sequentially; you have to express the operation independently for each row.
The number you want is the count of rows with the same old ID that also have a new ID that is the same or smaller as the new ID of the current row.
This can be computed with a correlated subquery:
UPDATE MyTable
SET New_Name = Name || '_Part_' ||
(SELECT COUNT(*)
FROM MyTable AS T2
WHERE T2.OLD_ID = MyTable.OLD_ID
AND T2.NEW_ID <= MyTable.NEW_ID);

Sqlite how to select case for multiple values in a column

So, I have this table:
id|otherid|key|value
--------------------
1 1 ak av
2 1 bk bv
3 2 ak av
3 2 ak av2
The things to note is that other ids are repeating and they can have same keys with values multiple times. The thing I want to retrieve would be the value for the key, or, if there are multiple values for same key some string.
So, I'd like to receive for otherids
otherid|key|value
-----------------
1 ak av
1 bk bv
2 ak SEQUENCE
Where 'SEQUENCE' string allows me to know that there are multiple values for the single key for otherid. What query would accomplish this?
To get one output row for multiple input rows, use grouping.
The count of rows in the group is available with COUNT(*); you can handle the cases with a CASE expression:
SELECT otherid,
key,
CASE COUNT(*)
WHEN 1 THEN MIN(value)
ELSE 'SEQUENCE'
END AS value
FROM MyTable
GROUP BY otherid,
key;
SELECT DISTINCT
otherid,key,
(SELECT
CASE
WHEN COUNT(value)=1 THEN value
WHEN COUNT(value)=0 THEN '*nil*'
ELSE '*sequence*'
END)
FROM datasingle
WHERE otherid=myid GROUP BY key;

Linq Let Object not showing up in resuls - Creating a crosstab query

I'm trying to convert a very simple linq tabular query into a crosstab.
dim q results
Zone Cube QTY
100 LARGE 19
100 MEDIUM 4
100 SMALL 2
100 TINY 1
200 TINY 9
200 LARGE 1
200 TINY 2
linq to crosstab
Dim b = From f In (From x In q Group x By x.Zone Into Group) _
Let MEDIUM As Object = (From r In f.Group Where _
r.Cube = "MEDIUM" Select r.QTY).Sum _
Let SMALL As Object = (From r In f.Group Where _
r.Cube = "SMALL" Select r.QTY).Sum _
Select New With {f.Zone ,SMALL,MEDIUM}
This should give me a crosstab table however I'm only getting the group column and the object columns are not showing up.
I tried different things but I'm stuck. any ideas?

display row data as column in sqlite

I have a DB table with below structure in SQLITE
Name Count Type
Roy 3 CA
Roy 2 BT
John 2 CA
John 1 BT
Need a query that can display the above data like below(sqlite)
Name count CA BT
Roy 5 3 2
John 3 2 1
thanks
There is no pivoting in SQLite, but you can use a lot of subquerys:
SELECT DISTINCT name,
(SELECT sum([count]) FROM test tst WHERE tst.name = test.name) as count,
(SELECT sum([count]) FROM test tst WHERE tst.name = test.name AND type='CA') as CA,
(SELECT sum([count]) FROM test tst WHERE tst.name = test.name AND type='BT') as BT
FROM test
assuming tablename "test"

Parent Child table record - Building SQL query

Here is my table and data of these tables
Table name: Code
CID Code
1 abc
2 def
3 xyz
Table Name : Details
ID Name CID
1 a 1
2 b 2
Resultant Table:
ID Code Name
1 abc a
2 abc Null
3 def b
4 def Null
5 xyz Null
6 xyz Null
I nned to get all record from the code table and against each code I have to get all the rows from the details table, if some code have value their need value and if not then Null
Thanks
Sounds like you're looking for the cartesian product:
SELECT
c.CID * d.ID AS ID,
c.Code,
CASE
WHEN c.CID = d.CID THEN d.Name
ELSE NULL
END AS Name
FROM Code c
CROSS JOIN Details d
Although cartesian products are quite slow for larger tables... so be sure that this is what you really want.

Resources