次のクエリを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 BY
T-SQLは初めてであり、クエリがどのような意味でも最適になるとは思えません。