productID Date
1 2008-1-1
2 2008-1-1
1 2008-1-2
1 2008-1-2
--就这数据你想要什么样的结果
1 2008-1-1
2 2008-1-1
1 2008-1-2
1 2008-1-2
--就这数据你想要什么样的结果
解决方案 »
- sql server2005 性能计数器
- 把语句转换为PL/SQL 谢谢啊
- 不知这样的情况该怎么处理???
- 关于在sql语句中where 条件后面加 like 的问题 C.category_id=P.product_category_id ,我要把=换成 like可是总是错,应该如何写?
- 在高并发的系统怎样获取一个全局唯一的编号?
- 通过查询分析器执行后的.sql文件,在哪里可以查看到具体的表格及连接???(在线等)
- 怎样定义这样的规则??
- 急急急!!关于触发器执行失败的问题!!
- 为什么我录入的数据不能保存呢?
- 还原数据库提示“ 因为数据库正在使用,所以无法获得对数据库的独占访问权。”
- 这个SQL 语句怎么写?好像SQL 只有CASE分支
- 我想查询结果先按紧急程度排序再按id排序,sql语句怎么写?
select count(productID)as allQuantity from table group by productID //按productID分组统计所有纪录
select count(productID)as allQuantity from table where Date=@Date group by productID //按productID分组统计某一天的纪录
-->
select count(productID)as allQuantity from table where Date=isnull(@Date,Date) group by productID
这句没办法,group by的条件都不一样了,没有办法合并
select count(productID)as allQuantity,Date from table group by productID , Date //按productID,Date分组统计所有纪录
1 2008-1-1
2 2008-1-1
1 2008-1-2
1 2008-1-2 */
create table tableA
(
productID int,
Date datetime
)
select * from tableA
insert into tableA(productID,Date) values(1,'2008-1-1')
insert into tableA(productID,Date) values(2,'2008-1-2')
insert into tableA(productID,Date) values(1,'2008-1-3')
insert into tableA(productID,Date) values(2,'2008-1-4')select count(distinct(productID)),Date from tableA group by productID,Date
select count(productID)as allQuantity,null as Date from table group by productID
union all
select count(productID)as allQuantity,null as Date from table where Date=@Date group by productID
union all
select count(productID)as allQuantity,Date from table group by productID , Date //按productID,Date
就简单的union all 就行了
2、你的结果集中没有分组字段,应该加上。
3、你的结果形式没有说明。
3.1 如果你要的行合并,可以用UNION
select productid,date,count(1) allquantity from table group by productid,date
union
select productid,null,count(1) allquantity from table group by productid
3.2 如果你要进行列合并,可以用动态行转列
declare @s varchar(10)
select @s = 'select productid,count(*) as allquantity'
select @s = @s + 'sum(case when convert(char(10),date,120) = '''+d
+ ''' then 1 else 0 end)as ['+d+'] '
from (select distinct convert(char(10),date,120) as d from table) a
select @s = @s + ' from table group by productid '
exec(@s)
select @s = @s + ',sum(case when convert(char(10),date,120) = '''+d
--莫非LZ想要这种?????????declare @tb table(productID int,date datetime)
insert into @tb select 1,'2008-1-1'
union all select 2,'2008-1-1'
union all select 1,'2008-1-2'
union all select 1,'2008-1-2'select case when (grouping(productID)=1) then '合计' else ltrim(productID) end as productID,
case when (grouping(productID)=1) then '' when (grouping([date])=1) then '小计' else convert(varchar(10),[date],120) end as [date],
count(productID) from @tb
group by productID,date WITH ROLLUP/*
1 2008-01-01 1
1 2008-01-02 2
1 小计 3
2 2008-01-01 1
2 小计 1
合计 4*/
--莫非LZ想要这种?????????declare @tb table(productID int,date datetime)
insert into @tb select 1,'2008-1-1'
union all select 2,'2008-1-1'
union all select 1,'2008-1-2'
union all select 1,'2008-1-2'select case when (grouping(productID)=1) then '合计' else ltrim(productID) end as productID,
case when (grouping(productID)=1) then '' when (grouping([date])=1) then '小计' else convert(varchar(10),[date],120) end as [date],
count(productID) from @tb
group by productID,date WITH ROLLUP/*
1 2008-01-01 1
1 2008-01-02 2
1 小计 3
2 2008-01-01 1
2 小计 1
合计 4*/
为了讨论方便,我是把问题简化到只有两个字段的表,实际上不止这两个字段。
我重新描述一下我的问题:
我有两个表:
表1:AddRecord:
ProductID Quantity Date
1 10 2008-1-1
2 10 2008-1-1
1 20 2008-1-2
1 20 2008-1-2
表2:Product:
productID productName Date
1 name_A 2008-1-1
2 name_B 2008-1-1
1 name_A 2008-1-2
1 name_A 2008-1-2 针对三种不同的统计需要,分别对应两条语句(其中两种情况我已经合并了)。
我可以这样写:
declare @isGroupByDate int ,@Date Datetime ,@sql nvarchar (5000)
if((@Date == null) && (@isGroupByDate == 1))
{
select @sql=sql语句2
}
else
{
select @sql=sql语句1;
}
sql语句1:
select ISNULL(@LDateInfo,@LDateInfo) as [LDateInfo],A.ProductName, A.ProductID, ISNULL(Quantity,0) as [Quantity] from
(select ProductName,ProductID from Product group by ProductID ,ProductName)as A left join
(select ProductID,count(ProductID) as Quantity from AddRecord where 1=1 and(@Date is null or Date=@Date) group by ProductID)as B on
A.ProductID=B.ProductID
这条语句有两种结果(也就是我所说的这两种情况已经用一条语句实现):
一种是不区分Date的,另一种是指统计某一个Date的情况sql语句2:
select A.Date ,A.ProductName, A.ProductID, ISNULL(B.Quantity,0) as [Quantity] from
(select * from Product ) as A left join
(select LDate,ProductID,count(ProductID) as Quantity from AddRecord group by ProductID, Date)as B on
A.ProductID=B.ProductID and A.Date=B.Date order by A.Date,A.ProductID
这条语句的结果是区分Date而且是对所有Date而不是某个Date的统计结果。
该语句第二行写成(select * from Product ) as A只是希望和第一条语句统一。
两条语句相比较:集合A不一样,集合B的where 和group by不一样(其中where可以用and(@isGroupByDate is null or...实现),但是我不知道该如何写group) ,on的条件不一样(这个可以用and(@isGroupByDate is null or A.Date=B.Date))实现。
我的问题就是:如何借助变量@isGroupByDate用一条语句实现上述两条语句的功能。
具体地说,是如何用一条语句实现结果集A,如何用一条语句实现group by的组合。
谢