--------------------------------------------------- 2345王牌技术员联盟、2345王牌技术员联盟、期待与您交流!---------------------------------------------------------
1.黑马程序员之SQL学习笔记:表中有ABC三列,用SQL语句实现,当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。
select (case when a>b then a else b end),(case when b>c then b else c end) from tDate Name Score
2008-8-8 拜仁 胜
2008-8-9 奇才 胜
2008-8-9 湖人 胜
2008-8-10 拜仁 负
2008-8-8 拜仁 负
2008-8-12 奇才 胜
要求输出下面的格式:
Name 胜 负
拜仁 1 2
湖人 1 0
奇才 2 0
数据和参考答案见备注
注意:在中文字符串前加N, 比如N'胜'
select Name,
{
case Score
when N'胜' then 1
else 0
end
} as 胜,
{
case Score
when N'负' then 1
else 0
end
} as 负
from T_Scores
输出结果:
Name 胜 负
拜仁 1 0
奇才 1 0
湖人 1 0
拜仁 0 1
拜仁 0 1
奇才 1 0
select Name,
Sum{
case Score
when N'胜' then 1
else 0
end
} as 胜,
Sum{
case Score
when N'负' then 1
else 0
end
} as 负
from T_Scores
group by Name
输出结果:
Name 胜 负
拜仁 1 2
湖人 1 0
奇才 2 0
单号 金额
Rk1 10
Rk2 20
Rk3 -30
Rk4 -10
将上面的表输出为如下的格式:
单号 收入 支出
Rk1 10 0
Rk2 20 0
Rk3 0 30
Rk4 0 10
select Name,
{
case Score
when N'胜' then 1
else 0
end
} as 胜,
{
case Score
when N'负' then 1
else 0
end
} as 负
from T_Scores
SELECT FNumber,
{
CASE WHEN FAmount>0 THEN FAmount
ELSE 0
END
}AS 收入,
{
CASE WHEN FAmount<0 THEN ABS(FAmount)
ELSE 0
END)
}AS 支出
FROM SALARY_TABLE;
4.黑马程序员之SQL学习笔记:创建一张表,记录电话呼叫员的工作流水,记录呼叫员编号,对方号码,通话开始时间,通话结束时间。建表,插数据等最后都自己写SQL语句。
要求:
1. 输出所有数据中通话时间最长的5条记录; ( 提示:order by datediff )
2. 输出所有数据中拨打长途号码(对方号码以0开头)的总时长;
3. 输出本月通话总时长最多的前三个呼叫员的编号;
4. 输出本月拨打电话次数最多的前三个呼叫员的编号
5. 输出所有数据的拨号流水,并且在最后一行添加总呼叫时长
* 呼叫员编号,对方号码,通话时长
* ......
* 汇总 [市内号码总时长][长途号码总时长]
--所有数据中通话时间最长的5条记录; ( 提示:order by datediff )
select * from T_CallRecords1 order by DateDiff(second,StartDateTime,EndDateTime) Desc
--输出所有数据中拨打长途号码(对方号码以0开头)的总时长。like ,sum
select sum(datediff(second,StartDateTime,EndDateTime)) from T_CallRecords1 where TelNum like '0%'
--输出本月通话总时长最多的前三个呼叫员的编号;datediss(month...),sum,order by
--select daediff(month,convert(datetime,'2010-06-01'),convert(datetime,'2010-07-02')),datediff(month,convert(datetime,'2010-06---15'),convert(datetime,'2010-07-02'))
--select CallerNumber,TelNum,datediff(month,StartDateTime,getdate()) from T_CallRecord1
--select * from T_CallRecords1 where datediff(month,StartDateTime,getdate())=0
select top 3 CallerNumber from T_CallRecords1 where datediff(month,StartDateTime,getdate())=0 group by CallerNumber order by Sum(datediff(month,StartDateTime,getdate()) DESC
--输出本月拨打电话次数最多的前三个呼叫员的编号
select CallerNumber, count(*) from T_CallRecords1 where datediff(month,StartDateTime,getdate()) = 0 group by CallerNumber order by count(*) DESC
--汇总 [市内号码总时长][长途号码总时长]
select
sum((
case
when TelNum not like '0%' then datediff(second,StartDateTime,EndDateTime)
else 0
end
) )as 市内通话,
sum((
case
when TelNum like '0%' then datediff(second,StartDateTime,EndDateTime)
else 0
end
) )as 长途通话
from T_CallRecords1
---呼叫员编号,对方号码,通话时长
select CallerNumber,TelNum,datediff(second,StartDateTime,EndDateTime)
from T_CallRecords1
union all
select
sum((
case
when TelNum like '0%' then datediff(second,StartDateTime,EndDateTime)
else 0
end
) )as 长途通话
from T_CallRecords1
--------------------------------------------------- 2345王牌技术员联盟、2345王牌技术员联盟、期待与您交流!---------------------------------------------------------