productID  Date 
    1      2008-1-1 
    2      2008-1-1 
    1      2008-1-2 
    1      2008-1-2 
--就这数据你想要什么样的结果

解决方案 »

  1.   

    declare @Date Datetime 
    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分组统计所有纪录 
      

  2.   

    /*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
      

  3.   

    declare @Date Datetime 
    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
      

  4.   

    有点不明白。是简单的union all 还是什么...
      

  5.   


    就简单的union all 就行了
      

  6.   

    不是很明白你的意思,colmn不相同哦
      

  7.   

    好像就是要union all楼主应该贴出想要的结果才好写。
      

  8.   

    1、你的第三条语句已经包含了第二条语句的结果,因此第二条语句应该忽略。
    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) 
      

  9.   

    少了个逗号。 
    select @s = @s + ',sum(case when convert(char(10),date,120) = '''+d
      

  10.   


    --莫非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*/
      

  11.   


    --莫非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*/
      

  12.   

    我没表达清楚哈。先谢谢大家的帮助。特别感谢12楼的哥们,不过这不是我要的结果
    为了讨论方便,我是把问题简化到只有两个字段的表,实际上不止这两个字段。
    我重新描述一下我的问题:
    我有两个表:
    表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的组合。