日期:2014-05-18  浏览次数:20513 次

SQL2000中数据分类后取每类数据前4项出问题
是这样的:Prudocts表是ProductId,ProductName,CategoryId字段,Category表,字段有CategoryId,Depth字段
我现在要取出Product表中的ProductId,ProductName,CategoryId前4项数据,要求是Product表中的CategoryId属于Category表,且Depth=1,我自己写的SQL语句如下:

select a.ProductId,a.CategoryId,a.ProductName from Products a 
where (select count(*) from Products a where CategoryId in 
(select b.CategoryId from Category b where b.Depth=1) and ProductId<a.ProductId)<4
order by CategoryId

查询结果永远不对。。。。我搞了好久的,请各方朋友出手帮一下,在线等。。。

------解决方案--------------------
探讨
修改一下~

SQL code

SELECT *
FROM Prudocts AS T
WHERE (SELECT COUNT(*) FROM Prudocts WHERE categoryId=T.categoryId AND ProductId<T.ProductId)<4
and exists (select 1 from Category where Depth=1 a……