SQL查询每行中最大值的技巧
--------------------------------------------------------------------------
-- Author : htl258(Tony)
-- Date : 2010-04-23 08:08:36
-- Version:Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
-- Blog : http://blog.csdn.net/htl258
-- Subject: SQL查询每行中最大值的技巧
--------------------------------------------------------------------------
--> 生成测试数据表:tb
IF NOT OBJECT_ID('[tb]') IS NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb](
a SMALLINT,
b SMALLINT,
c SMALLINT,
d SMALLINT,
e SMALLINT,
f SMALLINT,
g SMALLINT
)
INSERT [tb]
SELECT 1,2,3,4,5,2,3 UNION ALL
SELECT 4,5,6,7,7,2,0 UNION ALL
SELECT 4,9,6,7,7,9,6
GO
--SELECT * from [tb]
-->SQL查询如下:
SELECT *,
(SELECT MAX(a)
from(
SELECT a UNION ALL
SELECT b UNION ALL
SELECT c UNION ALL
SELECT d UNION ALL
SELECT e UNION ALL
SELECT f UNION ALL
SELECT g
) AS t
) AS maxvalue
from tb
/*
a b c d e f g maxvalue
------ ------ ------ ------ ------ ------ ------ --------
相关文档:
1 避免无计划的全表扫描
如下情况进行全表扫描:
- 该表无索引
- 对返回的行无人和限制条件(无Where子句)
- 对于索引主列(索引的第一 ......
Use DatabaseName
--DB shrink
--获取database 空余空间, 决定是否作shrinkDB
exec [DBNAME].dbo.sp_spaceused
DBCC ShrinkDB(DBNAME)
--Log file shrink
Use DatabaseName
GO
Alter Database DatabaseName Set Recovery Simple
GO
Alter Database DatabaseName Set Recovery Full
GO
DBCC SHRINKFILE ('Log ......
用sql操作oracle的blob字段
1、查询
select utl_raw.cast_to_varchar2(dlob),id from system.t_htinfo
2、插入
insert into system.t_htinfo values ('3',utl_raw.cast_to_raw('你'));
3、更新
update system.t_htinfo set fld_value=utl_raw.cast_to_raw('1') where fh_nm=1820648 and form_index=52
......
同一表多字段同时重复记录的SQL查询及处理数
比如现在有一人员表 (表名:peosons)
若想将姓名、身份证号、住址这三个字段完全相同的记录查询出来
select p1.* from persons p1,persons p2 where p1.idp2.id and p1.cardid = p2.cardid and p1.pname = p2.pname and p1.address ......
Student(S#,Sname,Sage,Ssex) 学生表
Course(C#,Cname,T#) 课程表
SC(S#,C#,score) 成绩表
Teacher(T#,Tname) 教师表
问题:
1、查询“001”课程比“002”课程成绩高的所有学生的学号;
select a.S# from (select s#,score from SC where C#='001') a,(select s#,score
fr ......