Top
回复人: seafarer777(还是离爱情远点......) ( ) 信誉:95 2003-3-19 10:21:04 得分:0
收藏,^_^
Top
回复人: csdntoll(低调惯了) ( ) 信誉:147 2003-3-19 10:51:59 得分:0
cpp2017(长安不见使人愁)这么长一句,少见,能否介绍介绍它的功力?^_^
SELECT DISTINCT TSD.Time_Sheet_Dtl_Record_No,TSD.User_Record_No,TP.Period_Start_Date ,TP.Period_End_Date INTO #temp FROM Time_Sheet_Details TSD, Time_Sheet_Period TP ,User_Group_User_Relationship UGUR,User_Group_Master UGM ,User_Data_Access_Right UDAR WHERE TSD.status ='TS_WFMGRA' AND DATEDIFF(dd,TP.period_start_date,TSD.work_Date)>=0 AND DATEDIFF(dd,TP.period_end_date,TSD.work_Date)<=0 AND UGUR.User_Record_No = TSD.User_Record_No AND UGM.User_Group_Record_No=UGUR.User_Group_Record_No AND UGM.User_Group_Name IN('Technician','Engineer') AND UDAR.User_Record_No = TSD.User_Record_No AND UDAR.Division_Record_No IN(1) SELECT DISTINCT A.User_Record_No, B.Staff_No, B.Full_Name,B.Job_Title, SUM(working_hour) AS Working_Hours,SUM(ot) AS OT_HOURS, C.Period_Start_Date,C.Period_End_Date INTO #temp2 FROM Time_Sheet_Details A INNER JOIN User_Master B ON B.User_Record_No= A.User_Record_No INNER JOIN #temp C ON C.Time_Sheet_Dtl_Record_No = A.Time_Sheet_Dtl_Record_No GROUP BY A.User_Record_No, B.Staff_No,B.Full_Name,B.Job_Title, C.Period_Start_Date,C.Period_End_Date HAVING COUNT(*) = DATEDIFF(d,C.Period_Start_Date,C.Period_End_Date)+1 Select A.User_Record_No,A.Staff_No,A.Full_Name,A.Job_Title ,A.Working_Hours,A.OT_HOURS,A.Period_Start_Date,A.Period_End_Date ,DM.Division_Code INTO #temp3 FROM #temp2 AS A INNER JOIN User_Data_Access_Right UDAR ON UDAR.User_Record_No = A.User_Record_No INNER JOIN Division_Master DM ON DM.Division_Record_No = UDAR.Division_Record_No SELECT * From #temp3 order by 1,7,8; Select Count(Distinct User_Record_No+Period_Start_Date+Period_End_Date) From #temp3 DROP TABLE #temp ,#temp2,#temp3
Top
回复人: yonghengdizhen(刹那←→永恒) ( ) 信誉:112 2003-3-19 11:14:05 得分:0
我认为上面的语句在实现需求时并不是最简,效率最高的语句.
Top
回复人: yonghengdizhen(刹那←→永恒) ( ) 信誉:112 2003-3-19 11:45:29 得分:0
各种查询技巧结合才能写出最高效的查询
在SQL中使用DISTINCT是代价比较高的查询方式
Top
回复人: oldsky(九指神丐) ( ) 信誉:54 2003-3-19 13:13:09 得分:0
微软不公布SQLserver2000中的函数:
print pwdcompare('helloworld', pwdencrypt('helloworld'))
print pwdcompare('hello', pwdencrypt('world'))
Top
回复人: funboy88(司令) ( ) 信誉:121 2003-3-19 13:20:20 得分:0
pwdencrypt
密码加密?
有些什么作用,不明白
随机取出10条数据
select top 10 * from tablename order by newid()
Top
回复人: mzcih(小马过河) ( ) 信誉:110 2003-3-19 13:25:31 得分:0
看君一张贴,胜看一天书。
Top
回复人: funboy88(司令) ( ) 信誉:121 2003-3-19 13:26:48 得分:5
功能:
type vender pcs
电脑 A1
电脑 A1
光盘 B2
光盘 A2
手机 B3
手机 C3
select type,sum(case vender when 'A' then pcs else 0 end),sum(case vender when 'C' then pcs else 0 end),sum(case vender when 'B' then pcs else 0 end) FROM tablename group by type
Top
回复人: funboy88(司令) ( ) 信誉:121 2003-3-19 13:30:27 得分:0
FUNCTION:DELELTE REPEAT RECORDS
DELETEfrom tablename where id not in (select max(id) from tablename group by col1,col2,...)
Top
回复人: chonboy(一只来自南方的羊) ( ) 信誉:100 2003-3-19 14:32:56 得分:0
Microsoft SQL Server是如何加密口令的?未公开的加密函数?
如果对MSSQL的用户信息有兴趣的,可能会发现master.dbo.sysxlogins里面存放着用户的口令,可是呢,password字段如果不是null就是一堆看不懂的binary,这个口令是怎么加密的呢?
其实只要仔细看看master.dbo.sp_addlogin就知道了,MSSQL的sp都可以看到代码,真是不错。
让我们来看看它是怎么做的,注意这一行select @passwd = pwdencrypt(@passwd),这个时后@passwd就被加密了,让我们也来试一下
DECLARE @ClearPWD varchar(255)
DECLARE @EncryptedPWD varbinary(255)
SELECT @ClearPWD = 'test'
SELECT @EncryptedPWD = CONVERT(varbinary(255), pwdencrypt(@ClearPWD))
SELECT @EncryptedPWD
看上去不错,确实被加密了,可是我怎么还原呢?
呵呵,这就没戏了,口令加密都是单向的,用加密后的密文来比较就可以了。
继续看看其它用户相关的sp,可以发现master.dbo.sp_password里面有口令比较的内容。
pwdcompare(@old, password, (CASE WHEN xstatus&2048 = 2048 THEN 1 ELSE 0 END))
不用去理会xstatus,这是一个状态掩码,一般我们用的时候就直接用0就可以了
DECLARE @ClearPWD varchar(255)
DECLARE @EncryptedPWD varbinary(255)
SELECT @ClearPWD = 'test'
SELECT @EncryptedPWD = CONVERT(varbinary(255), pwdencrypt(@ClearPWD))
SELECT pwdcompare(@ClearPWD, @EncryptedPWD, 0)
SELECT pwdcompare('ErrorPassword', @EncryptedPWD, 0)
这样我们就可以使用这两个函数来加密自己的密码了,怎么样,还不错吧?
引用自
http://www.bgchina.com/daily/bd_1/
Top
回复人: csdntoll(低调惯了) ( ) 信誉:147 2003-3-20 10:00:45 得分:0
SELECT语法:(基本)
SELECT [DISTINCT]
(column [{, column } ] )| *
FROM table [ { , table} ]
[ORDER BY column [ASC] | [DESC
[{ , column [ASC] | [DESC } ] ]
WHERE predicate [ { logical-connector predicate } ];
------------------------------------------------------
INSERT语法:
INSERT INTO table
[(column { ,column})]
VALUES
(columnvalue [{,columnvalue}]);
------------------------------------------------------
UPDATE语法:
UPDATE table
SET column = value [{, column = value}]
[ WHERE predicate [ { logical-connector predicate}]];
------------------------------------------------------
DELETE语法:
DELETE FROM table
[WHERE predicate [ { logical-connector predicate} ] ];
------------------------------------------------------
Top
回复人: liuzxit(每日晕十几次) ( ) 信誉:115 2003-3-20 10:13:05 得分:0
想把我那兩頁長的SELECT語句拿出來了﹐不過沒興趣
Top
回复人: lgj1012(oοО○≮国产超人≯○Оοo) ( ) 信誉:99 2003-3-20 10:13:41 得分:0
MARK MARK
Top
回复人: csdntoll(低调惯了) ( ) 信誉:147 2003-3-20 10:41:56 得分:0
常识补充
统计函数:
AVG(字段名) 得出一个表格栏平均值
COUNT(*|字段名) 对数据行数的统计或对某一栏有值的数据行数统计
MAX(字段名) 取得一个表格栏最大的值
MIN(字段名) 取得一个表格栏最小的值
SUM(字段名) 把数据栏的值相加
eg:
sql="select sum(字段名) as 别名 from 数据表 where 条件表达式"
Top
回复人: carl__yao(頭目) ( ) 信誉:95 2003-3-20 10:50:06 得分:0
select top 0 * into b from a
Top
回复人: flashasp(flashasp) ( ) 信誉:95 2003-3-20 13:05:01 得分:0
select name from sysobjects where type='U''列出数据库里所有的表名
select name from syscolumns where id=object_id('TableName')'列出表里的所有的
Top
回复人: flashasp(flashasp) ( ) 信誉:95 2003-3-20 13:05:47 得分:0
select name from sysobjects where type='U''列出数据库里所有的表名
select name from syscolumns where id=object_id('TableName')'列出表里的所有的字段名
Top
回复人: annkie(无声的雨) ( ) 信誉:113 2003-3-20 13:27:22 得分:0
帮忙看看这个帖子,解决了我想贴在这里应该是可以的 :)
http://expert.csdn.net/Expert/topic/1554/1554516.xml?temp=.6041071
Top
回复人: flyinto(孤独求败) ( ) 信誉:100 2003-3-20 14:14:42 得分:0
MARK
Top
回复人: yonghengdizhen(刹那←→永恒) ( ) 信誉:112 2003-3-20 14:15:00 得分:0
source data
kzx4dm xbdm jylsfsdm ...
112 10
112 10
122 10
121 20
121 20
destination:
kzx4dmbys_count yjs_count jy_ratio
112 21.00
123 11/3
SELECT DISTINCT kzx4dm,(SELECT COUNT(jylsfsdm) FROM tablename WHERE kzx4dm=TA.kzx4dm) AS bys_count,(SELECT COUNT(jylsfsdm) FROM tablename WHERE kzx4dm=TA.kzx4dm WHERE jylsfsdm=10) AS yjs_count,yjs_count/bys_count AS jy_ratio
FROM tablename AS TA
Top
回复人: yonghengdizhen(刹那←→永恒) ( ) 信誉:112 2003-3-20 14:18:35 得分:10
to annkie(活着便精彩)
看看上面的语句是否解决了问题?
纠正一下,应该是
SELECT DISTINCT kzx4dm,(SELECT COUNT(jylsfsdm) FROM tablename WHERE kzx4dm=TA.kzx4dm) AS bys_count,(SELECT COUNT(jylsfsdm) FROM tablename WHERE kzx4dm=TA.kzx4dm AND jylsfsdm=10) AS yjs_count,yjs_count/bys_count AS jy_ratio
FROM tablename AS TA
Top
回复人: GageCSDN(稻草人) ( ) 信誉:100 2003-3-20 17:30:45 得分:0
seeing........
Top
回复人: allserver(server) ( ) 信誉:100 2003-3-20 19:13:25 得分:0
8错
Top
回复人: kirc(无聊中...) ( ) 信誉:100 2003-3-20 19:16:32 得分:0
关注....
Top
回复人: annkie(无声的雨) ( ) 信誉:113 2003-3-20 19:28:23 得分:0
To yonghengdizhen:
thank you.
正在研究此问题中.
yjs_count/bys_count AS jy_ratio 这句无法通过检查,不过可以把前面的东西拿倒这里来搞定,不使用字段名.
你的sql语句是对的.
Top
回复人: flyycyu(fly) ( ) 信誉:100 2003-3-21 9:36:40 得分:0
gz
Top
回复人: csdntoll(低调惯了) ( ) 信誉:147 2003-3-21 10:02:00 得分:0
我决定:把200分中的180分,奖给贴出最精妙的SQL的高手!
Top
回复人: yonghengdizhen(刹那←→永恒) ( ) 信誉:112 2003-3-21 11:00:52 得分:0
二维表 T(F1,F2,F3,F4,F5,F6,F7) 表示如下关系:
学生ID学生姓名课程ID课程名称 成绩教师ID教师姓名
S3王五K4政治53 T4 赵老师
S1张三K1数学61 T1 张老师
S2李四K3英语88 T3 李老师
S1张三K4政治77 T4 赵老师
S2李四K4政治67 T5 周老师
S3王五K2语文90 T2 王老师
S3王五K1数学55 T1 张老师
S1张三K2语文81 T2 王老师
S4赵六K2语文59 T1 王老师
S1张三K3英语37 T3 李老师
S2李四K1数学81 T1 张老师
请以一句 T-SQL (Ms SQL Server) 或 Jet SQL (Ms Access) 在 原表 T 基础上作答
1.如果 T 表还有一字段 F0 数据类型为自动增量整型(唯一,不会重复),
而且 T 表中含有除 F0 字段外,请删除其它字段完全相同的重复多余的脏记录数据:
delete from twhere f0 in(select max(f0) from t group by f1,f2,f3,f4,f5,f6,f7 having count(f0)>1)
2.列印各科成绩最高和最低的记录: (就是各门课程的最高、最低分的学生和老师)
课程ID,课程名称,最高分,学生ID,学生姓名,教师ID,教师姓名,最低分,学生ID,学生姓名,教师ID,教师姓名
select tb.f4,tb.f3,tb1.f5,tb.f1,tb.f2,tb.f6,tb.f7,tb2.f5,tb2.f1,tb2.f2,tb2.f6,tb2.f7 from t tb where
f5=(select max(f5) from t where t.f4=tb.f4)
join select f2,f7 from t tb2 where
f5=(select min(f5) from t where t.f4=tb2.f4)
on tb.f4=tb2.f4
先完成一个,想想在做下一个.
3.按成绩从高到低顺序,列印所有学生四门(数学,语文,英语,政治)课程成绩: (就是每个学生的四门课程的成绩单)
学生ID,学生姓名,数学,语文,英语,政治,有效课程数,有效平均分
(注: 有效课程即在 T 表中有该学生的成绩记录,如不明白可不列印"有效课程数"和"有效平均分")
select tb1.f1,tb1.f2,count(tb1.f5) as scores,sum(tb1.f5) as scoresum, avg(tb1.f5) AS average,
tb2.f5,tb3.f5,tb4.f5,tb5.f5
from t as tb1
left join t as tb2
on tb1.f0=tb2.f0 and tb2.f3=k4
left join t as tb3
on tb1.f0=tb3.f0 and tb3.f3=k3
left join t as tb4
on tb1.f0=tb4.f0 and tb4.f3=k2
left join t as tb5
on tb1.f0=tb5.f0 and tb5.f3=k1
grout by tb1.f2 order by tb1.scoresum desc
4.按各科不及格率的百分数从低到高和平均成绩从高到低顺序,统计并列印各科平均成绩和不及格率的百分数(用"N行"表示): (就是分析哪门课程难)
课程ID,课程名称,平均成绩,及格百分数
selectf3,f4, (select count(f1) from t where t.f4=tb.f4 and f5<60)/(select count(f1) from t where t.f4=tb.f4) as failper,((select sum(f5) from t where t.f4=tb.f4)/ (select count(f5) from t where t.f4=tb.f4)) as averagescore
from t tb order by failper asc, as averagescore desc
Top
回复人: wenhao676(zzc) ( ) 信誉:93 2003-3-21 11:01:50 得分:0
/****** Object:Stored Procedure dbo.dt_checkoutobjectScript Date: 2003-3-12 9:25:26 ******/
create proc dbo.dt_checkoutobject
@chObjectTypechar(4),
@vchObjectName varchar(255),
@vchCommentvarchar(255),
@vchLoginNamevarchar(255),
@vchPassword varchar(255),
@iVCSFlags int = 0,
@iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */
as
set nocount on
declare @iReturn int
declare @iObjectId int
select @iObjectId =0
declare @VSSGUID varchar(100)
select @VSSGUID = 'SQLVersionControl.VCS_SQL'
declare @iReturnValue int
select @iReturnValue = 0
declare @vchTempText varchar(255)
/* this is for our strings */
declare @iStreamObjectId int
select @iStreamObjectId = 0
declare @iPropertyObjectId int
select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID')
declare @vchProjectName varchar(255)
declare @vchSourceSafeINI varchar(255)
declare @vchServerNamevarchar(255)
declare @vchDatabaseNamevarchar(255)
exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT
exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT
exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerNameOUT
exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseNameOUT
if @chObjectType = 'PROC'
begin
/* Procedure Can have up to three streams
Drop Stream, Create Stream, GRANT stream */
exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT
if @iReturn <> 0 GOTO E_OAError
exec @iReturn = sp_OAMethod @iObjectId,
'CheckOut_StoredProcedure',
NULL,
@sProjectName = @vchProjectName,
@sSourceSafeINI = @vchSourceSafeINI,
@sObjectName = @vchObjectName,
@sServerName = @vchServerName,
@sDatabaseName = @vchDatabaseName,
@sComment = @vchComment,
@sLoginName = @vchLoginName,
@sPassword = @vchPassword,
@iVCSFlags = @iVCSFlags,
@iActionFlag = @iActionFlag
if @iReturn <> 0 GOTO E_OAError
exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT
if @iReturn <> 0 GOTO E_OAError
create table #commenttext (id int identity, sourcecode varchar(255))
select @vchTempText = 'STUB'
while @vchTempText IS NOT NULL
begin
exec @iReturn = sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT
if @iReturn <> 0 GOTO E_OAError
if (@vchTempText IS NOT NULL) insert into #commenttext (sourcecode) select @vchTempText
end
select 'VCS'=sourcecode from #commenttext order by id
select 'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid
end
CleanUp:
return
E_OAError:
exec dbo.dt_displayoaerror @iObjectId, @iReturn
GOTO CleanUp
GO
看的我直晕~!
Top
回复人: yonghengdizhen(刹那←→永恒) ( ) 信誉:112 2003-3-21 11:17:12 得分:0
1.如果 T 表还有一字段 F0 数据类型为自动增量整型(唯一,不会重复),
而且 T 表中含有除 F0 字段外,请删除其它字段完全相同的重复多余的脏记录数据:
delete from twhere f0 in(select max(f0) from t group by f1,f2,f3,f4,f5,f6,f7 having count(f0)>1)
上面这个sql有问题
正确的如下
DELETE Legal_Dispute_Lawyer WHERE Lawyer_Record_No IN(SELECT Lawyer_Record_No FROM Legal_Dispute_Lawyer LDL WHERE Lawyer_Record_No(SELECT TOP 1 Lawyer_Record_No FROM Legal_Dispute_Lawyer WHERE LD=LDL.LD AND Name=LDL.Name AND Email=LDL.Email AND Phone_No=LDL.Phone_No AND Fax_No=LDL.Fax_No))
消除Legal_Dispute_Lawyer 表中除Lawyer_Record_No(自增字段)外其余数据完全相同的记录.
表结构如下
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Legal_Dispute_Lawyer]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Legal_Dispute_Lawyer]
GO
CREATE TABLE [dbo].[Legal_Dispute_Lawyer] (
[Lawyer_Record_No] [int] IDENTITY (1, 1) NOT NULL ,
[LD] [int] NOT NULL ,
[Name] [int] NOT NULL ,
[Phone_No] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
[Fax_No] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
screen.width-500)this.style.width=screen.width-500;" align=middle border=0> [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
以这种方式还可以实现组内消除重复值..
Top
回复人: yonghengdizhen(刹那←→永恒) ( ) 信誉:112 2003-3-21 11:19:48 得分:0
DELETE Legal_Dispute_Lawyer WHERE Lawyer_Record_No IN(SELECT Lawyer_Record_No FROM Legal_Dispute_Lawyer LDL WHERE Lawyer_Record_No<>(SELECT TOP 1 Lawyer_Record_No FROM Legal_Dispute_Lawyer WHERE LD=LDL.LD AND Name=LDL.Name AND Email=LDL.Email AND Phone_No=LDL.Phone_No AND Fax_No=LDL.Fax_No))
Top
回复人: ylecho(~猫猫) ( ) 信誉:100 2003-3-21 13:58:37 得分:0
select * from table;
xixi
Top
回复人: SeaSee(纵横四海) ( ) 信誉:100 2003-3-21 14:10:19 得分:0
table1
kzx4dm xbdm jylsfsdm ...
102 8
112 9
122 19
131 18
141 19
select top 3 with ties jylsfsdm from table1
结果:
kzx4dm xbdm jylsfsdm ...
122 19
141 19
131 18
112 9
说明:取出并列排行的所有记录
Top
回复人: lsqteng(阿琦) ( ) 信誉:102 2003-3-21 14:18:11 得分:0
说到语法我就来兴趣了,哈哈,请看:
SELECT statement::=
<query_experssion>
[ORDER BY {order_by_expression|Column_position[ASC|DESC]}
[,...n]]
[COMPUTE
{{AVG|COUNT|MAX|MIN|SUM}(exression)}[,...n]
[BY expression[,...n]]
]
[FOR {BROWSE |XML{RAW|AUTO|EXPLICIT}
[,XMLDATA]
[,ELEMENTS]
[,BINARY base64]
}
]
[OPTION(<query_hint>[,...n])]
<query expression>::=
{<query specification>|(<query expression>)}
[UNION[ALL]<query specification|(<query expression<)[...n]]
<query specification>::=
SELECT [ALL|DISTINCT]
[{TOP integer|TOP integer PERCENT}[WITH TIES]]
<select _list>
[INTO new_table]
[FROM{<table_source>}[,...n]]
[WHERE <search_condition>]
[GROUP BY [ALL]group_by_expression[,...n]
[WITH{CUBE|ROLLUP}]
]
[HAVING<search_condition>}
哎,行了吧,这还是select 语句的形式。要看完全的,喔,那可不得了喔。我不写了,累死了 |
|