数据查询

小宏 2010-12-16 03:42:12
表1:(省份、城市、数量、id)
江苏 南京 3 1
江苏 南京 3 2
江苏 南京 3 3
表2结果与表1一样
江苏 南京 2 4
江苏 南京 2 5
结果:
江苏 南京 5 1
江苏 南京 5 2
江苏 南京 5 3
江苏 南京 5 4
江苏 南京 5 5
2个表都是临时只是里面存放的数据不同结果是一样的
各位帮忙想下.
...全文
44 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
小宏 2010-12-16
  • 打赏
  • 举报
回复
谢谢,按照蛋兄思路解决了!
哈哈结贴
Rockets_NBA 2010-12-16
  • 打赏
  • 举报
回复
哎,感觉写得很牵强,也不知道道理对不对,可能只有楼主知道了
--> 测试数据: #TA
if object_id('tempdb.dbo.#TA') is not null drop table #TA
GO
create table #TA (省份 varchar(4),城市 varchar(4),数 int,id int)
insert into #TA
select '江苏','南京',3,1 union all
select '江苏','南京',3,2 union all
select '江苏','南京',3,3
--> 测试数据: #TB
if object_id('tempdb.dbo.#TB') is not null drop table #TB
GO
create table #TB (省份 varchar(4),城市 varchar(4),数 int,id int)
insert into #TB
select '江苏','南京',2,4 union all
select '江苏','南京',2,5

;with cte as
(
select 省份,城市,id from #TA
UNION ALL
select 省份,城市,id from #TB
)
select *,n=(select sum(distinct 数) from #ta) +(select sum(distinct 数) from #tb)
from cte


省份 城市 id n
---- ---- ----------- -----------
江苏 南京 1 5
江苏 南京 2 5
江苏 南京 3 5
江苏 南京 4 5
江苏 南京 5 5

(5 行受影响)
Rockets_NBA 2010-12-16
  • 打赏
  • 举报
回复
--> 测试数据: #TA
if object_id('tempdb.dbo.#TA') is not null drop table #TA
GO
create table #TA (省份 varchar(4),城市 varchar(4),数 int,id int)
insert into #TA
select '江苏','南京',3,1 union all
select '江苏','南京',3,2 union all
select '江苏','南京',3,3
--> 测试数据: #TB
if object_id('tempdb.dbo.#TB') is not null drop table #TB
GO
create table #TB (省份 varchar(4),城市 varchar(4),数 int,id int)
insert into #TB
select '江苏','南京',2,4 union all
select '江苏','南京',2,5

;with cte as
(
select 省份,城市,id from #TA
UNION ALL
select 省份,城市,id from #TB
)
select *,n=(select count(*) from cte) from cte


省份 城市 id n
---- ---- ----------- -----------
江苏 南京 1 5
江苏 南京 2 5
江苏 南京 3 5
江苏 南京 4 5
江苏 南京 5 5

(5 行受影响)
小宏 2010-12-16
  • 打赏
  • 举报
回复
表1:(省份、城市、有效数量、id)
江苏 南京 3 1
江苏 南京 3 2
江苏 南京 3 3
江苏 南京 3 4
江苏 南京 3 5
表2结果与表1一样
江苏 南京 2 6
江苏 南京 2 7
结果:
江苏 南京 5 1
江苏 南京 5 2
江苏 南京 5 3
江苏 南京 5 4
江苏 南京 5 5
江苏 南京 5 6
江苏 南京 5 7
其实核实数量和后面的id没有关系,可能题目里面给的数据会误导
核实数据量是所有给的数据的核实有效数,后面的id列出的是所有的数据
小宏 2010-12-16
  • 打赏
  • 举报
回复
题目中的倒数第二行修改
结果——>结构
不好意思

34,591

社区成员

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

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