create table Table1(ctn varchar(10) , ship varchar(10))
insert into table1 values('123', 'APL')
insert into table1 values('187', 'APL')
insert into table1 values('143', 'APL')
insert into table1 values('456', 'CMA')
insert into table1 values('111', 'CMA')
insert into table1 values('45678', 'NS')
insert into table1 values('789', 'OOCL')
insert into table1 values('99999', 'OOCL')
create table Table2(ctn varchar(10))
insert into table2 values('123')
insert into table2 values('187')
insert into table2 values('456')
insert into table2 values('111')
insert into table2 values('45678')
insert into table2 values('789')
insert into table2 values('11551')
insert into table2 values('4555')
insert into table2 values('78889')
go 我想在table1里面找进场的总数和公司名,在table2里面出现了就说已进来,没出现就是没进来
然后生成下表
ship 总数 已进来 未进来
APL 3 2 1
CMA 2 2 0
NS 1 1 0
OOCL 2 1 1应该怎么写呢~!
insert into table1 values('123', 'APL')
insert into table1 values('187', 'APL')
insert into table1 values('143', 'APL')
insert into table1 values('456', 'CMA')
insert into table1 values('111', 'CMA')
insert into table1 values('45678', 'NS')
insert into table1 values('789', 'OOCL')
insert into table1 values('99999', 'OOCL')
create table Table2(ctn varchar(10))
insert into table2 values('123')
insert into table2 values('187')
insert into table2 values('456')
insert into table2 values('111')
insert into table2 values('45678')
insert into table2 values('789')
insert into table2 values('11551')
insert into table2 values('4555')
insert into table2 values('78889')
go 我想在table1里面找进场的总数和公司名,在table2里面出现了就说已进来,没出现就是没进来
然后生成下表
ship 总数 已进来 未进来
APL 3 2 1
CMA 2 2 0
NS 1 1 0
OOCL 2 1 1应该怎么写呢~!
解决方案 »
- SQL类型转换查询语句
- 存储过程使用动态条件时,出现从" 数据类型 varchar 转换为 float 时出错"的问题
- 同一个表,合计前面几列的触发器要怎么写好?
- 请教关于表中日期截取并减去现有日期的问题
- 急...这个字段该如何更新?
- 表 A 有 三个字段 ID,Name,cityID 假设 查询条件为 cityID= 2001 优先调取该城市的资源,如果没有4个则用其他城市的补齐四个
- 关于SQL server2008安装报错,求指教
- 呵呵。帮帮我呀。我的联机帮助有问题:)
- 一个关于企业管理器的疑问!
- 如何保证一个存储过程在某一时刻只有一个实例在运行?
- 请教如何将一个表指定内容提取并把数据赋值给另外一张表的指定内容?(在线等,谢谢)
- 1E条记录的表结构设计及查询优化请教
select a.ship ,count(1) as 总数,
count(case when b.ctn is not null then 1 end) as 已进来,
count(case when b.ctn is null then 1 end) as 未进来
from table1 a left join table2 b
on a.ctn=b.ctn
group by a.shipship 总数 已进来 未进来
---------- ----------- ----------- -----------
APL 3 2 1
CMA 2 2 0
NS 1 1 0
OOCL 2 1 1
警告: 聚合或其他 SET 操作消除了空值。(4 行受影响)
select ship,
总数=count(1),
已进来=count(b.ctn),
未进来=sum(case when b.ctn is null then 1 else 0 end)
from table1 a
left join table2 b on a.ctn=b.ctn
group by ship
总数=count(1),
已进来=count(b.ctn),
未进来=count(1)-count(b.ctn)
from table1 a
left join table2 b on a.ctn=b.ctn
group by ship
总数 =count(a.ctn),
已进来=count(b.ctn),
未进来=count(a.ctn)-count(b.ctn)
from table1 a
left join table2 b on a.ctn=b.ctn
group by ship