DataTable没有SQL功能强大,当遇到distinct使用时,还需要重写类,在
还是使用了SQL语句:
select count(expr1) as expr1
from (select distinct 列名 as expr1 from 表 )
derivedtbl
如:3个表联合查询中统计一个列中不同行的个数
3个student表与card表,还有DownloadData表,之间关系为
student.id=card.studentid ,card.cardno=DownloadData.cardno
联合查询还有个时间段的判断
student.classid是班级编号.
SELECT COUNT(studentname) AS Expr1
FROM (SELECT DISTINCT
CONVERT(varchar(12), recordtime, 111) AS [Time], studentname
FROM (SELECT student.studentname, recordtime
FROM Student INNER JOIN
Card ON Student.ID = Card.StudentID INNER JOIN
DownloadData ON
Card.CardNo = DownloadData.CardNo
WHERE (Student.ClassID = '6') AND
DownloadData.RecordTime > '2005 - 12 - 1 00 : 00 : 00 ') AND
(DownloadData.RecordTime < '2005 - 12 - 30 23 : 00 : 00'))
DERIVEDTBL) DERIVEDTBL
FROM (SELECT DISTINCT
CONVERT(varchar(12), recordtime, 111) AS [Time], studentname
FROM (SELECT student.studentname, recordtime
FROM Student INNER JOIN
Card ON Student.ID = Card.StudentID INNER JOIN
DownloadData ON
Card.CardNo = DownloadData.CardNo
WHERE (Student.ClassID = '6') AND
DownloadData.RecordTime > '2005 - 12 - 1 00 : 00 : 00 ') AND
(DownloadData.RecordTime < '2005 - 12 - 30 23 : 00 : 00'))
DERIVEDTBL) DERIVEDTBL
其中有段代码有注意
CONVERT(varchar(12), recordtime, 111) AS [Time], studentname
convert函数,它可以将一种数据类型的表达式转换为另一种数据类型的表达式.此处我们先将数据库内的datetime类型转换为char类型,这样在查询时数据库得到参数后先自动将数据库内的信息转换为yyyy-mm-dd格式,也就是取年月日,这样就可以取一个时间段内,一个班级的刷卡总人数了
好久没写BLOG,临时写个刚刚想的,不登大雅啊.:(