日期:2014-05-17  浏览次数:20446 次

请高手帮忙看看这几个要求怎么写SQL语句
1:A表结构如下

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

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

要求输出如下结果          


3:表CC数据如下

要求输出:



求以上三条SQL语句……

 
------解决方案--------------------
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

------解决方案--------------------
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       &nbs