另开一贴,简化问题,求sql语句。

youzhj 2009-06-23 02:38:14
表Tb1: Sdt(datetime) Ua1 Ub1 Uc1
2009-6-23 10:52:00 25 32 26
2009-6-23 10:57:00 35 75 38
2009-6-23 11:02:00 58 68 39
2009-6-24 11:07:00 28 69 46
2009-6-24 11:12:00 25 58 37
2009-6-24 11:17:00 25 58 37
表Tb2: Sdt(datetime) Ua2 Ub2 Uc2
2009-6-23 10:52:00 67 54 45
2009-6-23 10:57:00 38 69 33
2009-6-24 11:02:00 56 84 39
2009-6-24 11:07:00 76 36 46
2009-6-24 11:12:00 64 66 37
2009-6-24 11:17:00 250 47 60
求每天(以一日为单位)tb1和tb2中Ua1,Ub1,Uc1和Ua2,Ub2,Uc2的最大值,并存到表tb3(sDt smalldatetime,maxUa1,maxUb1,maxUc1,maxUa2,maxUb2,maxUc2)中?
...全文
35 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
youzhj 2009-06-23
  • 打赏
  • 举报
回复
通知:feixianxxx,csdyyr两位的分数于另一贴中给了,此贴不再给分。谢谢你们
谢谢各位帮小弟解决了问题。
祝各位工作愉快,万事如意!
csdyyr 2009-06-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 csdyyr 的回复:]
引用 7 楼 csdyyr 的回复:
SQL codeselectisnull(ta.Sdt),Ua1,Ua2from(selectconvert(varchar(10),Sdt,120)asSdt,max(Ua1)asUa1fromAgroupbyconvert(varchar(10),Sdt,120)
) tafulljoin(selectconvert(varchar(10),Sdt,120),max(Ua1)asUa1fromBgroupbyconvert(varchar(10),Sdt,120)
) tbonta.Sdt=tb.Sdt

SQL codeselectisnull(ta.Sdt),Ua1,Ua2from(selectconvert(varchar(10),Sdt,120)asSdt,max(Ua1)asUa1fromAgroupbyconv…
[/Quote]

select isnull(ta.Sdt,tb.Sdt),Ua1,Ua2
from (
select convert(varchar(10),Sdt,120) as Sdt,max(Ua1) as Ua1
from A
group by convert(varchar(10),Sdt,120)
) ta
full join (
select convert(varchar(10),Sdt,120),max(Ua2) as Ua2
from B
group by convert(varchar(10),Sdt,120)
) tb
on ta.Sdt=tb.Sdt

feixianxxx 2009-06-23
  • 打赏
  • 举报
回复
create table tb1(sdt datetime ,ua1 int,ub1 int,uc1 int) 
create table tb2(sdt datetime ,ua2 int,ub2 int,uc2 int)
insert TB1
select '2009-6-23 10:52:00',25 , 32, 26 union all
select '2009-6-23 10:52:00',35 , 75, 38 union all
select '2009-6-23 10:52:00',58 , 68, 39 union all
select '2009-6-24 10:52:00',28 , 69, 46 union all
select '2009-6-24 10:52:00',25 , 58, 37 union all
select '2009-6-24 10:52:00',25 , 58, 37
insert TB2
select '2009-6-23 10:52:00', 67 ,54 ,45 union all
select '2009-6-23 10:52:00', 38 ,69 ,33 union all
select '2009-6-24 10:52:00', 56 ,84 , 44 union all
select '2009-6-24 10:52:00', 76 ,46 ,46 union all
select '2009-6-24 10:52:00', 64 ,66 ,37 union all
select '2009-6-24 10:52:00', 250 ,47 ,60
go
select t.sdt,maxUa1,maxUb1,maxUc1,maxUa2,maxUb2,maxUc2
from (
select convert(varchar(10),sdt,120) sdt ,max(ua1) as maxUa1,max(ub1) as maxub1,MAX(uc1) as maxuc1
from TB1 group by convert(varchar(10),sdt,120) ) t join
(select convert(varchar(10),sdt,120) sdt ,max(ua2) as maxUa2,max(ub2) as maxub2,MAX(uc2) as maxuc2
from TB2 group by convert(varchar(10),sdt,120) ) k on t.sdt=k.sdt

/*
2009-06-23 58 75 39 67 69 45
2009-06-24 28 69 46 250 84 60
*/
feixianxxx 2009-06-23
  • 打赏
  • 举报
回复
create table tb1(sdt datetime ,ua1 int,ub1 int,uc1 int)
create table tb2(sdt datetime ,ua2 int,ub2 int,uc2 int)
insert TB1
select '2009-6-23 10:52:00',25 , 32, 26 union all
select '2009-6-23 10:52:00',35 , 75, 38 union all
select '2009-6-23 10:52:00',58 , 68, 39 union all
select '2009-6-24 10:52:00',28 , 69, 46 union all
select '2009-6-24 10:52:00',25 , 58, 37 union all
select '2009-6-24 10:52:00',25 , 58, 37
insert TB2
select '2009-6-23 10:52:00', 67 ,54 ,45 union all
select '2009-6-23 10:52:00', 38 ,69 ,33 union all
select '2009-6-24 10:52:00', 56 ,84 , 44 union all
select '2009-6-24 10:52:00', 76 ,46 ,46 union all
select '2009-6-24 10:52:00', 64 ,66 ,37 union all
select '2009-6-24 10:52:00', 250 ,47 ,60
go
select t.sdt,maxUa1,maxUb1,maxUc1,maxUa2,maxUb2,maxUc2
from (
select convert(varchar(10),sdt,120) sdt ,max(ua1) as maxUa1,max(ub1) as maxub1,MAX(uc1) as maxuc1
from TB1 group by convert(varchar(10),sdt,120) ) t join
(select convert(varchar(10),sdt,120) sdt ,max(ua2) as maxUa2,max(ub2) as maxub2,MAX(uc2) as maxuc2
from TB2 group by convert(varchar(10),sdt,120) ) k on t.sdt=k.sdt

/*
2009-06-23 58 75 39 67 69 45
2009-06-24 28 69 46 250 84 60
*/
Small_Four 2009-06-23
  • 打赏
  • 举报
回复

CREATE TABLE tb1
(
Sdt datetime,
Ua1 int,
Ub1 int,
Uc1 int,
)
CREATE TABLE tb2
(
Sdt datetime,
Ua2 int,
Ub2 int,
Uc2 int,
)

INSERT INTO tb1
SELECT '2009-6-23 10:52:00', 25, 32, 26 UNION ALL
SELECT '2009-6-23 10:57:00', 35, 75, 38 UNION ALL
SELECT '2009-6-23 11:02:00', 58, 68, 39 UNION ALL
SELECT '2009-6-24 11:07:00', 28, 69, 46 UNION ALL
SELECT '2009-6-24 11:12:00', 25, 58, 37 UNION ALL
SELECT '2009-6-24 11:12:00', 25, 58, 37

INSERT INTO tb2
SELECT '2009-6-23 10:52:00', 67, 54, 45 UNION ALL
SELECT '2009-6-23 10:57:00', 38, 69, 33 UNION ALL
SELECT '2009-6-23 11:02:00', 56, 84, 39 UNION ALL
SELECT '2009-6-24 11:07:00', 76, 36, 46 UNION ALL
SELECT '2009-6-24 11:12:00', 64, 66, 37 UNION ALL
SELECT '2009-6-24 11:12:00', 250, 47, 60


SELECT
A.Sdt,
A.Ua1, A.Ub1, A.Uc1,
B.Ua2, B.Ub2, B.Uc2
INTO tb3 FROM
(
SELECT
Sdt = CONVERT(varchar, Sdt, 112),
Ua1 = MAX(Ua1),
Ub1 = MAX(Ub1),
Uc1 = MAX(Uc1)
FROM tb1
GROUP BY CONVERT(varchar, Sdt, 112)
) A
LEFT JOIN
(
SELECT
Sdt = CONVERT(varchar, Sdt, 112),
Ua2 = MAX(Ua2),
Ub2 = MAX(Ub2),
Uc2 = MAX(Uc2)
FROM tb2
GROUP BY CONVERT(varchar, Sdt, 112)
) B
ON A.Sdt = B.Sdt

SELECT * FROM tb3

DROP TABLE tb1
DROP TABLE tb2
DROP TABLE tb3
xiequan2 2009-06-23
  • 打赏
  • 举报
回复
group by convert(varchar(10),sdt,120)
xiequan2 2009-06-23
  • 打赏
  • 举报
回复

insert into tb3
select convert(varchar(10),sdt,120) sdt,max(Ua1),max(Ub1),max(Uc1),max(Ua2),max(Ub2),max(Uc2) from tb1 join tb2 on tb1.sdt=tb2.sdt
feixianxxx 2009-06-23
  • 打赏
  • 举报
回复
select sdt,maxUa1,maxUb1,maxUc1,maxUa2,maxUb2,maxUc2
from (
select convert(varchar(10),sdt,120) sdt ,max(ua1) as maxUa1,max(ub1) as maxub1,MAX(uc1) as maxuc1
from TB1 group by convert(varchar(10),sdt,120) ) t join
(select convert(varchar(10),sdt,120) sdt ,max(ua2) as maxUa2,max(ub2) as maxub2,MAX(uc2) as maxuc2
from TB2 group by convert(varchar(10),sdt,120) ) k on t.sdt=k.sdt
youzhj 2009-06-23
  • 打赏
  • 举报
回复
麻烦在这个帖子给代码的大侠,去这个帖子留个名,http://topic.csdn.net/u/20090623/11/e158bb0b-7d6c-4f7b-8bb1-d6469011fa8e.html?83944
因为这里我给的分多一些,以便给分,小弟分比较少,抱歉了。
csdyyr 2009-06-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 csdyyr 的回复:]
SQL codeselectisnull(ta.Sdt),Ua1,Ua2from(selectconvert(varchar(10),Sdt,120)asSdt,max(Ua1)asUa1fromAgroupbyconvert(varchar(10),Sdt,120)
) tafulljoin(selectconvert(varchar(10),Sdt,120),max(Ua1)asUa1fromBgroupbyconvert(varchar(10),Sdt,120)
) tbonta.Sdt=tb.Sdt
[/Quote]

select isnull(ta.Sdt),Ua1,Ua2
from (
select convert(varchar(10),Sdt,120) as Sdt,max(Ua1) as Ua1
from A
group by convert(varchar(10),Sdt,120)
) ta
full join (
select convert(varchar(10),Sdt,120),max(Ua2) as Ua2
from B
group by convert(varchar(10),Sdt,120)
) tb
on ta.Sdt=tb.Sdt
feixianxxx 2009-06-23
  • 打赏
  • 举报
回复
select sdt,maxUa1,maxUb1,maxUc1,maxUa2,maxUb2,maxUc2
from (
select sdt ,max(ua1) as maxUa1,max(ub1) as maxub1,MAX(uc1) as maxuc1
from TB1 group by sdt ) t join
(select sdt ,max(ua2) as maxUa2,max(ub2) as maxub2,MAX(uc2) as maxuc2
from TB2 group by sdt ) k on t.sdt=k.sdt
csdyyr 2009-06-23
  • 打赏
  • 举报
回复
select isnull(ta.Sdt),Ua1,Ua2
from (
select convert(varchar(10),Sdt,120) as Sdt,max(Ua1) as Ua1
from A
group by convert(varchar(10),Sdt,120)
) ta
full join (
select convert(varchar(10),Sdt,120),max(Ua1) as Ua1
from B
group by convert(varchar(10),Sdt,120)
) tb
on ta.Sdt=tb.Sdt
youzhj 2009-06-23
  • 打赏
  • 举报
回复
我只知道用
insert into tb3
select sDt,maxUa1,maxUb1,maxUc1,maxUa2,maxUb2,maxUc2
from .....
可是两个表,我就写不出来。
ai_li7758521 2009-06-23
  • 打赏
  • 举报
回复
insert tb3(sDt smalldatetime,maxUa1,maxUb1,maxUc1,maxUa2,maxUb2,maxUc2)
select A.*,B.Ua2,B.Ub2,B.Uc2
from
(
select Sdt=convert(varchar(10),Sdt,120), Ua1=max(Ua1),Ub1=max(Ub1),Uc1=max(Uc1)
from Tb1
group by convert(varchar(10),Sdt,120)
) A
join
(
select Sdt=convert(varchar(10),Sdt,120), Ua2=max(Ua2),Ub2=max(Ub2),Uc2=max(Uc2)
from Tb2
group by convert(varchar(10),Sdt,120)
) B
on A.Sdt=B.Sdt
tim_spac 2009-06-23
  • 打赏
  • 举报
回复
select sdt, 
max(maxua1) as maxua1,
max(maxub1) as maxub1,
max(maxuc1) as maxuc1,
max(maxua2) as maxua2,
max(maxub2) as maxub2,
max(maxuc2) as maxuc2
from (
select dateadd(day,datediff(day,0,std),0) as sdt,
max(ua1) as maxua1,
max(ub1) as maxub1,
max(uc1) as maxuc1,
maxua2 = 0, maxub2 = 0, maxuc2 = 0
from tb1
group by dateadd(day,datediff(day,0,std),0)
union
select dateadd(day,datediff(day,0,std),0) as sdt,
maxua1 = 0, maxub1 = 0, maxuc1 = 0
max(ua2) as maxua2,
max(ub2) as maxub2,
max(uc2) as maxuc2
from tb2
group by dateadd(day,datediff(day,0,std),0)
) as a
group by std
ai_li7758521 2009-06-23
  • 打赏
  • 举报
回复
select A.*,B.Ua2,B.Ub2,B.Uc2 
from
(
select Sdt=convert(varchar(10),Sdt,120), Ua1=max(Ua1),Ub1=max(Ub1),Uc1=max(Uc1)
from Tb1
group by convert(varchar(10),Sdt,120)
) A
join
(
select Sdt=convert(varchar(10),Sdt,120), Ua2=max(Ua2),Ub2=max(Ub2),Uc2=max(Uc2)
from Tb2
group by convert(varchar(10),Sdt,120)
) B
on A.Sdt=B.Sdt
youzhj 2009-06-23
  • 打赏
  • 举报
回复
不哈意思,能不能给我整个句子啊,我现在就要用,而且按你给的句子,我目前水平还写不出来。
jinjazz 2009-06-23
  • 打赏
  • 举报
回复
最简单的
row_number()over(partition by cast(sdt as int) order by sdt desc)

34,590

社区成员

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

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