"Go" [SQL Server] group take maximum minimum value common SQL

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/xys_777/article/details/5711128

--group to take the largest and smallest common SQL
--Test environment
If object_id (' TB ') is not null drop table TB;
Go
CREATE TABLE TB (
col1 int,
col2 int,
Fcount int)
INSERT INTO TB
Select 11,20,1 UNION ALL
Select 11,22,1 UNION ALL
Select 11,23,2 UNION ALL
Select 11,24,5 UNION ALL
Select 12,39,1 UNION ALL
Select 12,40,3 UNION ALL
Select 12,38,4
Go
--Query
--1
SELECT * from TB t where fcount= (select Max (fcount) from TB where col1=t.col1)
--2
SELECT * from TB T where NOT exists (select 1 from TB where col1=t.col1 and Fcount>t.fcount)--much higher efficiency (LUI2015-5-13 notes)
-Results
/*
Col1 col2 Fcount
----------- ----------- -----------
12 38 4
11 24 5

*/

"Go" [SQL Server] group take maximum minimum value common SQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.