急,求一个sql语句!!

youzhj 2013-03-13 10:27:06

表T_Result(varchar,datetime,int,int,int)
BarCode dt TP0 TP1 TestType
aaa 2013-2-3 12 13 0
aaa 2013-2-4 11 14 0
aaa 2013-3-6 15 23 1
aaa 2013-3-7 17 25 1
--------------------------------------------
表M_Result(varchar,datetime,int,int,int)
BarCode dt TP10 TP11 TestType
aaa 2013-1-3 15 17 0
aaa 2013-2-1 13 12 0
aaa 2013-1-16 21 22 1
aaa 2013-1-17 27 35 1
----------------------------------------------
想得到以下结果:
BarCode dt TP0 TP1 TP10 TP11 TestType
aaa 2013-2-4 11 14 13 12 0
aaa 2013-3-7 17 25 27 35 1
说明:按照日期排序desc,按照TestType分类,各类取时间最新的那条记录。
请教句子该如何写?谢谢啦
...全文
531 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
youzhj 2013-03-14
  • 打赏
  • 举报
回复
我可能没说清楚,你们给的代码测试了都不是我要的结果啊。
我是想把T_Result as t中的t.BarCode,t.dt,t.TP0,t.TP1,t.TestType按t.TestType分组,取各组中t.dt最大的记录;
同时还要把M_Result as m 中的m.TP10,m.TP11按照m.TestType分组,取各组中m.dt最大的记录;
然后把上面两个记录合并成在一起。不知道我这样说清楚了没,请大家再耐心帮我看看,谢谢。
习惯性蹭分 2013-03-14
  • 打赏
  • 举报
回复

with a as(select * from t_result t1 where not exists(
select 1 from t_result where testtype=t1.testtype and dt<t1.dt
)),
b as(
select * from m_result m1 where not exists(
select 1 from m_result where testtype=m1.testtype and dt<t1.dt
))
select a.barcode,a.dt,tp0,tp1,tp10,tp11,a.testtype from a join b
on a.testtype=b.testtype
youzhj 2013-03-14
  • 打赏
  • 举报
回复
不好意思,今天重装了一下电脑,现在正在装sql2008,一会装好了试试大家给的句子,谢谢哈。
  • 打赏
  • 举报
回复

select barcode,dt,tp0,tp1,tp10,tp11,testtype from(select row=row_number()over(partition by a.barcode,a.testtype order by (case when a.dt<b.dt then b.dt else a.dt end) desc),a.barcode, dt=(case when a.dt<b.dt then b.dt else a.dt end),a.tp0, a.tp1, b.tp10, b.tp11, a.testtype
 from t_result a join m_result b on a.barcode=b.barcode and a.testtype=b.testtype)t where row=1
youzhj 2013-03-14
  • 打赏
  • 举报
回复
引用 4 楼 Vidor 的回复:
引用 3 楼 youzhj 的回复: 我所给的两个表及其中的数据都是举例而已,请大侠帮忙再看看还有没有别的办法? SQL code ? 12345 select a.BarCode, -- BarCode取哪个自己定逻辑 case when a.dt<b.dt then b.dt else a.dt end dt, a.TP0, a.TP1, b.TP……
从你的句子中得到了启发,非常感谢。
Vidor 2013-03-13
  • 打赏
  • 举报
回复
引用 3 楼 youzhj 的回复:
我所给的两个表及其中的数据都是举例而已,请大侠帮忙再看看还有没有别的办法?
select a.BarCode, -- BarCode取哪个自己定逻辑
	case when a.dt<b.dt then b.dt else a.dt end dt, a.TP0, a.TP1, b.TP10, b.TP11, a.TestType
from T_Result a, M_Result b where a.TestType=b.TestType
	and not exists (select 1 from T_Result where TestType=a.TestType and dt>a.dt)
	and not exists (select 1 from M_Result where TestType=b.TestType and dt>b.dt)
youzhj 2013-03-13
  • 打赏
  • 举报
回复
我所给的两个表及其中的数据都是举例而已,请大侠帮忙再看看还有没有别的办法?
youzhj 2013-03-13
  • 打赏
  • 举报
回复
引用 1 楼 Vidor 的回复:
SQL code?1234567with cte as( select * from T_Result union all select * from M_Result)select * from cte as t where not exists (select 1 from cte where TestType=t.TestType and dt>t……
消息 205,级别 16,状态 1,第 1 行 使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式。 实际上,两个表的列数不同,且从两个表中所取的列数也不一样。
Vidor 2013-03-13
  • 打赏
  • 举报
回复
with cte as
(
	select * from T_Result
	union all
	select * from M_Result
)
select * from cte as t where not exists (select 1 from cte where TestType=t.TestType and dt>t.dt)

34,837

社区成员

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

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