如何在一个表中显示两个查询的结果

时间:2016-03-21 16:00:51

标签: sql-server sql-server-2008

我的选择查询,我删除了t0.brandID = null它不是那么重要只需要在一个表中获取查询结果。

SELECT 
    t0.brandID, t0.brandName, t0.cdt, t0.udt, t0.brandstatus, t0.AddedBy  
FROM 
    brands t0 
WHERE
    t0.brandName = 'budwieser'

SELECT
    AVG(CAST (brandID AS bigint)) AS brandID_AVERAGE,
    MIN(CAST (brandID AS bigint)) as branid_min,
    MAX(CAST (brandID AS bigint)) as brandid_max,
    COUNT(CAST (brandID AS bigint)) as brandid_count  
FROM
    (SELECT 
         t0.brandID, t0.brandName, t0.cdt, t0.udt, t0.brandstatus, t0.AddedBy  
     FROM 
         brands t0 
     WHERE
         t0.brandID = null OR t0.brandName = 'budwieser') temptable 

上述查询的结果如下图所示:

brandid brandname       cdt                       udt       brandstatus added by  

    8   budwieser   2013-11-14 16:26:43.913 2014-02-12 19:26:43.913 1   8
    18  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1
    23  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1
    37  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1
    63  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1
    82  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1
    92  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1

brandid_average  brandid_min   brandid_max   brandid_count
 46              8             92             7

我想将结果显示如下:

  brandid brandname   cdt               udt  brandstatus addedby branid_average brandid_min   brandid_max   branid_count
     8   budwieser 2013-11-14      2014-02-12   1    8          46             8               92             7
    18  budwieser  2013-11-15      2013-11-15   1    1         null           null            null          null
    ........................................................       null           null            null          null
    --------------------------------------------------------       null           null            null          null

2 个答案:

答案 0 :(得分:0)

在需要组合两个表结果的情况下,可以通过从两个表中进行选择来简单地完成。 例如table1,table2。

Table1 has columns id,name,age
table2 has columns someid,group,gender

显示以下格式的结果的简单查询是:

select t1.*,t2.* from table1 t1,table2 t2

id name age someid group gender
1  one  1    10     5     M

类似于上述问题,因为不存在物理表,我们必须声明一个表并将值保存到该临时表中,然后只需从两个表中选择将返回所需结果的表。 / p>

     drop table #mytemptable --- droping the temporary table if exists 
//select query
      select AVG(CAST (brandID AS bigint)) AS brandID_AVERAGE,
       min(CAST (brandID AS bigint)) as branid_min,
       MAX(CAST (brandID AS bigint)) as brandid_max,
       COUNT(CAST (brandID AS bigint)) as brandid_count
 into #mytemptable ---//Here inserting the selected values from below query to temporary table
  from
      ( SELECT t0.brandID, t0.brandName, t0.cdt, t0.udt, t0.brandstatus, t0.AddedBy  

       FROM brands t0 
       Where  t0. brandID=null OR  t0. brandName='budwieser'

       )   temptable 
       //simple select from both tables
       SELECT t0.brandID, t0.brandName, t0.cdt, t0.udt, t0.brandstatus, t0.AddedBy ,t2.* 
       from brands t0,#mytemptable t2
       where t0.brandName='budwieser'

预期结果将是:

brandID brandName   cdt udt brandstatus AddedBy brandID_AVERAGE branid_min  brandid_max brandid_count
8   budwieser   2013-11-14 16:26:43.913 2014-02-12 19:26:43.913 1   8   46  8   92  7
18  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1   46  8   92  7
23  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1   46  8   92  7
37  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1   46  8   92  7
63  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1   46  8   92  7
82  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1   46  8   92  7
92  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1   46  8   92  7

答案 1 :(得分:0)

SELECT t0.brandID,
       t0.brandName,
       t0.cdt,
       t0.udt,
       t0.brandstatus,
       t0.AddedBy,
       AVG(CAST(brandID AS BIGINT)) OVER(PARTITION BY t0.brandName) AS brandID_AVERAGE,
       MIN(CAST(brandID AS BIGINT)) OVER(PARTITION BY t0.brandName) AS branid_min,
       MAX(CAST(brandID AS BIGINT)) OVER(PARTITION BY t0.brandName) AS brandid_max,
       COUNT(CAST(brandID AS BIGINT)) OVER(PARTITION BY t0.brandName) AS brandid_count
FROM   brands t0
WHERE  t0.brandName = 'budwieser'