日期:2014-05-17 浏览次数:20620 次
SELECT ItemNumber,
LastCountDate = MAX(UploadDate),
VarianceQuantity = MAX(VarianceQuantity)
FROM pwh.dbo.CycleCountUploadLog A WITH(NOLOCK)
GROUP BY ItemNumber
ORDER BY ItemNumber
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-02-21 11:30:24
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64)
-- Jun 17 2011 00:54:03
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1, v.721)
--
----------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([ID] int,[ItemNumber] varchar(11),[Quantity] int,[InDate] datetime)
insert [huang]
select 1,'03-001-3901',10,'2012-08-15 18:59:28.717' union all
select 2,'03-001-3901',20,'2012-08-14 18:59:28.717' union all
select 3,'03-003-681',30,'2012-09-15 18:59:28.717'
--------------开始查询--------------------------
SELECT *
FROM [huang] a
WHERE EXISTS ( SELECT 1
FROM ( SELECT ItemNumber ,
MAX(InDate)[InDate]
FROM huang
&nb