提问:涉及到两个表的查询的问题

bluesky1980 2003-12-17 02:28:46
我写的语句如下:(哪里有错误,应如何改正,谢谢)
select 公司名称=table1.company
,总订单数=count(table1.id)
,成功订单数=sum(case table1.result when 'y' then 1 else 0 end)
,成功订票数=sum(case table1.result when 'y' and table2.remove='y'then 1 else 0 end)from 表 where table1.id=table2.order_id (主外键相关联)
group by table1.company

我的表结构为
table1: id company result
1 A y
2 A n
......................
table2 id order_id remove
1 1 y
2 1 y
3 1 n
4 2 n
5 2 n
.......................
我想得到的结果为
公司名称 总订单数 成功订单数 成功票数
A 2 1 2
...全文
53 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluesky1980 2003-12-17
  • 打赏
  • 举报
回复
谢谢邹建
同时也感谢victorycyz(中海,学SQL Server的菜鸟) Rotaxe(程序员)
zjcxc 元老 2003-12-17
  • 打赏
  • 举报
回复
--下面是例子:

--测试数据
create table table1(id int,company varchar(1),result varchar(1))
insert into table1
select 1,'A','y'
union all select 2,'A','n'

create table table2(id int,order_id int,remove varchar(1))
insert into table2
select 1,1,'y'
union all select 2,1,'y'
union all select 3,1,'n'
union all select 4,2,'n'
union all select 5,2,'n'
go

--查询
select a.*,b.成功票数 from(
select 公司名称=company,总订单数=count(*)
,成功订单数=sum(case result when 'y' then 1 else 0 end)
from table1 group by company
) a join(
select a.company,成功票数=sum(case when a.result='y' and b.remove='y'then 1 else 0 end)
from table1 a join table2 b on a.id=b.order_id group by a.company
) b on a.公司名称=b.company
go

--删除测试
drop table table1,table2

/*--测试结果

公司名称 总订单数 成功订单数 成功票数
---- ----------- ----------- -----------
A 2 1 2

(所影响的行数为 1 行)
--*/
zjcxc 元老 2003-12-17
  • 打赏
  • 举报
回复
--上面理解错楼主的意思,应该用:
select a.*,b.成功票数 from(
select 公司名称=company,总订单数=count(*)
,成功订单数=sum(case result when 'y' then 1 else 0 end)
from table1 group by company
) a join(
select a.company,成功票数=sum(case when a.result='y' and b.remove='y'then 1 else 0 end)
from table1 a join table2 b on a.id=b.order_id group by a.company
) b on a.公司名称=b.company
bluesky1980 2003-12-17
  • 打赏
  • 举报
回复
victorycyz(中海,学SQL Server的菜鸟) :昨天是帮过我了,但现在有新问题,所以又来请教大家了:)
zjcxc(邹建)
您的测试结果有问题啊,应该总订单数为2,成功订单数为1,成功票数为2(统计票数是COUNT(table2.id))
zjcxc 元老 2003-12-17
  • 打赏
  • 举报
回复
--下面是例子:

--测试数据
create table table1(id int,company varchar(1),result varchar(1))
insert into table1
select 1,'A','y'
union all select 2,'A','n'

create table table2(id int,order_id int,remove varchar(1))
insert into table2
select 1,1,'y'
union all select 2,1,'y'
union all select 3,1,'n'
union all select 4,2,'n'
union all select 5,2,'n'
go

--查询
select 公司名称=a.company
,总订单数=count(a.id)
,成功订单数=sum(case a.result when 'y' then 1 else 0 end)
,成功订票数=sum(case when a.result='y' and b.remove='y'then 1 else 0 end)
from table1 a join table2 b on a.id=b.order_id
group by a.company
go

--删除测试
drop table table1,table2

/*--测试结果
公司名称 总订单数 成功订单数 成功订票数
---- ----------- ----------- -----------
A 5 3 2

(所影响的行数为 1 行)

--*/
zjcxc 元老 2003-12-17
  • 打赏
  • 举报
回复
--建议用:
select 公司名称=a.company
,总订单数=count(a.id)
,成功订单数=sum(case a.result when 'y' then 1 else 0 end)
,成功订票数=sum(case when a.result='y' and b.remove='y'then 1 else 0 end)
from table1 a join table2 b on a.id=b.order_id
group by a.company
victorycyz 2003-12-17
  • 打赏
  • 举报
回复
好象昨天已经帮你做过了。
zjcxc 元老 2003-12-17
  • 打赏
  • 举报
回复
select 公司名称=table1.company
,总订单数=count(table1.id)
,成功订单数=sum(case table1.result when 'y' then 1 else 0 end)
,成功订票数=sum(case when table1.result='y' and table2.remove='y'then 1 else 0 end)from 表 where table1.id=table2.order_id (主外键相关联)
group by table1.company
Rotaxe 2003-12-17
  • 打赏
  • 举报
回复
试一下
select 公司名称=table1.company
,总订单数=count(table1.id)
,成功订单数=sum(case table1.result when 'y' then 1 else 0 end)
,成功订票数=sum(case when table1.result='y' and table2.remove='y' then 1 else 0 end)from 表 where table1.id=table2.order_id
group by table1.company

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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