100分求Oracle中一条语句,帮忙解决下,着急,谢谢了,解决马上结贴

zzz22333 2008-09-01 04:12:54
现在有三张表Unit、UnitEntry和Type

Unit:
id name type
1 a 1
2 b 1
3 c 1
4 d 2
5 e 2
6 f 2
7 g 3
Type:
id name
1 aa
2 bb
3 cc
4 dd
UnitEntry:
unitID name
3 111
2 222
2 333
3 444
3 555
3 666
7 777
7 888
7 999

我想通过一条SQL语句,查询出结果如下:

typeid typecount
1 6
2 0
3 3
4 0

请高手帮忙写下,谢谢
...全文
150 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzz22333 2008-09-01
  • 打赏
  • 举报
回复
........


貌似我自己弄出来了,哈哈,不过看了wxg22526451回复,我又想了个方法
yinrongg 2008-09-01
  • 打赏
  • 举报
回复
UP!
lovehongyun 2008-09-01
  • 打赏
  • 举报
回复
看来我猜对了楼主的意思..呵呵.

结帖吧.
wxg22526451 2008-09-01
  • 打赏
  • 举报
回复
--> 测试数据: @Unit
declare @Unit table ([id] int,[name] varchar(1),[type] int)
insert into @Unit
select 1,'a',1 union all
select 2,'b',1 union all
select 3,'c',1 union all
select 4,'d',2 union all
select 5,'e',2 union all
select 6,'f',2 union all
select 7,'g',3
--> 测试数据: @Type
declare @Type table ([id] int,[name] varchar(2))
insert into @Type
select 1,'aa' union all
select 2,'bb' union all
select 3,'cc' union all
select 4,'dd'
--> 测试数据: @UnitEntry
declare @UnitEntry table ([unitID] int,[name] int)
insert into @UnitEntry
select 3,111 union all
select 2,222 union all
select 2,333 union all
select 3,444 union all
select 3,555 union all
select 3,666 union all
select 7,777 union all
select 7,888 union all
select 7,999

--三表关联
select * from @Type a left join @Unit b on a.id= b.[type] left join @unitEntry c on b.id=c.unitid
--查询
select a.id as typeid,count(unitid) as typecount from @Type a left join @Unit b on a.id= b.[type] left join @unitEntry c on b.id=c.unitid
group by a.id
/*结果

id name id name type unitID name
----------- ---- ----------- ---- ----------- ----------- -----------
1 aa 1 a 1 NULL NULL
1 aa 2 b 1 2 222
1 aa 2 b 1 2 333
1 aa 3 c 1 3 111
1 aa 3 c 1 3 444
1 aa 3 c 1 3 555
1 aa 3 c 1 3 666
2 bb 4 d 2 NULL NULL
2 bb 5 e 2 NULL NULL
2 bb 6 f 2 NULL NULL
3 cc 7 g 3 7 777
3 cc 7 g 3 7 888
3 cc 7 g 3 7 999
4 dd NULL NULL NULL NULL NULL

(14 行受影响)

typeid typecount
----------- -----------
1 6
2 0
3 3
4 0
警告: 聚合或其他 SET 操作消除了空值。

(4 行受影响)

*/
我姓区不姓区 2008-09-01
  • 打赏
  • 举报
回复
关键就是typecount是怎么得出来的
lovehongyun 2008-09-01
  • 打赏
  • 举报
回复
是想要这样的吗?

select type.id typeid,count(*) typecount
from type
left join unit on
type.id = unit.type
left join unitentry
on unit.id = unitentry.unitID
group by type.id
GhostAdai 2008-09-01
  • 打赏
  • 举报
回复
UnitEntry与前两张表有什么关系实在没看明白。
lovehongyun 2008-09-01
  • 打赏
  • 举报
回复
或者说typecount你想怎么算出来.你把这个说清楚了
lovehongyun 2008-09-01
  • 打赏
  • 举报
回复
typecont的意思是??
zzz22333 2008-09-01
  • 打赏
  • 举报
回复
我这个是三张表的关联
zzz22333 2008-09-01
  • 打赏
  • 举报
回复
UnitEntry表中的unitID就是Unit表中的id

查询结果中的typeid是Type表中的id
typecount是我想查到的结果

表中的关联如下:

UnitEntry表 Unit表
unitID ----------> id
name name Type表
typeid <------- id
name
lovehongyun 2008-09-01
  • 打赏
  • 举报
回复
Type left join Unit
lovehongyun 2008-09-01
  • 打赏
  • 举报
回复
select Type.id,count(*) from
Unit inner join Type
on Unit.type = Type.id
group by Type.id

不会是这么简单的问题吧?汗..

lz把问题说清楚些
lovehongyun 2008-09-01
  • 打赏
  • 举报
回复
说详细点呗!
我姓区不姓区 2008-09-01
  • 打赏
  • 举报
回复
没看出什么规律

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧