2017-03-02 17 views
0

多対多の関係を使用してSQL Serverからデータを取得するための次のsqlコードスニペット。 私はここでグループバイヤを実装することに失敗しました 私は以下に述べた結果を得るための良い解決策を提案してください。SQLで多対多の関係を使用する方法

select COUNT(inc.inc_id) ,Contract.Contract_n,Cust_id, 
SUM(inc.service_time)/60 as Service_Time 
from inc 
inner join item_con 
inner join Contract on item_con.Contract_id = Contract.Contract_id 
on inc.item_id = item.item_id 
ORDER BY Cust_id,Contract.Contract_n 

表:株式会社

+--------+---------+---------+-----------+ 
| inc_id | cust_id | Item_id | Serv_Time | 
+--------+---------+---------+-----------+ 
|  1 |  100 |  55 |  60 | 
|  2 |  100 |  33 |  120 | 
|  3 |  200 |  44 |  180 | 
|  4 |  300 |  77 |  40 | 
|  5 |  200 |  66 |  300 | 
|  6 |  100 |  55 |  120 | 
|  7 |  200 |  44 |  20 | 
+--------+---------+---------+-----------+ 

表:

+--------+---------+ 
| con_id | item_id | 
+--------+---------+ 
| 500 |  33 | 
| 600 |  44 | 
| 700 |  55 | 
| 800 |  66 | 
| 900 |  77 | 
| 300 |  55 | 
+--------+---------+ 

表item_con:契約

+--------+---------+ 
| con_id | item_id | 
+--------+---------+ 
| 300 | ABC  | 
| 500 | EFG  | 
| 600 | HIJ  | 
| 800 | KLM  | 
| 700 | NOP  | 
| 900 | QRS  | 
+--------+---------+ 

結果:

+-------+------+----------+--------------+ 
| Calls | Cust | Contract | Total_S_Time | 
+-------+------+----------+--------------+ 
|  2 | 100 | NOP  |   180 | 
|  1 | 100 | EFG  |   120 | 
|  2 | 200 | HIJ  |   200 | 
|  1 | 200 | KLM  |   40 | 
|  1 | 300 | QRS  |   300 | 
+-------+------+----------+--------------+ 

答えて

0
select 
    Calls = count(*) 
    , Cust = inc.Cust_id 
    , Contract = c.item_id 
    , Serv_Time = sum(Serv_Time) 
from inc 
    inner join item_con as i 
    on inc.Item_id = i.item_id 
    inner join contract as c 
    on i.con_id = c.con_id 
group by inc.Cust_id, c.item_id 
order by inc.Cust_Id, c.Item_Id 

リターン:

+-------+------+----------+-----------+ 
| Calls | Cust | Contract | Serv_Time | 
+-------+------+----------+-----------+ 
|  2 | 100 | ABC  |  180 | 
|  1 | 100 | EFG  |  120 | 
|  2 | 100 | NOP  |  180 | 
|  2 | 200 | HIJ  |  200 | 
|  1 | 200 | KLM  |  300 | 
|  1 | 300 | QRS  |  40 | 
+-------+------+----------+-----------+ 

テストのセットアップ:http://rextester.com/OKP21617

create table inc (inc_id int, cust_id int, item_id int, serv_time int); 
insert into inc values 
(1,100,55,60) 
,(2,100,33,120) 
,(3,200,44,180) 
,(4,300,77,40) 
,(5,200,66,300) 
,(6,100,55,120) 
,(7,200,44,20); 
create table item_con (con_id int, item_id int); 
insert into item_con values 
(500,33) 
,(600,44) 
,(700,55) 
,(800,66) 
,(900,77) 
,(300,55); 
create table contract (con_id int, item_id char(3)); 
insert into contract values 
(300,'ABC') 
,(500,'EFG') 
,(600,'HIJ') 
,(800,'KLM') 
,(700,'NOP') 
,(900,'QRS'); 
select 
    Calls = count(*) 
    , Cust = inc.Cust_id 
    , Contract = c.item_id 
    , Serv_Time = sum(Serv_Time) 
from inc 
    inner join item_con as i 
    on inc.Item_id = i.item_id 
    inner join contract as c 
    on i.con_id = c.con_id 
group by inc.Cust_id, c.item_id 
order by inc.Cust_Id, c.Item_Id 
2

これを試してみてください。

DECLARE @inc TABLE (inc_id int, cust_id int, item_id int, serv_time int) 
DECLARE @item_con TABLE (item_id int, con_id int) 
DECLARE @con TABLE (item_id varchar(3), con_id int) 

insert into @inc (inc_id, cust_id, item_id, serv_time) values (1, 100, 55, 60) 
insert into @inc (inc_id, cust_id, item_id, serv_time) values (2, 100, 33, 120) 
insert into @inc (inc_id, cust_id, item_id, serv_time) values (3, 200, 44, 180) 
insert into @inc (inc_id, cust_id, item_id, serv_time) values (4, 300, 77, 40) 
insert into @inc (inc_id, cust_id, item_id, serv_time) values (5, 200, 66, 300) 
insert into @inc (inc_id, cust_id, item_id, serv_time) values (6, 100, 55, 120) 
insert into @inc (inc_id, cust_id, item_id, serv_time) values (7, 200, 44, 20) 

insert into @item_con (con_id, item_id) values (500, 33) 
insert into @item_con (con_id, item_id) values (600, 44) 
insert into @item_con (con_id, item_id) values (700, 55) 
insert into @item_con (con_id, item_id) values (800, 66) 
insert into @item_con (con_id, item_id) values (900, 77) 
insert into @item_con (con_id, item_id) values (300, 55) 

insert into @con (con_id, item_id) values (300, 'ABC') 
insert into @con (con_id, item_id) values (500, 'EFG') 
insert into @con (con_id, item_id) values (600, 'HIJ') 
insert into @con (con_id, item_id) values (800, 'KLM') 
insert into @con (con_id, item_id) values (700, 'NOP') 
insert into @con (con_id, item_id) values (900, 'QRS') 


select count(1) as Calls, i.cust_id as Cust, c.item_id as [Contract], sum(i.serv_time) as TotalTime 
from @inc i 
inner join @item_con ic on i.item_id = ic.item_id 
inner join @con c on ic.con_id = c.con_id 
group by i.cust_id, c.item_id 

結果:

2 100 ABC 180 
1 100 EFG 120 
2 200 HIJ 200 
1 200 KLM 300 
2 100 NOP 180 
1 300 QRS 40 
+0

値は、あなたの結果に、私は結果をoutputedまし –

+0

を繰り返しています。繰り返しは何ですか? –