实际应用中经常需要统计单位时间的记录数: 一个小时: SELECT ),createdate,) AS 日期 ,DATEPART(hh,createdate) AS 小时,COUNT(*)AS 记录 --varchar(10)得到结果为日期('2016-01-01') FROM log --或者用DATE格式提取日期(VARCHAR(10)换成DATE) where createdate>=CAST(CONVERT(VARCHAR(10),createdate,120))+' 00:00:00'
SQLSERVER中统计所有表的记录数 利用系统索引表sysindexes中索引ID indid<1的行中的rows列存有该表的行数这一特点. 方法是利用隐藏未公开的系统存储过程sp_MSforeachtable ), RowCnt INT) EXEC sp_MSforeachtable 'INSERT INTO #temp SELECT ''?'', COUNT(*) FROM ?' SELECT TableName, RowCnt FROM #temp ORDER BY RowCnt
表结构 then d.name else null end) 表名, a.colorder 字段序号,a.name 字段名, ( then '√'else '' end) 标识, (case when (SELECT count(*) FROM sysobjects WHERE (name in (SELECT name FROM sysindexes WHERE (id = a.id) AND (indid in (SELECT indid FROM sysindexkeys WHERE (i
尊重原创:http://blog.csdn.net/love_java_cc/article/details/78268326 有如下一张表score: 建表语句: CREATE TABLE `score` (`id` int NOT NULL AUTO_INCREMENT ,`name` varchar(20) NOT NULL ,`category` varchar(20) NOT NULL ,`score` int NOT NULL ); 现在要去查询学生的各科成绩和总分,效果如下
select NVL(sum(case when create_date_time>=to_date('2014-11-24 00:00:00','yyyy-mm-dd hh24:mi:ss') and create_date_time<=to_date('2014-11-24 23:59:59','yyyy-mm-dd hh24:mi:ss') then amount end),0) today ,NVL(sum(case when create_date_time>=to_date(
有些时候,我们希望同时更新表中的多条记录,但更新的值不一样. declare @i int declare @j int declare @phone varchar(20) set @j=1 select @i=count(*) from LatestLocation DECLARE L_cursor CURSOR FOR SELECT Phone FROM LatestLocation OPEN L_cursor while @j<=@i begin FETCH NEXT FROM L_cu
环境:mssql ent 2k8 r2 原理:遍历所有用户表,用sp_spaceused过程分别获取每张表的行数并写入临时表,最后返回临时表 IF OBJECT_ID('tempdb..#TableRowCount','U') IS NOT NULL DROP TABLE #TableRowCount GO ,), Reserved ), Data ), Index_Size ), Unused )) GO DECLARE curCntAllTableRows CURSOR LOCAL FAST
按用户注册时间统计每天注册的不同来源.不同状态的用户数量: ), RegisterTime, ) RDate ,--DATEPART(YEAR, RegisterTime) 年 ) END 'AWaitForAudit' , ) END 'AAudited' , ) END 'BAudited' INTO #Temp FROM dbo.Customer WHERE RegisterTime BETWEEN '2015-11-23 00:00:00' AND '2015-11-25 23:59:5
今天需要筛选出来库中行数不为零的表,于是动手写下了如下存储过程. CREATE PROCEDURE TableCount AS BEGIN SET NOCOUNT ON ),RowsCount INT) DECLARE @indexid AS INT DECLARE @maxid AS INT DECLARE @count AS INT ) ) FROM dbo.sysobjects WHERE type='U' FROM @t1 WHILE(@maxid>=@indexid) BEGIN SE
declare v_tName varchar(50); v_sqlanalyze varchar(500); v_num number; v_sql varchar(500); cursor c1 is select table_name from user_tables; begin open c1; loop fetch c1 into v_tName; if c1%found then v_sqlanalyze :='analyze table '||v_tName||' es
select sClass 班级,count(*) 班级学生总人数, sum(case when sGender=0 then 1 else 0 end) 女生人数, sum(case when sGender=0 then 1 else 0 end)*1.0/count(*)女生所占比例, sum(case when sGender=1 then 1 else 0 end) 男生人数, sum(case when sGender=1 then 1 else 0 end)*1.0 /coun