次のクエリをMicrosoftSQLServerに書き込む方がよいのではないかと思います。
、、の3つのテーブルsurveysがsurvey_presetsありsurvey_scenesます。次の列があります。
CREATE TABLE [dbo].[surveys](
[id] [int] IDENTITY(1,1) NOT NULL,
[caption] [nvarchar](255) NOT NULL,
[creation_time] [datetime] NOT NULL,
)
CREATE TABLE [dbo].[survey_presets](
[id] [int] IDENTITY(1,1) NOT NULL,
[survey_id] [int] NOT NULL,
[preset_id] [int] NOT NULL,
)
CREATE TABLE [dbo].[survey_scenes](
[id] [int] IDENTITY(1,1) NOT NULL,
[survey_id] [int] NOT NULL,
[scene_id] [int] NOT NULL,
)
両方ともsurvey_presets、列のsurvey_scenes外部キーがオンsurveysになっていsurvey_idます。
次に、対応するプリセットとシーンの数をそれぞれに含むすべての調査を選択します。これが私が欲しいものの「疑似クエリ」です:
SELECT
surveys.*,
COUNT(survey_presets, where survey_presets.survey_id = surveys.id),
COUNT(survey_scenes, where survey_scenes.survey_id = surveys.id)
FROM surveys
ORDER BY suverys.creation_time
SELECT DISTINCT、、などJOINをいじることはできますが、GROUP BYT-SQLは初めてであり、クエリがどのような意味でも最適になるとは思えません。