日期:2014-05-18 浏览次数:20498 次
select b.* from (select distinct [name] from table_name) a cross apply (select top(1) * from table_name where [name] = a.[name] order by value1 desc) b
------解决方案--------------------
CREATE TABLE tb ( name VARCHAR(10), value1 VARCHAR(10), value2 VARCHAR(10), value3 VARCHAR(10) ) GO INSERT INTO tb SELECT 'A', 'A1', 'A2', 'A3' UNION SELECT 'A', 'A_1', 'A_2', 'A_3' UNION SELECT 'B', 'B1', 'B2', 'B3' GO SELECT * FROM tb AS A WHERE (SELECT COUNT(1) FROM tb WHERE A.name = name AND A.value1 > value1) < 1
------解决方案--------------------
SELECT * FROM TAB A WHERE NOT EXISTS ( SELECT 1 FROM TAB WHERE NAME = A.NAME AND (value1 < A.value1 OR value1 = A.value1 AND value12 < A.value2 OR value1 = A.value1 AND value12 = A.value2 AND value13 < A.value3) )
------解决方案--------------------
1:如果数据量多 先把T_User 的数据放到一张临时表里(insert into tb select * from tb_ where vcuserName like '%1%')
然后用这张表和其他表做关联
2:
join col..=col..
where col..
关联字段和where条件判断字段都增加索引