Ⅰ 復雜的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()))) --上邊兩者相減就是去年的今天是去年本月的第幾周