Ⅰ 复杂的sql语句
描述有点不具体,这个是把结果以列的形式展示:
selectid,name,casewhentime1>time2then'错误一'endastype1,
casewhentime1>time3then'错误二'endastype2,
casewhentime4>time5then'错误三'endastype3
fromtest
wherename='小明'
---结果集放在一起
selectid,name,casewhentime1>time2then'错误一'end+
casewhentime1>time3then'错误二'end+
casewhentime4>time5then'错误三'endasResult
fromtest
wherename='小明'
Ⅱ 求一个复杂的SQL语句
-- =============================================
-- Author: <Rocky,f_split>
-- Create date: <2010.11.4>
-- Description: <将传入的参数以字符分割,并返回表格形式的参数所分割出来的值>
--select * from f_split('1,2,3,4,5,6',',')
-- =============================================
create function f_split(@SourceSql varchar(8000),@StrSeprate varchar(10))
returns @temp table(a varchar(100))
as
begin
declare @i int
set @SourceSql=rtrim(ltrim(@SourceSql)) --去掉字符中的空格
set @i=charindex(@StrSeprate,@SourceSql) --找分割符在字符中的位置
while @i>=1
begin
insert @temp values(left(@SourceSql,@i-1))
set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)
set @i=charindex(@StrSeprate,@SourceSql)
end
if @SourceSql<>''
insert @temp values(@SourceSql)
return
end
GO
IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME='test_4')
DROP TABLE test_4
GO
create table test_4
(
id varchar(20) not null,
value varchar(100) not null
)
insert into test_4 values('1008','A1|A2|A3')
insert into test_4 values('1009','B1|B2|B3|B4')
DECLARE @COUNT INT,@INDEX INT,@ID VARCHAR(20),@VALUE VARCHAR(100)
if object_id(N'[tempdb].dbo.[#TEMP_TEST_4_1]')>0
DROP TABLE #TEMP_TEST_4_1
if object_id(N'[tempdb].dbo.[#TEMP_TEST_4_2]')>0
DROP TABLE #TEMP_TEST_4_2
SELECT * INTO #TEMP_TEST_4_2 FROM test_4 WHERE 1=2
SELECT IDENTITY(INT,1,1) AS IDX ,* INTO #TEMP_TEST_4_1 FROM test_4
SELECT @COUNT=COUNT(*) FROM #TEMP_TEST_4_1
SET @INDEX=1
WHILE (@INDEX<=@COUNT)
BEGIN
SELECT @ID=ID,@VALUE=VALUE FROM #TEMP_TEST_4_1 WHERE IDX=@INDEX
INSERT INTO #TEMP_TEST_4_2 SELECT @ID,* FROM F_SPLIT(@VALUE,'|')
SET @INDEX=@INDEX+1
END
SELECT * FROM #TEMP_TEST_4_2
以上代码经过测试,完全能满足你的需求,请采纳,谢谢!
Ⅲ 一段很复杂很复杂的SQL语句。。求大神解释。。。
Select
*
From
(
Select
*
From
(
Select
Number
,ChineseName
,tb1.DepartmentCode
,DepartmentName
,ShiftId
,ShiftName
,Degree
,AskForLeaveType
,WorkOnDate
,OnDutyType
,StartTime
,EndTime
,AOnDuty
,AOffDuty
,Allday
,tb1.SumTotal
,'Error'=
Case
WhenDegree=1ANDStartTime<=AOnDutyANDEndTime<=AOnDutyANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenDegree=1ANDStartTime>=AOffDutyANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenDegree=1ANDStartTime>=WorkOnDate+''+'12:30'ANDEndTime<=WorkOnDate+''+'13:30'ANDCharindex('常白班',ShiftName)>0ANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenDegree=2ANDStartTime<=AOnDutyANDEndTime<=AOnDutyANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenDegree=2ANDStartTime>=AOffDutyANDEndTime<=BOnDutyANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenDegree=2ANDStartTime>=BOffDutyANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenDegree=3ANDStartTime<=AOnDutyANDEndTime<=AOnDutyANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenDegree=3ANDStartTime>=AOffDutyANDEndTime<=BOnDutyANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenDegree=3ANDStartTime>=BOffDutyANDEndTime<=COnDutyANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenDegree=3ANDStartTime>=COffDutyANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenShiftId=0ANDLeft(AskForLeaveType,1)='0'Then0
WhenShiftId>0ANDLeft(AskForLeaveType,1)<>'0'ANDLeft(OnDutyType,1)=0Then0
WhenShiftId>0ANDLeft(AskForLeaveType,1)<>'0'ANDLeft(OnDutyType,1)=2ANDAskForLeaveType='出差假'Then0
WhenShiftId>0ANDLeft(AskForLeaveType,1)<>'0'ANDLeft(OnDutyType,1)=2ANDAskForLeaveType='产假'Then0
WhenShiftId=0ANDLeft(AskForLeaveType,1)<>'0'ANDConvert(varchar(10),StartTime,108)='00:00:00'ANDConvert(varchar(10),EndTime,108)='00:30:00'Then0
Else
1
End
From
(
Select
Number
,ChineseName
,DepartmentName
,d.DepartmentCode
,p.ShiftId
,p.ShiftName
,Degree
,AskForLeaveType
,Convert(varchar(10),WorkOnDate,120)as'WorkOnDate'
,StartTime
,EndTime
,AllDay
,SumTotal
,CaseWhenRight(Convert(Varchar(10),WorkOnDate,120),5)IN(selectRight(Convert(Varchar(10),HolidayDate,120),5)FromAttendanceHoliday)THEN'2-公众假上班'ELSE'0-平时上班'ENDAS'OnDutyType'
,CaseWhenLeft(FristBrushDate,1)='1'ANDConvert(varchar(10),AOffDuty,120)>Convert(varchar(10),AOnDuty,120)ThenCAST(Convert(Varchar(11),Dateadd(dd,-1,OnDutyDate),120)+Convert(Varchar(5),AOnDuty,108)ASDateTime)ELSECAST(Convert(Varchar(11),OnDutyDate,120)+Convert(Varchar(5),AOnDuty,108)ASDateTime)EndAS'AOnDuty'
,CaseWhenLeft(FristBrushDate,1)='0'ANDConvert(varchar(10),AOnDuty,120)<Convert(varchar(10),AOffDuty,120)ThenCAST(Convert(Varchar(11),Dateadd(dd,1,OnDutyDate),120)+Convert(Varchar(5),AOffDuty,108)ASDateTime)ELSECAST(Convert(Varchar(11),OnDutyDate,120)+Convert(Varchar(5),AOffDuty,108)ASDateTime)EndAS'AOffDuty'
,CAST(Convert(Varchar(11),OnDutyDate,120)+Convert(Varchar(5),BOnDuty,108)ASDateTime)AS'BOnDuty'
,CAST(Convert(Varchar(11),OnDutyDate,120)+Convert(Varchar(5),BOffDuty,108)ASDateTime)AS'BOffDuty'
,CAST(Convert(Varchar(11),OnDutyDate,120)+Convert(Varchar(5),COnDuty,108)ASDateTime)AS'COnDuty'
,CAST(Convert(Varchar(11),OnDutyDate,120)+Convert(Varchar(5),COffDuty,108)ASDateTime)AS'COffDuty'
From
.DepartmentCode=d.DepartmentCode
.Employeeid=a.Employeeid
INNERJOINAttendanceSpellpona.Employeeid=p.Employeeidanda.WorkOndate=p.OnDutyDate
LEFTJOINAttendanceShiftssonp.Shiftid=s.Shiftid
)tb1
)tb2WhereError=1
UNIONALL
Select
Number
,ChineseName
,DepartmentCode
,DepartmentName
,ShiftId
,ShiftName
,Degree
,AskForLeaveType
,WorkOnDate
,CaseWhenRight(Convert(Varchar(10),WorkOnDate,120),5)IN(selectRight(Convert(Varchar(10),HolidayDate,120),5)fromAttendanceHoliday)THEN'2-公众假上班'ELSE'0-平时上班'ENDAS'OnDutyType'
,StartTime
,EndTime
,AOnDuty
,AOffDuty
,Allday
,SumTotal
,Error
From
(
Select
Number
,ChineseName
,d.DepartmentCode
,DepartmentName
,p.ShiftId
,p.ShiftName
,Degree
,AskForLeaveType
,WorkOnDate
,StartTime
,EndTime
,Sumtotal
,NULLAS'AOnDuty'
,NULLAS'AOffDuty'
,AllDay
,1AS'Error'
From
AttendanceAskForLeavea
LEFTJOINEmployeeseONa.Employeeid=e.Employeeid
LEFTJOINDepartmentsdONe.DepartmentCode=d.DepartmentCode
LEFTJOINAttendanceSpellpONa.Employeeid=p.EmployeeidANDa.WorkOnDate=p.OnDutyDate
LEFTJOINAttendanceShiftssONp.ShiftId=s.ShiftId
Where
Left(IncumbencyStatus,1)<>1
Groupby
Number,ChineseName,d.DepartmentCode,DepartmentName,p.ShiftId,p.ShiftName,Degree,AskForLeaveType,AllDay,WorkOnDate,StartTime,EndTime,Sumtotal
having
Count(1)>1
)tb3
)tb4
对你的代码格式化后发现,还缺少一些语句,你是否粘贴完整了?
读完SQL语句后发现,其实是一个考勤核算的查询语句,是两个大的SQL查询Union ALL的结果
Ⅳ 求一较复杂的SQL
select a.LogID,a.Name,a.成功总数,b.失败总数 from
(select LogID,name,count(*) 成功总数 from table_name where SuccessFlag=1 group by LogID,name) a,
(select LogID,name,count(*) 失败总数 from table_name where SuccessFlag=0 LogID,name) b
where a.LogID=b.LogID
Ⅳ 复杂的SQL 语句
select 职工号,姓名,性别,出生日期 into infor_da from 表 where 婚姻状况='已婚' order by 出生日期 desc
exec master..xp_cmdshell 'bcp 数据库名..infor_da out c:\ma_da.txt -c -t'
也可以这样,直接导出结果,不用放入临时表
exec master..xp_cmdshell 'bcp "select 职工号,姓名,性别,出生日期 into infor_da from 数据库名..表 where 婚姻状况=''已婚'' order by 出生日期 desc" queryout c:\ma_da.txt -c -t'
楼上的方式太不前卫了,还需要开始,运行,输入cmd,用我的方式一劳永逸
Ⅵ 复杂sql语句查询
selectid,name,isnull((selectSUM(金额)fromB表whereid=A表.idandbno=20),0)正常消费,isnull((selectSUM(金额)fromB表whereid=A表.idandbno=30),0)充值,(selecttop1余额fromB表whereid=A表.idorderby时间desc)余额,(selectMAX(时间)fromB表whereid=A表.id)时间
fromA表
这样看一下行不行
Ⅶ 实现几条比较复杂的SQL语句
未经验证,一起探讨
1:
select a.S# from SC a,SC b where a.S#=b.S# and a.C#=b.C# and
b.C#='002' and a.C#='001' and a.score>b.score
group by S#
3:
Select *
FROM SC A
Where EXISTS
(Select COUNT(1)
FROM orders
Where C# = A.C# AND score < A.score
HAVING COUNT(1) < 3)
ORDER BY score DESC
4:
update SC set score=d.avgscore
from
(select a.C#,avg(a.score) as avgscore
from SC a,Teacher b,Course c
where a.C#=c.C# and
b.T#=c.T# and
b.Tname='叶平'
group by a.C#) d
where C#=d.C#
5:
select b.S#,b.Sname from Student b,
(select S# from SC a
where exists
(select * from sc where S#='1001'
and C#=a.C#)
group by S#) c
where b.S#=c.S#
6:
delete from Teacher
truncate table Teacher
Ⅷ 复杂sql语句
你试试这样看行不行:select S_ID,convert(char(10),F_Time,20) as '日期',count(S_ID) as '访问量',
count(S_ID)*S_Price as '收入'
from 表 A,外键表 B
where A.S_ID=B.S_ID and convert(char(10),F_Time,20) between '开始日期' and '结束日期'
group by S_ID,convert(char(10),F_Time,20)
Ⅸ 比较复杂的sql语句
如果你的是ACCESS数据库或者MS SQL数据库,可以使用视图来实现,做起来很容易的。你可以试一下。
以下介绍主要方法:
INNER JOIN 运算
组合两个表中的记录,只要在公共字段之中有相符的值。
语法
FROM table1 INNER JOIN table2 ON table1.field1 compopr table2.field2
INNER JOIN 运算可分为以下几个部分:
部分 说明
table1, table2 记录被组合的表的名称。
field1, field2 被联接的字段的名称。若它们不是由数字构成的,则这些字段必须为相同的数据类型并包含同类数据,但它们无须具有相同的名称。
compopr 任何的关系比较运算子:"=," "<," ">," "<=," ">=," 或 "<>."
说明
可以在 FROM 子句中使用INNER JOIN运算。.这是最普通的联接类型。只要在这两个表的公共字段之中有相符值,内部联接将组合两个表中的记录。
可以使用 INNER JOIN 与部门表及员工表选择每一个部门中的全部员工。反之,可以使用 LEFT JOIN或 RIGHT JOIN运算创建 outer join,从而选择所有部门(即使有些并没有员工)或所有员工(即使有些尚未分配到部门)。
若试图联接包含 Memo或 OLE Object数据的字段,会导致错误。
可以联接任何两个相同类型的数值字段。例如,可以联接 AutoNumber和 Long字段,因为它们类型相似。但不能联接 Single 和 Double 类型的字段。
下列示例显示如何在类标识符字段联接类表及产品表:
SELECT CategoryName, ProctName
FROM Categories INNER JOIN Procts
ON Categories.CategoryID = Procts.CategoryID;
在上面的示例中,类标识符是已被联接的字段,但是它并不包含在查询输出中,因它并非被包含在 SELECT 语句之中。在这个示例中,若要包含联接字段,将字段名包含在 SELECT 语句中, Categories.CategoryID.
也可以使用下列语法,在一个 JOIN 语句中链接多个 ON 子句:
SELECT fields
FROM table1 INNER JOIN table2
ON table1.field1 compopr table2.field1 AND
ON table1.field2 compopr table2.field2) OR
ON table1.field3 compopr table2.field3)];
也可以使用下列语法,嵌套 JOIN 语句:
SELECT fields
FROM table1 INNER JOIN
(table2 INNER JOIN [( ]table3
[INNER JOIN [( ]tablex [INNER JOIN ...)]
ON table3.field3 compopr tablex.fieldx)]
ON table2.field2 compopr table3.field3)
ON table1.field1 compopr table2.field2;
在一个 INNER JOIN 之中,可以嵌套 LEFT JOIN 或 RIGHT JOIN,但是在 LEFT JOIN 或 RIGHT JOIN 中不能嵌套 INNER JOIN。
Ⅹ 复杂的sql语句怎么写
1、select * from 表 where year(列名) = year(getdate())-1 and month(列名)= 3 --这个比较容易理解,就不解释了
2、select datepart(wk,dateadd(year,-1,getdate())) -- 去年的今天是去年的第几周
select datepart(wk,dateadd(day,1-day(dateadd(year,-1,getdate())),dateadd(year,-1,getdate()))) -- 去年的本月的1号是去年的第几周
select datepart(wk,dateadd(year,-1,getdate())) - datepart(wk,dateadd(day,1-day(dateadd(year,-1,getdate())),dateadd(year,-1,getdate()))) --上边两者相减就是去年的今天是去年本月的第几周