作者TCH (最无价值单身汉)
看板Database
标题Re: [SQL ] 两个SQL语法要合并写在一起
时间Tue Sep 4 22:26:49 2007
※ 引述《diamondking (天剑流星)》之铭言:
: ※ 引述《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的语法不是很肯定,有错请指正
感谢...
不过这样的结果,在SQL Server执行却只有显示出现在Spell中的资料
我希望有此班级内全部人的资料,因为是 left join 所以右边会有 null,都要显示
ps:我好像试出来了,明天再PO上来
(因为是胡乱试出来的,自己现在也忘了作法 ^^|||)
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.137.101.19
※ 编辑: TCH 来自: 220.137.101.19 (09/04 22:28)