关于SQL查询结果的问题。

royy918 2012-06-20 11:15:37
有一个表,其中主要有3个字段:
货号、收货时间、收货数量。

每个货号可能有多次收货(收货时间不同),每次收货的数量可能一样也可能不一样。

现在有个需求是:
需要提取每个货号最后一次收货时间和最后一次收货数量,要求用一条语句实现,最好不要用子查询。
例如:
货号---------收货时间---------收货数量
1123 2012-06-10 10
1124 2012-06-11 13
1123 2012-06-15 22
1125 2012-06-19 9
1122 2012-06-01 14
1123 2012-06-19 20

查询得到:
货号---------收货时间---------收货数量
1123 2012-06-19 20
1124 2012-06-11 13
1125 2012-06-19 9
1122 2012-06-01 14

请各位大侠不吝赐教,谢谢。
...全文
105 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
gogodiy 2012-06-21
  • 打赏
  • 举报
回复

CREATE TABLE t1
(
huohao INT,
shijian DATETIME,
shuliang INT
)
INSERT INTO t1
SELECT 1123, '2012-06-10', 10 UNION ALL
SELECT 1124, '2012-06-11', 13 UNION ALL
SELECT 1123, '2012-06-15', 22 UNION ALL
SELECT 1125, '2012-06-19', 9 UNION ALL
SELECT 1122, '2012-06-01', 14 UNION ALL
SELECT 1123, '2012-06-19', 20
SELECT * FROM t1

SELECT * FROM t1 AS a WHERE NOT EXISTS (SELECT 1 FROM t1 WHERE huohao=a.huohao AND shijian>a.shijian)
ORDER BY huohao

---------------------------
huohao shijian shuliang
1122 2012-06-01 00:00:00.000 14
1123 2012-06-19 00:00:00.000 20
1124 2012-06-11 00:00:00.000 13
1125 2012-06-19 00:00:00.000 9
royy918 2012-06-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

引用 6 楼 的回复:
引用 4 楼 的回复:

SQL code
select [货号],MAX([收货时间]) 收货时间,[收货数量] from tb group by [货号],[收货数量]

大哥,这个试了不行。

大哥我的肯定可以!!
[/Quote]

我这边试了,货号会有重复的,要求每个货号只出现一次。
天-笑 2012-06-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
引用 4 楼 的回复:

SQL code
select [货号],MAX([收货时间]) 收货时间,[收货数量] from tb group by [货号],[收货数量]

大哥,这个试了不行。
[/Quote]
大哥我的肯定可以!!
royy918 2012-06-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

SQL code
select [货号],MAX([收货时间]) 收货时间,[收货数量] from tb group by [货号],[收货数量]
[/Quote]
大哥,这个试了不行。
royy918 2012-06-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

SQL code

--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([货号] int,[收货时间] datetime,[收货数量] int)
insert [test]
select 1123,'2012-06-10',10 union all
select 112……
[/Quote]

没有其他办法了吗?我这边数据量很大,收货记录表记录有几百万条
royy918 2012-06-20
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

SQL code

if object_id('[test]') is not null drop table [test]
create table [test]([货号] int,[收货时间] datetime,[收货数量] int)
insert [test]
select 1123,'2012-06-10',10 union all
select 1124,'2012-06-11',1……
[/Quote]
大哥,能不能不输出Row???
Felixzhaowenzhong 2012-06-20
  • 打赏
  • 举报
回复
select [货号],MAX([收货时间]) 收货时间,[收货数量] from tb group by [货号],[收货数量]
  • 打赏
  • 举报
回复

--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([货号] int,[收货时间] datetime,[收货数量] int)
insert [test]
select 1123,'2012-06-10',10 union all
select 1124,'2012-06-11',13 union all
select 1123,'2012-06-15',22 union all
select 1125,'2012-06-19',9 union all
select 1122,'2012-06-01',14 union all
select 1123,'2012-06-19',20

--看错了,改好了

select distinct * from test a
where a.收货时间=(select MAX(b.收货时间) from test b where a.货号=b.货号)

/*
货号 收货时间 收货数量
---------------------------------------------
1122 2012-06-01 00:00:00.000 14
1123 2012-06-19 00:00:00.000 20
1124 2012-06-11 00:00:00.000 13
1125 2012-06-19 00:00:00.000 9
*/
  • 打赏
  • 举报
回复

--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([货号] int,[收货时间] datetime,[收货数量] int)
insert [test]
select 1123,'2012-06-10',10 union all
select 1124,'2012-06-11',13 union all
select 1123,'2012-06-15',22 union all
select 1125,'2012-06-19',9 union all
select 1122,'2012-06-01',14 union all
select 1123,'2012-06-19',20


select distinct * from test a
where a.收货时间=(select MAX(b.收货时间) from test b where a.货号=b.货号)
or a.收货时间=(select MIN(c.收货时间) from test c where a.货号=c.货号)

/*
货号 收货时间 收货数量
---------------------------------------------
1122 2012-06-01 00:00:00.000 14
1123 2012-06-10 00:00:00.000 10
1123 2012-06-19 00:00:00.000 20
1124 2012-06-11 00:00:00.000 13
1125 2012-06-19 00:00:00.000 9
*/
天-笑 2012-06-20
  • 打赏
  • 举报
回复

;with T as
(
select row_number() over( PARTITION BY [货号] order by [收货时间] desc ) as Row, * from [table]
)
select * from t where Row = 1
天-笑 2012-06-20
  • 打赏
  • 举报
回复

if object_id('[test]') is not null drop table [test]
create table [test]([货号] int,[收货时间] datetime,[收货数量] int)
insert [test]
select 1123,'2012-06-10',10 union all
select 1124,'2012-06-11',13 union all
select 1123,'2012-06-15',22 union all
select 1124,'2012-06-15',30 union all
select 1125,'2012-06-19',9 union all
select 1122,'2012-06-01',14 union all
select 1125,'2012-06-20',19 union all
select 1123,'2012-06-19',20

;with T as
(
select row_number() over( PARTITION BY [货号] order by [收货时间] desc ) as Row, * from [test]
)
select * from t where Row = 1

(8 行受影响)
Row 货号 收货时间 收货数量
-------------------- ----------- ----------------------- -----------
1 1122 2012-06-01 00:00:00.000 14
1 1123 2012-06-19 00:00:00.000 20
1 1124 2012-06-15 00:00:00.000 30
1 1125 2012-06-20 00:00:00.000 19

(4 行受影响)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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