I have an SQL many to many relationship with “Texts” and “Subjects”. How do I select texts that are linked to Subject A AND Subject B? Example from Stack Overflow: mysql - SQL: many to many relationships select where multiple criteria - Stack Overflow
Here’s simplified schemas
Texts:
cube(`Texts`, {
sql: `SELECT * FROM "Texts"`,
refreshKey: {
sql: `SELECT COUNT(*) FROM public."Texts"`,
},
joins: {
TextSubjectMapping: {
relationship: `hasMany`,
sql: `${TextSubjectMapping}."TextId" = ${Texts}."id"`,
}
},
measures: {
count: {
type: `count`,
title: 'legends:Texts.count',
},
},
dimensions: {
id: {
sql: `id`,
type: `number`,
primaryKey: true,
},
headline: {
sql: `${CUBE}."headline"`,
type: `string`,
title: 'legends:Texts.headline',
},
},
});
Subjects:
cube(`Subjects`, {
sql: `SELECT * FROM public."Subjects"`,
refreshKey: {
sql: `SELECT MAX("updatedAt") FROM public."Subjects"`,
},
joins: {
TextSubjectMapping: {
relationship: `hasMany`,
sql: `${Subjects}.id = ${TextSubjectMapping}."SubjectId"`,
},
},
measures: {
count: {
type: `count`,
drillMembers: [name],
title: 'legends:Subjects.count',
},
},
dimensions: {
id: {
sql: `id`,
type: `number`,
primaryKey: true,
shown: false,
},
name: {
sql: `name`,
type: `string`,
title: 'legends:Subjects.name',
},
},
});
TextsSubjectsMapping:
cube(`TextSubjectMapping`, {
sql: `SELECT * FROM "TextSubjectMapping"`,
refreshKey: {
sql: `SELECT MAX("updatedAt") FROM public."TextSubjectMapping"`,
},
joins: {
Subjects: {
relationship: `belongsTo`,
sql: `${TextSubjectMapping}."SubjectId" = ${Subjects}."id"`,
},
Texts: {
relationship: `belongsTo`,
sql: `${TextSubjectMapping}."TextId" = ${Texts}."id"`,
},
},
dimensions: {
textid: {
sql: `${TextSubjectMapping}."TextId"`,
type: `number`,
primaryKey: true,
shown: false,
},
subjectid: {
sql: `${TextSubjectMapping}."SubjectId"`,
type: `number`,
primaryKey: true,
shown: false,
},
},
});
This kind of sql is what I’m looking for:
SELECT
"texts"."id", "texts"."headline"
FROM
"Texts" AS "texts"
INNER JOIN
"TextSubjectMapping" AS "txtsubmap" ON "txtsubmap"."TextId" = "texts"."id"
INNER JOIN
"Subjects" AS "subjects" ON "txtsubmap"."SubjectId" = "subjects"."id"
WHERE
"subjects".name IN ('Subject A', 'Subject B')
GROUP BY
1,
2
HAVING
COUNT(DISTINCT "txtsubmap"."SubjectId") = 2
Where COUNT(DISTINCT "txtsubmap"."SubjectId") = 2
should be dynamic based on the amount of values in "subjects".name IN ('Subject A', 'Subject B')
How this can be achieved in cube.js client with filters?