作者:敖士伟
一张有group by后可能很多重复行,这时用not in等基于唯一列的分布算法会存在问题。
我的解决办法是:
一张表有一个id int的主键,对其它列进行group by,分页思想是:把max(id)做group by后的唯一列,还是用not in的分布思想。
例:
select top 4 sum(int_TZ2_id) as id,dt_TZ2_date,vchar_TZ2_PingZheng,vchar_TZ2_ZaiYao,vchar_TZ2_DanSN,convert(decimal(18,4),sum(float_TZ2_J_ShuLiang)) as j_sl, convert(decimal(18,4),sum(money_TZ2_J_JingE)) as j_je,convert(decimal(18,4),sum(float_TZ2_D_ShuLiang)) as d_sl, convert(decimal(18,4),sum(money_TZ2_D_JingE)) as d_je from tbTaiZhang2
where 1=1 and 2=2 group by dt_TZ2_date,vchar_TZ2_PingZheng,vchar_TZ2_ZaiYao,vchar_TZ2_DanSN
having (max(int_TZ2_id) NOT IN
(SELECT TOP 4 max(int_TZ2_id) as id
......
写了一段例子,通过sql创建一个job,定期执行一些清除工作。在sql2005上测试通过。
sql帮助文档太零散了。这是一个完整的流程。不过注意定时执行时需要sql server agent服务器启动的。
USE msdb ;
GO
EXEC dbo.sp_add_job
@job_name = N'Clear oldest HB',
@enabled = 1,
@description = N'Clear heartbeating data older than one month',
@notify_level_eventlog = 3;
GO
EXEC sp_add_jobstep
@job_name = N'Clear oldest HB',
@step_name = N'Clear Data',
@database_name = N'HINAMIIS',
@subsystem = N'TSQL',
@command = N'DELETE from dbo.JobTest WHERE DATEDIFF(DAY, dbo.JobTest.inserttime, GETDATE()) >=30',
@retry_attempts = 1,
@retry_interval = 5 ;
GO
EXEC sp_add_schedule
@schedule_name = N'WeeklyClearJobs' ,
@freq_type = 8,
@freq_interval = 1,
@freq_recurrence_factor = 1,
@active_start_time = 100000 ;
GO
EXEC sp_attach_schedule
@job_name = N'Clear oldest HB',
@schedule_name = N'WeeklyClearJobs' ;
GO
EXEC dbo.sp_add_jobserv ......
函数
SQLServer和Oracle的常用函数对比
1.绝对值
S:select abs(-1) value
O:select abs(-1) value from dual
2.取整(大)
S:select ceiling(-1.001) value
O:select ceil(-1.001) value from dual
3.取整(小)
S:select floor(-1.001) value
O:select floor(-1.001) value from dual
4.取整(截取)
S:select cast(-1.002 as int) value
O:select trunc(-1.002) value from dual
5.四舍五入
S:select round(1.23456,4) value 1.23460
O:select round(1.23456,4) value from dual 1.2346
6.e为底的幂
S:select Exp(1) value 2.7182818284590451
O:select Exp(1) value from dual 2.71828182
7.取e为底的对数
S:select log(2.7182818284590451) value 1
O:select ln(2.7182818284590451) value from dual; 1
8.取10为底对数
S:select log10(10) value 1
O:select log(10,10) value from dual; 1
9.取平方
S:select SQUARE(4) value 16
O:select power(4,2) value from dual 16
10.取平方根
......
SELECT
count
(
userName
)
from
`userInfo`
GROUP
BY
userName
HAVING
count
(
userName
)
>
1。
这个Having一定要放在GROUP BY的后面。
而且很特殊的情况是在有group by的时候,是不能用到where子句的。而且Having子句一般是放在其他子句的后面的。
当我们选不只一个栏位,且其中至少一个栏位有包含函数的运用时,
我们就需要用到
GROUP BY
这个指令。
SELECT "栏位1", SUM("栏位2")
from "表格名"
GROUP BY "栏位1"
HAVING (函数条件)
SELECT * from `userInfo` group by `userName` having count(userName) = 1 ;
......
此问题折腾半天,google也搜索了,网上很多朋友一般做到第二步不成功,我也差点放弃了.最后通过第三步连接成功了.
1.防火墙允许1433
2.配置工具里的SQL Server 配置管理器.打开后,开启右边三个协议.并对tcp/ip如下配置
3.网上大部分做到第二版重启sql server服务,仍然无效.其实,需要修改连接字符串.指定我们配置的端口
new SqlConnection("Server=192.168.1.3,1433;database=db;uid=sa;pwd=sa"); ......
一、具有主键的情况
I.具有唯一性的字段id(为唯一主键)
delete 用户表
where id not in
(
select max(id) from 用户表 group by col1,col2,col3...
)
group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,
那么只要col1字段内容相同即表示记录相同。
II.具有联合主键
假设col1+','+col2+','...col5 为联合主键
(找出相同记录)
select * from 用户表 where col1+','+col2+','...col5 in
(
select max(col1+','+col2+','...col5) from 用户表
group by col1,col2,col3,col4
having count(*)>1
)
group by 子句后跟的字段就是你用来判断重复的条件,
如只有col1,那么只要col1字段内容相同即表示记录相同。
或者:
(找出相同记录)
select * from 用户表 where exists (select 1 from 用户表 x where 用户表.col1 = x.col1 and
用户表.col2= x.col2 group by x.col1,x.col2 having count(*) >1)
III:判断所有的字段
select * into #aa from 用户表 group ......