SQL方面
Code
title 排前面
select
*
from
tb
where
title like '%酒精灯%' or body like '%酒精灯%'
order by
case when title like '%酒精灯%' then 0 else 1 end
得到一个加法的
declare @str varchar(8000)
select @str = isnull(@str + '/','') + [piShi] from tab2 where arcID = @arcID and charindex('采用',[piShi])>0
return @str
删除重复值, 这里是编号相同的。。但日期不同。。
delete t from tb t where not exists(select 1 from tb where 编号=t.编号 and 日期>t.日期)
表A
TYPE NAME PRICE
2 A 5.4
2 A 3.7
2 B 4.3
1 B 4.7
2 B 6.7
2 B 9.8
要求按TYPE NAME分组查出PRICE对应的中值.中值定义为,如果分组的记录条数奇数时,就是从高到低排序最中间的那条记录,如果分组的记录条数为偶数时,就取中间两条记录的PRICE的平均值
以上表需生成结果为
TYPE NAME PRICE
2 A 4.55 (注:为5.4+3.7的平均值)
2 B 6.7
1 B 4.7
set nocount on
declare @t table (Type int ,Name char,Price numeric(2,1))
insert into @t select 2, 'A', 5.4
union all select 2, 'A', 3.7
union all select 2, 'B', 4.3
union all select 1, 'B', 4.7
union all select 2, 'B' , 6.7
union all select 2, 'B', 9.8
/*
2 A 4.55 (注:为5.4+3.7的平均值)
2 B 6.7
1 B 4.7
*/
select Type,
Name,
sum(case
when mpx%2=1 and px=mpx/2+1 then Price
when mpx%2=0 and (px=mpx/2 or px=mpx/2+1) then Price/2.0
else 0
end ) as AvgPrice
from
(
select *,
px=(select count(1)+1 from @t where Type=t.Type and Name=t.Name and Price>t.Price) ,
mpx=(select count(1) from @t where Type=t.Type and Name=t.Name )
from @t t
) x
group by Type,Name
order by Type,Name
DECLARE @T TABLE (TYPE INT,NAME VARCHAR(1),PRICE NUMERIC(2,1))
INSERT INTO @T
SELECT 2,'A',5.4 UNION ALL
SELECT 2,'A',3.7 UNION ALL
SELECT 2,'B',4.3 UNION ALL
SELECT 1,'B',4.7 UNION ALL
SELECT 2,'B',6.7 UNION ALL
SELECT 2,'B',9.8
--SQL查询如下:
;WITH Liang AS
(
SELECT
TYPE,NAME,PRICE,
asc_rnk=ROW_NUMBER() OVER(PARTITION BY TYPE,NAME ORDER BY PRICE),
desc_rnk=ROW_NUMBER() OVER(PARTITION BY TYPE,NAME ORDER BY PRICE DESC)
FROM @T
)
SELECT TYPE,NAME,AVG(PRICE) AS PRICE
FROM Liang
WHERE asc_rnk IN(desc_rnk,desc_rnk+1,desc_rnk-1)
GROUP BY TYPE,NAME
ORDER BY TYPE,NAME
跨服务器更新
可以
在B 服务器上用JOB, 管理->作业->新建作业
选择周期,和连接的数据库,你要执行的语句即可
insert into 表
select A.数据库.dbo.表
行转列
表A id city money
1 北京 100
2 天津 200
3 上海 300
4 北京 200
5 天津 300
6 上海 200
结果 id 北京 天津
1 100 空
2 空 200
4 200 空
5 空 300
select id,[北京]=sum(case city when '北京' then [money] end),[天津]=sum(case city when '天津' then [money] end) from 表A where city in('北京','天津') group by id
title 排前面
select
*
from
tb
where
title like '%酒精灯%' or body like '%酒精灯%'
order by
case when title like '%酒精灯%' then 0 else 1 end
得到一个加法的
declare @str varchar(8000)
select @str = isnull(@str + '/','') + [piShi] from tab2 where arcID = @arcID and charindex('采用',[piShi])>0
return @str
删除重复值, 这里是编号相同的。。但日期不同。。
delete t from tb t where not exists(select 1 from tb where 编号=t.编号 and 日期>t.日期)
表A
TYPE NAME PRICE
2 A 5.4
2 A 3.7
2 B 4.3
1 B 4.7
2 B 6.7
2 B 9.8
要求按TYPE NAME分组查出PRICE对应的中值.中值定义为,如果分组的记录条数奇数时,就是从高到低排序最中间的那条记录,如果分组的记录条数为偶数时,就取中间两条记录的PRICE的平均值
以上表需生成结果为
TYPE NAME PRICE
2 A 4.55 (注:为5.4+3.7的平均值)
2 B 6.7
1 B 4.7
set nocount on
declare @t table (Type int ,Name char,Price numeric(2,1))
insert into @t select 2, 'A', 5.4
union all select 2, 'A', 3.7
union all select 2, 'B', 4.3
union all select 1, 'B', 4.7
union all select 2, 'B' , 6.7
union all select 2, 'B', 9.8
/*
2 A 4.55 (注:为5.4+3.7的平均值)
2 B 6.7
1 B 4.7
*/
select Type,
Name,
sum(case
when mpx%2=1 and px=mpx/2+1 then Price
when mpx%2=0 and (px=mpx/2 or px=mpx/2+1) then Price/2.0
else 0
end ) as AvgPrice
from
(
select *,
px=(select count(1)+1 from @t where Type=t.Type and Name=t.Name and Price>t.Price) ,
mpx=(select count(1) from @t where Type=t.Type and Name=t.Name )
from @t t
) x
group by Type,Name
order by Type,Name
DECLARE @T TABLE (TYPE INT,NAME VARCHAR(1),PRICE NUMERIC(2,1))
INSERT INTO @T
SELECT 2,'A',5.4 UNION ALL
SELECT 2,'A',3.7 UNION ALL
SELECT 2,'B',4.3 UNION ALL
SELECT 1,'B',4.7 UNION ALL
SELECT 2,'B',6.7 UNION ALL
SELECT 2,'B',9.8
--SQL查询如下:
;WITH Liang AS
(
SELECT
TYPE,NAME,PRICE,
asc_rnk=ROW_NUMBER() OVER(PARTITION BY TYPE,NAME ORDER BY PRICE),
desc_rnk=ROW_NUMBER() OVER(PARTITION BY TYPE,NAME ORDER BY PRICE DESC)
FROM @T
)
SELECT TYPE,NAME,AVG(PRICE) AS PRICE
FROM Liang
WHERE asc_rnk IN(desc_rnk,desc_rnk+1,desc_rnk-1)
GROUP BY TYPE,NAME
ORDER BY TYPE,NAME
跨服务器更新
可以
在B 服务器上用JOB, 管理->作业->新建作业
选择周期,和连接的数据库,你要执行的语句即可
insert into 表
select A.数据库.dbo.表
行转列
表A id city money
1 北京 100
2 天津 200
3 上海 300
4 北京 200
5 天津 300
6 上海 200
结果 id 北京 天津
1 100 空
2 空 200
4 200 空
5 空 300
select id,[北京]=sum(case city when '北京' then [money] end),[天津]=sum(case city when '天津' then [money] end) from 表A where city in('北京','天津') group by id
SQL中写范围值
整数:select 150+cast(rand()*(300-150)as int)
小数:select 6.9+rang()*(7.3-6.9)