表a 含有单号(tc_xub01) 日期(tc_xub02) 表b 含有单号(tc_xud01) 料号(tc_xud05) 表c 含有料号(tc_xua03)
将三个表串起来查出 表c 和 表b 相同料号的 最大日期! 应该怎么写!~
我自己写的不对只能查出整个表中的最大日期!select max(tc_xub02) from tc_xub_file,tc_xud_file,tc_xua_file where tc_xub01 = tc_xud01 and tc_xud05 = tc_xua03这样写只会查出表c中与表b中相同料号整体的最大日期
而不是想要每一笔的最大日期!~
各位帮帮忙!~脑袋有些糊涂写不明白了!
将三个表串起来查出 表c 和 表b 相同料号的 最大日期! 应该怎么写!~
我自己写的不对只能查出整个表中的最大日期!select max(tc_xub02) from tc_xub_file,tc_xud_file,tc_xua_file where tc_xub01 = tc_xud01 and tc_xud05 = tc_xua03这样写只会查出表c中与表b中相同料号整体的最大日期
而不是想要每一笔的最大日期!~
各位帮帮忙!~脑袋有些糊涂写不明白了!
-- Author: liangCK 小梁
-- Title : 查每个分组前N条记录
-- Date : 2008-11-13 17:19:23
-----------------------------------> 生成测试数据: #T
IF OBJECT_ID('tempdb.dbo.#T') IS NOT NULL DROP TABLE #T
CREATE TABLE #T (ID VARCHAR(3),GID INT,Author VARCHAR(29),Title VARCHAR(39),Date DATETIME)
INSERT INTO #T
SELECT '001',1,'邹建','深入浅出SQLServer2005开发管理与应用实例','2008-05-10' UNION ALL
SELECT '002',1,'胡百敬','SQLServer2005性能调校','2008-03-22' UNION ALL
SELECT '003',1,'格罗夫Groff.J.R.','SQL完全手册','2009-07-01' UNION ALL
SELECT '004',1,'KalenDelaney','SQLServer2005技术内幕存储引擎','2008-08-01' UNION ALL
SELECT '005',2,'Alex.Kriegel.Boris.M.Trukhnov','SQL宝典','2007-10-05' UNION ALL
SELECT '006',2,'飞思科技产品研发中心','SQLServer2000高级管理与开发','2007-09-10' UNION ALL
SELECT '007',2,'胡百敬','SQLServer2005数据库开发详解','2008-06-15' UNION ALL
SELECT '008',3,'陈浩奎','SQLServer2000存储过程与XML编程','2005-09-01' UNION ALL
SELECT '009',3,'赵松涛','SQLServer2005系统管理实录','2008-10-01' UNION ALL
SELECT '010',3,'黄占涛','SQL技术手册','2006-01-01'--SQL查询如下:--按GID分组,查每个分组中Date最新的前2条记录
--1.字段ID唯一时:
SELECT * FROM #T AS T WHERE ID IN(SELECT TOP 2 ID FROM #T WHERE GID=T.GID ORDER BY Date DESC)--2.如果ID不唯一时:
SELECT * FROM #T AS T WHERE 2>(SELECT COUNT(*) FROM #T WHERE GID=T.GID AND Date>T.Date)--SQL Server 2005 使用新方法--3.使用ROW_NUMBER()进行排位分组
SELECT ID,GID,Author,Title,Date
FROM
(
SELECT rid=ROW_NUMBER() OVER(PARTITION BY GID ORDER BY Date DESC),*
FROM #T
) AS T
WHERE rid<=2--4.使用APPLY
SELECT DISTINCT b.*
FROM #T AS a
CROSS APPLY
(
SELECT TOP(2) * FROM #T WHERE a.GID=GID ORDER BY Date DESC
) AS b
--结果
/*ID GID Author Title Date
---- ----------- ----------------------------- --------------------------------------- -----------------------
003 1 格罗夫Groff.J.R. SQL完全手册 2009-07-01 00:00:00.000
004 1 KalenDelaney SQLServer2005技术内幕存储引擎 2008-08-01 00:00:00.000
005 2 Alex.Kriegel.Boris.M.Trukhnov SQL宝典 2007-10-05 00:00:00.000
007 2 胡百敬 SQLServer2005数据库开发详解 2008-06-15 00:00:00.000
009 3 赵松涛 SQLServer2005系统管理实录 2008-10-01 00:00:00.000
010 3 黄占涛 SQL技术手册 2006-01-01 00:00:00.000(6 行受影响)
*/
select c.tc_xua03,max(tc_xub02)
from tc_xub_file a,tc_xud_file b,tc_xua_file c
where a.tc_xub01 = b.tc_xud01 and b.tc_xud05 = c.tc_xua03
group by c.tc_xua03
from a, b, c
where a.tc_xub01 = b.tc_xud01 and b.tc_xud05 = c.tc_xua03
group by a.tc_xub01select a.tc_xub01 , b.tc_xud05 , max(a.tc_xub02) tc_xub02
from a, b, c
where a.tc_xub01 = b.tc_xud01 and b.tc_xud05 = c.tc_xua03
group by a.tc_xub01,b.tc_xud05
a.tc_xub01,max(a.tc_xub02) tc_xub02
from
a,b,c
where
a.tc_xub01 = b.tc_xud01 and b.tc_xud05 = c.tc_xua03
group by
a.tc_xub01
园丁和小F SQL很强悍咯
只要是这方面的每次必回咯
from a join b on a.tc_xub01=b.tc_xud01
join c on c.tc_xua03=b.tc_xud05
group by b.tc_xud05
select b.tc_xud05,max(b.tc_xud05)
from a,b,c
where a.tc_xub01 = b.tc_xud01 and b.tc_xud05 = c.tc_xua03
group by b.tc_xud05
use tempdb;
/*
create table A
(
tc_xub01 nvarchar(10) not null,
tc_xub02 date not null
);
insert into A(tc_xub01,tc_xub02)
values
('t001','20110101'),
('t002','20110214'),
('t003','20110308');create table B
(
tc_xud01 nvarchar(10) not null,
tc_xud05 nvarchar(10) not null
);
insert into B(tc_xud01,tc_xud05)
values
('t001','cl001'),
('t001','cl002'),
('t002','cl001'),
('t003','cl001');create table C
(
tc_xua03 nvarchar(10) not null
);
insert into C(tc_xua03)
values
('cl001'),('cl002'),('cl003'),('cl004');
*/
select B.tc_xud05,MAX(A.tc_xub02) as 最大日期
from B
join C on b.tc_xud05 = c.tc_xua03
join A on a.tc_xub01 = b.tc_xud01
group by B.tc_xud05;