日期:2014-05-17 浏览次数:20619 次
客户代码 客户型号 客户订单号 本厂型号 币种 单价 数量 下单日期
YTILZ 2X01900FC1 YT-1303028 FP2-3883 USD 1.326 15447 2013-04-02 18:15:00
YTILZ 2X01900FC1 YT-1304001 FP2-3883 USD 1.326 3000 2013-04-08 09:42:00
YTILZ 2X01900FC1 YT-1304001 FP2-3883 USD 1.326 4460 2013-04-08 09:44:00
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-10-21 12:05:30
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([客户代码] varchar(5),[客户型号] varchar(10),[客户订单号] varchar(10),[本厂型号] varchar(8),[币种] varchar(3),[单价] numeric(4,3),[数量] int,[下单日期] datetime)
insert [huang]
select 'YTILZ','2X01900FC1','YT-1303028','FP2-3883','USD',1.326,15447,'2013-04-02 18:15:00' union all
select 'YTILZ','2X01900FC1','YT-1304001','FP2-3883','USD',1.326,3000,'2013-04-08 09:42:00' union all
select 'YTILZ','2X01900FC1','YT-1304001','FP2-3883','USD',1.326,4460,'2013-04-08 09:44:00'
--------------开始查询--------------------------
select *
from [huang] a
where exists(select 1 from (select 客户代码,max(下单日期)下单日期 from [huang] group by 客户代码)b where a.客户代码=b.客户代码 and a.下单日期=b.下单日期)
----------------结果----------------------------
/*
客户代码 客户型号 客户订单号 本厂型号 币种 单价 数量 下单日期
----- ---------- ---------- -------- ---- -------------------------------