如何在SQL中使用多对多关系

时间:2017-03-02 12:23:15

标签: sql-server sql-server-2008 join qsqlquery

以下sql代码段,用于使用多对多关系从sql server检索数据。 我未能实现分组并加入此处 请建议任何好的解决方案,以获得我在下面提到的结果。

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

+--------+---------+---------+-----------+
| 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 |
+--------+---------+---------+-----------+

表格:item_con

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

表格:合同

+--------+---------+
| 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 |
+-------+------+----------+--------------+

2 个答案:

答案 0 :(得分:2)

试试这个:

Microsoft.CodeDom.Providers.DotNetCompilerPlatform

结果:

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

答案 1 :(得分: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