sqlserverwithcte
⑴ sqlserver 日期表的问题。
DECLARE@yVARCHAR(4),@mVARCHAR(2)
declare@sdateDATETIME
SET@y='2013'
SET@m='11'
set@sdate=@y+'-'+@m+'-01'
SELECT@sdate
selectDATEADD(dd,number,@sdate)asdate
frommaster..spt_valueswheretype='P'
andDATEADD(dd,number,@sdate)<DATEADD(mm,1,@sdate)
结果:
2013-11-0100:00:00.000
2013-11-0200:00:00.000
2013-11-0300:00:00.000
2013-11-0400:00:00.000
2013-11-0500:00:00.000
2013-11-0600:00:00.000
2013-11-0700:00:00.000
2013-11-0800:00:00.000
2013-11-0900:00:00.000
2013-11-1000:00:00.000
2013-11-1100:00:00.000
2013-11-1200:00:00.000
2013-11-1300:00:00.000
2013-11-1400:00:00.000
2013-11-1500:00:00.000
2013-11-1600:00:00.000
2013-11-1700:00:00.000
2013-11-1800:00:00.000
2013-11-1900:00:00.000
2013-11-2000:00:00.000
2013-11-2100:00:00.000
2013-11-2200:00:00.000
2013-11-2300:00:00.000
2013-11-2400:00:00.000
2013-11-2500:00:00.000
2013-11-2600:00:00.000
2013-11-2700:00:00.000
2013-11-2800:00:00.000
2013-11-2900:00:00.000
2013-11-3000:00:00.000
⑵ sqlserver数据 拆分问题
SELECT*FROM表
EXCEPT
SELECTTOP500000*FROM表
⑶ sqlserver问题。求思路
select t1.uid,datediff(dd,t1.adate,t2.adate) from
(select * from test a where (select count(*) from test b where a.uid=b.uid and b.adate<a.adate) between 1 and 2) t1,
(select * from test a where (select count(*) from test b where a.uid=b.uid and b.adate<a.adate) between 1 and 2) t2
where t1.uid=t2.uid and t2.adate>t1.adate
表名test,自己换一下
⑷ sqlserver 单表 分组 不同查询条件 统计 数据 。
select 姓名,
1 类型1,
sum(isnull(case when 类型=1 then 1 else 0 end,0)) 类型1的个数,
2 类型2,
sum(isnull(case when 类型=2 then 1 else 0 end,0)) 类型2的个数
from 表名
group by 姓名
⑸ sqlserver with 语法
一.sqlserver with as的含义
WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到。有的时候,是为了让SQL语句的可读性更高些,也有可能是在UNION ALL的不同部分,作为提供数据的部分。
特别对于UNION ALL比较有用。因为UNION ALL的每个部分可能相同,但是如果每个部分都去执行一遍的话,则成本太高,所以可以使用WITH AS短语,则只要执行一遍即可。如果WITH AS短语所定义的表名被调用两次以上,则优化器会自动将WITH AS短语所获取的数据放入一个TEMP表里,如果只是被调用一次,则不会。而提示materialize则是强制将WITH AS短语里的数据放入一个全局临时表里。很多查询通过这种方法都可以提高速度。
二.使用方法
先看下面一个嵌套的查询语句:
select * from person.StateProvince where CountryRegionCode in
(select CountryRegionCode from person.CountryRegion where Name like 'C%')
declare @t table(CountryRegionCode nvarchar(3))
insert into @t(CountryRegionCode) (select CountryRegionCode from person.CountryRegion where Name like 'C%')
select * from person.StateProvince where CountryRegionCode
in (select * from @t)
⑹ sqlserver 获得表中的第2条数据
select top 1 * from table
where id not in (select top 1 ID from table order by id)
order by id
⑺ 模糊匹配SQL语句写法 SQLSERVER
declare @str varchar(max)='6688 sasaaaa wty',@sql varchar(max)
set @sql='select * from mingTest where 1=0 '
;with cte as(select name=SUBSTRING(A.[str],number,CHARINDEX(' ',A.[str]+' ',number)-number) from A JOIN master..spt_values ON type='p' and number between 1 and LEN(@str)
WHERE CHARINDEX(' ',' '+A.[str],number)=number)
select @sql=@sql+'or [str] like ''%'+name+'%''' from cte where name<>''
EXEC @sql
⑻ sql server with cte as 的问题
with是CTE的语法。SQLServer2005以上版本(含SQL2005)支持。SQL2000与以下的版本,是不支持的。
⑼ sqlserver 递归查询
CREATE TABLE #tb1(stuId INT,stuName VARCHAR(30),teaId INT);
INSERT INTO #tb1 (stuId,stuName,teaId)
VALUES(1,'zhou',0),(2,'kong',0),(3,'hong',2),(4,'zhang',1),(5,'liu',4),
(6,'zhao',5),(7,'zheng',6),(8,'wei',7)
;WITH cte AS (
SELECT t.stuId,t.stuName,t.teaId FROM #tb1 AS t
WHERE t.stuId=8
UNION ALL
SELECT t.stuId,t.stuName,t.teaId FROM cte AS c
JOIN #tb1 AS t ON c.teaId=t.stuId
)
SELECT * FROM cte