请高手帮忙看看这几个要求怎么写SQL语句

Kav3000 2012-12-18 06:33:21
1:A表结构如下

要求一条SQL语句输出结果:


2:给定表TeacherLesson,如下数据
weekDays为星期数

要求输出如下结果


3:表CC数据如下

要求输出:



求以上三条SQL语句……

...全文
102 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kav3000 2012-12-18
  • 打赏
  • 举报
回复
膜拜两位大神! 特别是第一位写的,精简有力啊!
-晴天 2012-12-18
  • 打赏
  • 举报
回复
declare @tb table([day] int,amount decimal(10,5))
insert into @tb select 1,3.33333
union all select 2,4.22222
union all select 3,1.5555
union all select 4,9.888
select [day],(select sum(amount) from @tb where [day]<=a.[day]) amount from @tb a

declare @t table(id int,n varchar(10),weekdays int)
insert into @t select 1,'a',2
union all select 2,'a',3
union all select 3,'b',3
union all select 4,'b',4
union all select 5,'c',1
union all select 6,'c',4
union all select 7,'c',2
select n,max(case when weekdays=1 then '有课' else '' end) as 星期一,
max(case when weekdays=2 then '有课' else '' end) as 星期二,
max(case when weekdays=3 then '有课' else '' end) as 星期三,
max(case when weekdays=4 then '有课' else '' end) as 星期四,
max(case when weekdays=5 then '有课' else '' end) as 星期五
from @t group by n

declare @b table(a int,b int)
insert into @b select 1,2
union all select 1,3
union all select 1,4
union all select 2,1
union all select 2,2
union all select 3,1
union all select 4,1
union all select 5,3
union all select 5,2
select a,min(b)b from @b group by a
/*
day         amount
----------- ---------------------------------------
1           3.33333
2           7.55555
3           9.11105
4           18.99905

(4 行受影响)

n          星期一  星期二  星期三  星期四  星期五
---------- ---- ---- ---- ---- ----
a               有课   有课        
b                    有课   有课   
c          有课   有课        有课   

(3 行受影响)

a           b
----------- -----------
1           2
2           1
3           1
4           1
5           2

(5 行受影响)
*/
我腫了 2012-12-18
  • 打赏
  • 举报
回复
1:
select 
		a.Day			As 日期
		,a.Amount		As 日产
		,Sum(b.Amount)	As 累计日产 
	from A As a,A As b
	Where a.Day>=b.Day
	Group by a.Day,a.Amount

2:
Select 
		TeacherName
		,Max(Case when WeekDays=1 And IsLesson=1 Then N'有课' Else '' End) As 星期一
		,Max(Case when WeekDays=2 And IsLesson=1 Then N'有课' Else '' End) As 星期二
		,Max(Case when WeekDays=3 And IsLesson=1 Then N'有课' Else '' End) As 星期三
		,Max(Case when WeekDays=4 And IsLesson=1 Then N'有课' Else '' End) As 星期四
	From TeacherLesson
	Group by TeacherName

3:
Select 
		A
		,Min(B) As B
	from CC
	Group by A

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧