首页 > 代码库 > sql 常用的查询套路
sql 常用的查询套路
2016-12-01 1 1
2016-12-02 2 3
2016-12-03 2 5
2016-12-04 6 11
A.date,A.user_conut,(A.user_conut+B.user_conut) total_count
from
(select date,count(1) user_count from
user group by date
) A
join (select date,count(1) user_count from
user group by date
) B
on A.date-1=B.date
select COUNT(test)
from
test1 i
where
i.date<=o.date
) from test1 o group by date
方法三:
WITH cte AS(
SELECT a.date,COUNT(1) AS user_count FROM dbo.test007 AS a GROUP BY a.date
)
SELECT a.date,a.user_count,(SELECT SUM(user_count) FROM cte AS b where b.date<=a.date) AS total_count FROM cte AS a
---------------------------------------------------------------
一条sql掏出每个分类的前两条记录
一条sql取出每个分类的前两条记录
写道
--> 生成测试数据: #T
IF OBJECT_ID(‘tempdb.dbo.#T‘) IS NOT NULL DROP TABLE #T
CREATE TABLE #T (ID VARCHAR(3),GID INT,Author VARCHAR(29),Title VARCHAR(39),Date DATETIME)
INSERT INTO #T
SELECT ‘001‘,1,‘邹建‘,‘深入浅出SQLServer2005开发管理与应用实例‘,‘2008-05-10‘ UNION ALL
SELECT ‘002‘,1,‘胡百敬‘,‘SQLServer2005性能调校‘,‘2008-03-22‘ UNION ALL
SELECT ‘003‘,1,‘格罗夫Groff.J.R.‘,‘SQL完全手册‘,‘2009-07-01‘ UNION ALL
SELECT ‘004‘,1,‘KalenDelaney‘,‘SQLServer2005技术内幕存储引擎‘,‘2008-08-01‘ UNION ALL
SELECT ‘005‘,2,‘Alex.Kriegel.Boris.M.Trukhnov‘,‘SQL宝典‘,‘2007-10-05‘ UNION ALL
SELECT ‘006‘,2,‘飞思科技产品研发中心‘,‘SQLServer2000高级管理与开发‘,‘2007-09-10‘ UNION ALL
SELECT ‘007‘,2,‘胡百敬‘,‘SQLServer2005数据库开发详解‘,‘2008-06-15‘ UNION ALL
SELECT ‘008‘,3,‘陈浩奎‘,‘SQLServer2000存储过程与XML编程‘,‘2005-09-01‘ UNION ALL
SELECT ‘009‘,3,‘赵松涛‘,‘SQLServer2005系统管理实录‘,‘2008-10-01‘ UNION ALL
SELECT ‘010‘,3,‘黄占涛‘,‘SQL技术手册‘,‘2006-01-01‘
--SQL查询如下:
--按GID分组,查每个分组中Date最新的前2条记录
--1.字段ID唯一时:
SELECT * FROM #T AS T WHERE ID IN(SELECT TOP 2 ID FROM #T WHERE GID=T.GID ORDER BY Date DESC)
--2.如果ID不唯一时:
SELECT * FROM #T AS T WHERE 2>(SELECT COUNT(*) FROM #T WHERE GID=T.GID AND Date>T.Date)
--SQL Server 2005 使用新方法
--3.使用ROW_NUMBER()进行排位分组
SELECT ID,GID,Author,Title,Date
FROM
(
SELECT rid=ROW_NUMBER() OVER(PARTITION BY GID ORDER BY Date DESC),*
FROM #T
) AS T
WHERE rid<=2
--4.使用APPLY
SELECT DISTINCT b.*
FROM #T AS a
CROSS APPLY
(
SELECT TOP(2) * FROM #T WHERE a.GID=GID ORDER BY Date DESC
) AS b
--结果
/*
ID GID Author Title Date
---- ----------- ----------------------------- --------------------------------------- -----------------------
003 1 格罗夫Groff.J.R. SQL完全手册 2009-07-01 00:00:00.000
004 1 KalenDelaney SQLServer2005技术内幕存储引擎 2008-08-01 00:00:00.000
005 2 Alex.Kriegel.Boris.M.Trukhnov SQL宝典 2007-10-05 00:00:00.000
007 2 胡百敬 SQLServer2005数据库开发详解 2008-06-15 00:00:00.000
009 3 赵松涛 SQLServer2005系统管理实录 2008-10-01 00:00:00.000
010 3 黄占涛 SQL技术手册 2006-01-01 00:00:00.000
(6 行受影响)
以上内容引自互联网,
下面简要说明下在什么情况下会遇到此种情景,
比如有这样一种需求:
某一父类有很多的子类,每一子类又对应多条内容,此时我们需要获取包含该父类下所有子类的内容,且规定每个子类限取两条,则可以使用以上方法,具体使用情况可根据以上方式区别对待。
-------------------
select *
from
(select *,row_number() over(partition by classid order by classid) as rownum
--上面利用row_number()先按classid分类,然后给每类内分别编号1、2、3...
from tb) as tb1
where rownum<4
--------------------------------
要 id为188的前三条和id189的前三条 下面还有 每种都有前三条 要 oracle的代码 谢谢
最佳答案:
select
a.*
from
(
select
row_number()over(partition
by
id
order
by
排序字段)
as
num,*
from
[
table
] )a
where
num<=3
[SQL]select a.* from
(select row_number()over(partition by MENU_PARENTID order by MENU_PARENTID) as num,*
from DL_MENU )a where num<=3
[Err] ORA-00936: missing expression
select a.* from
(select row_number()over(partition by menu_parentid order by menu_parentid) as num,t.*
from DL_MENU t)a where num<=3
比如有分类表category。表中有A,B,C,D四个分类
商品表product中有一人categoryId与category关联。
请问:如何人product表中获取每个分类的第一条数据呢?
CREATE table Cate (CateID int, CateName Nvarchar(20)); CREATE table Product (ProductID int, CateID int, ProductName Nvarchar(20)); INSERT Cate values (1,‘DemoA‘), (2,‘DemoB‘), (3,‘DemoC‘), (4,‘DemoD‘) INSERT PRODUCT values (1,1,‘PA‘), (2,3,‘PB‘), (3,2,‘PC‘), (4,2,‘PD‘), (5,4,‘PE‘), (6,3,‘PF‘), (7,1,‘PG‘), (8,4,‘PH‘) SELECT * FROM product; /* ProductID CateID ProductName 1 1 PA 2 3 PB 3 2 PC 4 2 PD 5 4 PE 6 3 PF 7 1 PG 8 4 PH */
--方法一: Select t.CateID,t.ProductID,t.ProductName From (Select CateID,ProductID,ProductName, Row_Number() over(Partition By CATEID Order by productid Asc) RowID From PRODUCT ) t WHERE t.RowID=1 --方法二: SELECT CateID,ProductID,ProductName FROM PRODUCT T WHERE productID in ( SELECT top 1 productID FROM PRODUCT WHERE cateid=T.cateid order by cateID asc ) /* CateID ProductID ProductName 1 1 PA 2 3 PC 3 2 PB 4 5 PE */
sql 常用的查询套路