执行这条SQL
Select Count(*),ID,Name From Table1
Where CreateDate > '20080201' and CreateDate<'20080401'
and Name = 'xxx'
and ID = 'ID1'
Group by ID,Name
结果集为
85,ID1,xxx执行这条SQL
Select Count(*),ID,Name From Table1
Where CreateDate > '20080201' and CreateDate<'20080401'
and Name = 'xxx'
and ID in ('ID2','ID3',...'ID5')
Group by ID,Name
结果集为
64,ID2,xxx
103,ID3,xxx
...
44,ID4,xxx其中第一条SQL中的ID有可能是第二条SQL中ID集合的元素,也有可能不是现在的问题是,我想将两条SQL合并成一条SQL
然后只出一个二维表
85,ID1,xxx
64,ID2,xxx
103,ID3,xxx
...
44,ID4,xxx
有哪位高人指点一下

解决方案 »

  1.   

    Select Count(*),ID,Name From Table1
    Where CreateDate > '20080201' and CreateDate<'20080401'
    and Name = 'xxx'
    and ID = 'ID1'
    Group by ID,Nameunion allSelect Count(*),ID,Name From Table1
    Where CreateDate > '20080201' and CreateDate<'20080401'
    and Name = 'xxx'
    and ID in ('ID2','ID3',...'ID5')
    Group by ID,Name
      

  2.   

    Select Count(*),ID,Name From Table1
    Where CreateDate > '20080201' and CreateDate<'20080401'
    and Name = 'xxx'
    and ID = 'ID1'
    Group by ID,Name
    union 
    Select Count(*),ID,Name From Table1
    Where CreateDate > '20080201' and CreateDate<'20080401'
    and Name = 'xxx'
    and ID in ('ID2','ID3',...'ID5')
    Group by ID,Name
      

  3.   

    Select Count(*),ID,Name From Table1
    Where CreateDate > '20080201' and CreateDate<'20080401'
    and Name = 'xxx'
    and ID in ('ID1','ID2','ID3',...'ID5')
    Group by ID,Name
      

  4.   

    Select Count(*),ID,Name From Table1
    Where CreateDate > '20080201' and CreateDate<'20080401'
    and Name = 'xxx'
    and ID = 'ID1'
    Group by ID,Name
    union all
    Select Count(*),ID,Name From Table1
    Where CreateDate > '20080201' and CreateDate<'20080401'
    and Name = 'xxx'
    and ID in ('ID2','ID3',...'ID5')
    Group by ID,Name
      

  5.   

    如果就是这两条,可以这样Select Count(*),ID,Name From Table1
    Where CreateDate > '20080201' and CreateDate<'20080401'
    and Name = 'xxx'
    and ID in ('ID1','ID2','ID3',...'ID5')
    Group by ID,Name
      

  6.   

    但是 我有几百个这样的SQL需要合并
    有的Name只对应一个ID 而有点Name对应多个ID不太好控制怎样来union啊
      

  7.   

    你用这个就可以了,因为已经包括了第一个语句的记录了.
    Select Count(*),ID,Name From Table1
    Where CreateDate > '20080201' and CreateDate<'20080401'
    and Name = 'xxx'
    and ID in ('ID2','ID3',...'ID5')
    Group by ID,Name
    如果你要把第一个语句出现的记录重复出现在总记录中,用就下面的
    Select Count(*),ID,Name From Table1
    Where CreateDate > '20080201' and CreateDate<'20080401'
    and Name = 'xxx'
    and ID = 'ID1'
    Group by ID,Nameunion allSelect Count(*),ID,Name From Table1
    Where CreateDate > '20080201' and CreateDate<'20080401'
    and Name = 'xxx'
    and ID in ('ID2','ID3',...'ID5')
    Group by ID,Name
      

  8.   

    Select Count(*),ID,Name From Table1
    Where CreateDate > '20080201' and CreateDate<'20080401'
    and Name = 'xxx'
    and ID in ('ID1','ID2','ID3',...'ID5')
    Group by ID,Name
      

  9.   

    再问一下 SQL Server中对 union 的个数什么限制嘛?
      

  10.   

    终于看到一个我可以回复的问题。
    用union all连接查询的结果集
      

  11.   

    应该没有限制,,,UNION出要是产生一个结果集,跟表一样,,
      

  12.   

    我想到的写法Select Count(*),ID,Name From Table1
    Where CreateDate > '20080201' and CreateDate<'20080401' and 
       (Name = 'xxx' and ID = 'ID1')
    or (Name = 'xxx' and ID in ('ID2','ID3',...'ID5'))
    or (Name = 'yyy' and ID = 'IDxxxxx')
    or ...
    Group by ID,Name
      

  13.   

    Select Count(*),ID,Name From Table1
    Where CreateDate > '20080201' and CreateDate<'20080401'
    and Name = 'xxx'
    and ID in ('ID1','ID2','ID3',...'ID5')
    Group by ID,Name
      

  14.   


    Select Count(*),ID,Name From Table1
    Where CreateDate > '20080201' and CreateDate<'20080401'
    and Name = 'xxx'
    and ID in ('ID1','ID2','ID3',...'ID5')
    Group by ID,Name
      

  15.   

    Select Count(*),ID,Name From Table1
    Where CreateDate > '20080201' and CreateDate<'20080401'
    and Name = 'xxx'
    and ID in ('ID1','ID2','ID3',...'ID5')
    Group by ID,Name
      

  16.   


    select count(cnt),ID,Name
    from (
    Select Count(*) as cnt,ID,Name From Table1
    Where CreateDate > '20080201' and CreateDate<'20080401'
    and Name = 'xxx'
    and ID = 'ID1'
    Group by ID,Name union all Select Count(*) as cnt,ID,Name From Table1
    Where CreateDate > '20080201' and CreateDate<'20080401'
    and Name = 'xxx'
    and ID in ('ID2','ID3',...'ID5')
    Group by ID,Name
    ) a 
    group by ID ,Name
      

  17.   

    SQL code
    select sum(cnt),ID,Name
    from (
            Select Count(*) as cnt,ID,Name From Table1
            Where CreateDate > '20080201' and CreateDate<'20080401'
            and Name = 'xxx'
            and ID = 'ID1'
            Group by ID,Name        union all        Select Count(*) as cnt,ID,Name From Table1
            Where CreateDate > '20080201' and CreateDate<'20080401'
            and Name = 'xxx'
            and ID in ('ID2','ID3',...'ID5')
            Group by ID,Name
    ) a 
    group by ID ,Name
     
     
      

  18.   

    用union all 或 union 把两个sql连接起来就行了,union all 连接出来的结果是两个查询结果的所有记录,union会自动过滤重复的记录(重复的结果只保留一条)