作者diamondking (天剑流星)
看板Database
标题Re: [SQL ] 两个SQL语法要合并写在一起
时间Mon Sep 3 20:24:33 2007
※ 引述《TCH (最无价值单身汉)》之铭言:
: 我要的结果是:
: 产生一个有三栏的Table,第一栏的资料是Student表格的StuName,第二、三栏的资料
: 都是SpellSpecify表格的函数计算结果。
: Student表格内有各个班级的所有学生的基本资料。
: Spell表格内存放的应是所有学生的值日表(不一定每个学生都会出现在此表)。
: 并且,Student.StuID = Spell.SpellStuID
: 因为我正在写 .Net 的 GridView ,底下有我写的两个不完整的语法..
: 第一个语法:会显示某个班级内所有 Student 的 StuName於GridView第一栏中。
: 不完整之处:不知要怎麽安插函数,也不知要放哪个位置。
: string sqlStrTEST = "select StuName from Student ";
: sqlStrTEST += "left join Spell on Student.StuID = Spell.SpellStuID and
: YearMonth = '" + YearMonth + "'";
: sqlStrTEST += "where Student.UnitID ='" + ClassID.Substring(0, 3) + "' ";
: 第二个语法:会显示 Spell 表格中正确的函数值。
: 不完整之处:不知要怎麽显示某个班级内的学生於GridView第一栏中。
: string sqlStrTEST = "SELECT
: SpellStuName,sum(datediff(hh,StartTime,EndTime)),sum(datediff(hh,StartTime,EndTime))*100
: as Bonus ";
: sqlStrTEST += "FROM Spell ";
: sqlStrTEST += "group by SpellStuName ";
: 请位小弟解答,感激不尽..
其实有比较简单的写法:
select a.StuName,
sum(datediff(hh,b.StartTime,b.EndTime)) Hours,
sum(datediff(hh,b.StartTime,b.EndTime))*100 Bonus
from Student a left outer join Spell b on a.StuID=b.SpellStuID
where b.YearMonth=200708 and a.UnitID=123
group by a.StuName
大致上是这样,t-sql的语法不是很肯定,有错请指正
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 218.167.209.114