環境
・SQLServer2008
やりたいこと
異なるテーブルそれぞれの条件を満たす行を一発のクエリで取得する事は可能でしょうか。
テーブル構造が結構複雑で文章だと説明が難しいため、詳細は下記を御覧ください。
商品マスタ関連
・商品には種別が存在しており、1商品に付き複数の種別を設定できる
・「商品種別紐付けマスタ」を元に「商品」と「種別」の紐付けを行っている
オプション関連
・商品種別は別に「オプション」が存在している
・オプションは商品マスタに紐づくのではなく、注文トランに紐づく
・1注文に付き複数オプションが選択できる
割引マスタ関連
・「商品種別」と「オプション」の組み合わせによって割引マスタが決定される
・商品種別のみで判断する場合もあれば、オプションのみで判断する場合もある
・条件に一致する割引マスタが取得されればOK(=複数行が取得される場合もあるということ)
望まれる結果
CREATE文
SQL
1USE [test] 2GO 3SET ANSI_NULLS ON 4GO 5SET QUOTED_IDENTIFIER ON 6GO 7CREATE TABLE [dbo].[m_option]( 8 [option_id] [int] NOT NULL, 9 [option_name] [nchar](50) NULL, 10 CONSTRAINT [PK_m_option] PRIMARY KEY CLUSTERED 11( 12 [option_id] ASC 13)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 14) ON [PRIMARY] 15GO 16SET ANSI_NULLS ON 17GO 18SET QUOTED_IDENTIFIER ON 19GO 20CREATE TABLE [dbo].[m_shohin]( 21 [shohin_id] [int] NOT NULL, 22 [shohin_name] [nchar](50) NULL, 23 CONSTRAINT [PK_m_shohin] PRIMARY KEY CLUSTERED 24( 25 [shohin_id] ASC 26)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 27) ON [PRIMARY] 28GO 29SET ANSI_NULLS ON 30GO 31SET QUOTED_IDENTIFIER ON 32GO 33CREATE TABLE [dbo].[m_shohin_shubetsu]( 34 [shohin_id] [int] NOT NULL, 35 [shubetsu_id] [int] NOT NULL, 36 CONSTRAINT [PK_m_shohin_syubetu] PRIMARY KEY CLUSTERED 37( 38 [shohin_id] ASC, 39 [shubetsu_id] ASC 40)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 41) ON [PRIMARY] 42GO 43SET ANSI_NULLS ON 44GO 45SET QUOTED_IDENTIFIER ON 46GO 47CREATE TABLE [dbo].[m_shubetsu]( 48 [shubetsu_id] [int] NOT NULL, 49 [shubetsu_name] [nchar](50) NULL, 50 CONSTRAINT [PK_m_syubetu] PRIMARY KEY CLUSTERED 51( 52 [shubetsu_id] ASC 53)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 54) ON [PRIMARY] 55GO 56SET ANSI_NULLS ON 57GO 58SET QUOTED_IDENTIFIER ON 59GO 60CREATE TABLE [dbo].[m_waribiki]( 61 [waribiki_id] [int] NOT NULL, 62 [waribiki_name] [nchar](50) NULL, 63 [waribiki_ritsu] [int] NULL, 64 CONSTRAINT [PK_m_waribiki] PRIMARY KEY CLUSTERED 65( 66 [waribiki_id] ASC 67)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 68) ON [PRIMARY] 69GO 70SET ANSI_NULLS ON 71GO 72SET QUOTED_IDENTIFIER ON 73GO 74CREATE TABLE [dbo].[m_waribiki_option]( 75 [waribiki_id] [int] NOT NULL, 76 [option_id] [int] NOT NULL, 77 CONSTRAINT [PK_m_warbiki_option] PRIMARY KEY CLUSTERED 78( 79 [waribiki_id] ASC, 80 [option_id] ASC 81)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 82) ON [PRIMARY] 83GO 84SET ANSI_NULLS ON 85GO 86SET QUOTED_IDENTIFIER ON 87GO 88CREATE TABLE [dbo].[m_waribiki_shubetsu]( 89 [waribiki_id] [int] NOT NULL, 90 [shubetsu_id] [int] NOT NULL, 91 CONSTRAINT [PK_m_waribiki_syubetu] PRIMARY KEY CLUSTERED 92( 93 [waribiki_id] ASC, 94 [shubetsu_id] ASC 95)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 96) ON [PRIMARY] 97GO 98SET ANSI_NULLS ON 99GO 100SET QUOTED_IDENTIFIER ON 101GO 102CREATE TABLE [dbo].[t_uriage]( 103 [denp_no] [int] NOT NULL, 104 [det_no] [int] NOT NULL, 105 [shohin_id] [int] NULL, 106 [su] [int] NULL, 107 CONSTRAINT [PK_t_uriage] PRIMARY KEY CLUSTERED 108( 109 [denp_no] ASC, 110 [det_no] ASC 111)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 112) ON [PRIMARY] 113GO 114SET ANSI_NULLS ON 115GO 116SET QUOTED_IDENTIFIER ON 117GO 118CREATE TABLE [dbo].[t_uriage_option]( 119 [denp_no] [int] NOT NULL, 120 [det_no] [int] NOT NULL, 121 [option_id] [int] NOT NULL, 122 CONSTRAINT [PK_t_uriage_option] PRIMARY KEY CLUSTERED 123( 124 [denp_no] ASC, 125 [det_no] ASC, 126 [option_id] ASC 127)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 128) ON [PRIMARY] 129GO 130INSERT [dbo].[m_option] ([option_id], [option_name]) VALUES (1, N'3営業日以降出荷 ') 131GO 132INSERT [dbo].[m_option] ([option_id], [option_name]) VALUES (2, N'包装紙無し ') 133GO 134INSERT [dbo].[m_shohin] ([shohin_id], [shohin_name]) VALUES (1, N'明治チョコレート ') 135GO 136INSERT [dbo].[m_shohin] ([shohin_id], [shohin_name]) VALUES (2, N'グリコチョコレート ') 137GO 138INSERT [dbo].[m_shohin] ([shohin_id], [shohin_name]) VALUES (3, N'グリコクッキー ') 139GO 140INSERT [dbo].[m_shohin] ([shohin_id], [shohin_name]) VALUES (4, N'明治チョコ&クッキーセット ') 141GO 142INSERT [dbo].[m_shohin_shubetsu] ([shohin_id], [shubetsu_id]) VALUES (1, 1) 143GO 144INSERT [dbo].[m_shohin_shubetsu] ([shohin_id], [shubetsu_id]) VALUES (1, 3) 145GO 146INSERT [dbo].[m_shohin_shubetsu] ([shohin_id], [shubetsu_id]) VALUES (2, 2) 147GO 148INSERT [dbo].[m_shohin_shubetsu] ([shohin_id], [shubetsu_id]) VALUES (2, 3) 149GO 150INSERT [dbo].[m_shohin_shubetsu] ([shohin_id], [shubetsu_id]) VALUES (3, 2) 151GO 152INSERT [dbo].[m_shohin_shubetsu] ([shohin_id], [shubetsu_id]) VALUES (3, 4) 153GO 154INSERT [dbo].[m_shohin_shubetsu] ([shohin_id], [shubetsu_id]) VALUES (4, 1) 155GO 156INSERT [dbo].[m_shohin_shubetsu] ([shohin_id], [shubetsu_id]) VALUES (4, 3) 157GO 158INSERT [dbo].[m_shohin_shubetsu] ([shohin_id], [shubetsu_id]) VALUES (4, 4) 159GO 160INSERT [dbo].[m_shubetsu] ([shubetsu_id], [shubetsu_name]) VALUES (1, N'明治 ') 161GO 162INSERT [dbo].[m_shubetsu] ([shubetsu_id], [shubetsu_name]) VALUES (2, N'グリコ ') 163GO 164INSERT [dbo].[m_shubetsu] ([shubetsu_id], [shubetsu_name]) VALUES (3, N'チョコレート ') 165GO 166INSERT [dbo].[m_shubetsu] ([shubetsu_id], [shubetsu_name]) VALUES (4, N'クッキー ') 167GO 168INSERT [dbo].[m_waribiki] ([waribiki_id], [waribiki_name], [waribiki_ritsu]) VALUES (1, N'チョコレート割 ', 10) 169GO 170INSERT [dbo].[m_waribiki] ([waribiki_id], [waribiki_name], [waribiki_ritsu]) VALUES (2, N'包装紙無し割引 ', 5) 171GO 172INSERT [dbo].[m_waribiki] ([waribiki_id], [waribiki_name], [waribiki_ritsu]) VALUES (3, N'3営業日以降出荷 割引(明治チョコ限定) ', 3) 173GO 174INSERT [dbo].[m_waribiki] ([waribiki_id], [waribiki_name], [waribiki_ritsu]) VALUES (4, N'3営業日以降出荷 割引+包装紙無し割引(チョコレート限定) ', 5) 175GO 176INSERT [dbo].[m_waribiki] ([waribiki_id], [waribiki_name], [waribiki_ritsu]) VALUES (5, N'3営業日以降出荷 割引+包装紙無し割引(グリコクッキー限定) ', 5) 177GO 178INSERT [dbo].[m_waribiki_option] ([waribiki_id], [option_id]) VALUES (2, 2) 179GO 180INSERT [dbo].[m_waribiki_option] ([waribiki_id], [option_id]) VALUES (3, 1) 181GO 182INSERT [dbo].[m_waribiki_option] ([waribiki_id], [option_id]) VALUES (4, 1) 183GO 184INSERT [dbo].[m_waribiki_option] ([waribiki_id], [option_id]) VALUES (4, 2) 185GO 186INSERT [dbo].[m_waribiki_option] ([waribiki_id], [option_id]) VALUES (5, 1) 187GO 188INSERT [dbo].[m_waribiki_option] ([waribiki_id], [option_id]) VALUES (5, 2) 189GO 190INSERT [dbo].[m_waribiki_shubetsu] ([waribiki_id], [shubetsu_id]) VALUES (1, 3) 191GO 192INSERT [dbo].[m_waribiki_shubetsu] ([waribiki_id], [shubetsu_id]) VALUES (3, 1) 193GO 194INSERT [dbo].[m_waribiki_shubetsu] ([waribiki_id], [shubetsu_id]) VALUES (3, 3) 195GO 196INSERT [dbo].[m_waribiki_shubetsu] ([waribiki_id], [shubetsu_id]) VALUES (4, 3) 197GO 198INSERT [dbo].[m_waribiki_shubetsu] ([waribiki_id], [shubetsu_id]) VALUES (5, 2) 199GO 200INSERT [dbo].[m_waribiki_shubetsu] ([waribiki_id], [shubetsu_id]) VALUES (5, 3) 201GO 202INSERT [dbo].[t_uriage] ([denp_no], [det_no], [shohin_id], [su]) VALUES (1, 1, 1, 5) 203GO 204INSERT [dbo].[t_uriage] ([denp_no], [det_no], [shohin_id], [su]) VALUES (2, 1, 3, 5) 205GO 206INSERT [dbo].[t_uriage] ([denp_no], [det_no], [shohin_id], [su]) VALUES (3, 1, 1, 5) 207GO 208INSERT [dbo].[t_uriage] ([denp_no], [det_no], [shohin_id], [su]) VALUES (4, 1, 3, 5) 209GO 210INSERT [dbo].[t_uriage] ([denp_no], [det_no], [shohin_id], [su]) VALUES (5, 1, 3, 5) 211GO 212INSERT [dbo].[t_uriage] ([denp_no], [det_no], [shohin_id], [su]) VALUES (5, 2, 1, 10) 213GO 214INSERT [dbo].[t_uriage_option] ([denp_no], [det_no], [option_id]) VALUES (2, 1, 2) 215GO 216INSERT [dbo].[t_uriage_option] ([denp_no], [det_no], [option_id]) VALUES (3, 1, 2) 217GO 218INSERT [dbo].[t_uriage_option] ([denp_no], [det_no], [option_id]) VALUES (4, 1, 1) 219GO 220INSERT [dbo].[t_uriage_option] ([denp_no], [det_no], [option_id]) VALUES (4, 1, 2) 221GO 222INSERT [dbo].[t_uriage_option] ([denp_no], [det_no], [option_id]) VALUES (5, 1, 1) 223GO 224INSERT [dbo].[t_uriage_option] ([denp_no], [det_no], [option_id]) VALUES (5, 1, 2) 225GO 226INSERT [dbo].[t_uriage_option] ([denp_no], [det_no], [option_id]) VALUES (5, 2, 1) 227GO 228INSERT [dbo].[t_uriage_option] ([denp_no], [det_no], [option_id]) VALUES (5, 2, 2) 229GO 230