首頁 >資料庫 >mysql教程 >SQLServer基础语法实例应用(二)

SQLServer基础语法实例应用(二)

WBOY
WBOY原創
2016-06-07 16:20:281069瀏覽

-- 测试数据:[a] if object_id('[a]') is not null drop table [a] go create table [a]([ID] int,[品名] varchar(6),[入库数量] int,[入库时间] datetime) insert [a] select 1,'矿泉水',100,'2013-01-02' union all select 2,'方便面',60,'2013-01-03' un

--> 测试数据:[a]

if object_id('[a]') is not null drop table [a]

go

create table [a]([ID] int,[品名] varchar(6),[入库数量] int,[入库时间] datetime)

insert [a]

select 1,'矿泉水',100,'2013-01-02' union all

select 2,'方便面',60,'2013-01-03' union all

select 3,'方便面',50,'2013-01-03' union all

select 4,'矿泉水',80,'2013-01-04' union all

select 5,'方便面',50,'2013-01-05'

 

select a.[品名],a.[入库数量],b.[最后入库时间] from [test] a  ,

(select  [品名],max([入库时间]) as '最后入库时间' from [test]  group by [品名]) b

where a.[品名]=b.[品名]

 

/*

品名     入库数量        最后入库时间

------ ----------- -----------------------

方便面    60          2013-01-05 00:00:00.000

方便面    50          2013-01-05 00:00:00.000

方便面    50          2013-01-05 00:00:00.000

矿泉水    100         2013-01-04 00:00:00.000

矿泉水    80          2013-01-04 00:00:00.000

 

(5 行受影响)

 

*/

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn