Query to select a list of values from a table based on this multiple criteria
-
26-10-2019 - |
Question
I have a table with 2 columns, Ex_Id and Term_Id, both int type. My table will have many Term Ids for one Exercise Id.
Table would look like this:
Ex_Id Term_Id
1 2
1 3
1 4
1 5
2 2
3 2
3 4
etc. Getting a list of Ex_Id is the primary requirement. My function would be like this.
List<int> Get_ExId_List(List<int> lst_TermId)
{
// return a list of Ex_Id <int>
}
That is, I'll be passing a list of Term Ids and I need to get a list of Exercise Ids back matching some criteria. The criteria to select can be better explained with this pseudo-code: SELECT such Ex_Ids FROM table Exercise_Term WHERE Ex_Id has all the corresponding Term_Ids in the lst_TermId
For eg, from the sample table I provided above,
List<int> Get_ExId_List([2])
{
// return [1,2,3]
}
List<int> Get_ExId_List([2,4])
{
// return [1,3]
}
List<int> Get_ExId_List([2,3,4])
{
// return [1]
}
Query part is my confusion. What would be the query in this condition like? Rest I can manage. Hope question is clear. Thanks..
Solution
SELECT Ex_ID
FROM TableName
WHERE Term_ID IN (?, ?, ?) --- (2, 3, 4)
GROUP BY Ex_ID
HAVING COUNT(DISTINCT Term_ID) = 3 --- number of terms in the above list
If the combination (Ex_ID, Term_ID)
is unique in the table, you can replace COUNT(DISTINCT Term_ID)
with COUNT(*)
This is a relational division problem. The "standard" solution would be using two negatives (NOT EXISTS):
SELECT DISTINCT Ex_ID
FROM TableName e
WHERE NOT EXISTS
( SELECT *
FROM TableName t
WHERE t.Term_ID IN (?, ?, ?) --- the list of terms
AND NOT EXISTS
( SELECT *
FROM TableName a
WHERE a.Term_ID = t.Term_ID
AND a.Ex_ID = e.Ex_ID
)
)
or better in your case:
SELECT DISTINCT Ex_ID
FROM TableName e
WHERE NOT EXISTS
( SELECT *
FROM
( SELECT ? AS Term_ID
UNION
SELECT ?
UNION
SELECT ?
) AS t
WHERE NOT EXISTS
( SELECT *
FROM TableName a
WHERE a.Term_ID = t.Term_ID
AND a.Ex_ID = e.Ex_ID
)
)
OTHER TIPS
You can use LINQ. Get the whole table into an IEnumerable of some sort and then use LINQ. Here is an example:
static IEnumerable<int> Get_ExId_List(ICollection<int> lst_TermId)
{
//this is just for the example - get the real data instead
var data = new[] {
new { Ex_Id = 1, Term_Id = 2},
new { Ex_Id = 1, Term_Id = 3},
new { Ex_Id = 1, Term_Id = 4},
new { Ex_Id = 1, Term_Id = 5},
new { Ex_Id = 2, Term_Id = 2},
new { Ex_Id = 3, Term_Id = 2},
new { Ex_Id = 3, Term_Id = 4},
};
return data
.Where(row => lst_TermId.Contains(row.Term_Id))
.GroupBy(row => row.Ex_Id)
.Where(group => group.Count() == lst_TermId.Count())
.Select(group => group.Key);
}
static void Main(string[] args)
{
HashSet<int> lst_TermId = new HashSet<int>();
lst_TermId.Add(2);
Console.WriteLine();
var result = Get_ExId_List(lst_TermId);
foreach (var exid in result)
Console.WriteLine(exid);
lst_TermId.Add(4);
Console.WriteLine();
result = Get_ExId_List(lst_TermId);
foreach (var exid in result)
Console.WriteLine(exid);
lst_TermId.Add(3);
Console.WriteLine();
result = Get_ExId_List(lst_TermId);
foreach (var exid in result)
Console.WriteLine(exid);
}
Note that you'll get better performance if your lst_TermId is a HashSet<int>
, because the contains method will be O(1)
instead of O(n)
.