2016-03-21 157 views
0

我的select查詢,我已經刪除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 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 
+0

您不能使用常規的相等運算符檢查'NULL' - 你需要使用'to.BrandID IS NULL'(使用' IS NULL'或'IS NOT NULL' - 不是'= null')。 –

+0

你能解釋爲什麼你的預期產量有8個百威啤酒記錄的計算值,而不是18個百威啤酒記錄?閱讀你的問題,我期待在百威啤酒的所有記錄中都有相同的價值觀。你如何關聯/配對2個查詢的結果? –

+0

謝謝,我必須計算選定記錄的平均值,最小值,最大值。在我的表格中,我在Branid's(8,18,23,...,92)有budwiser條目,平均值爲(8 + 18 + 23 + ..)/ 7 = 46.07,接近46.選定記錄的最小brandid是8,所選記錄的最大brandid是92,記錄數是7.我必須顯示這些值以及選定的值,如branid,brandname,..,brandid_average,branid_min,brandid_max,brandid_count。這是測試場景,實際使用將用於計算不同逆變器的電壓。這將作爲報告導出爲ex​​cel。 – Tan

回答

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 

同樣,對於上述問題,因爲沒有物理表存在,我們要聲明一個表,並保存值到臨時表,然後簡單地從兩個表中選擇將返回問題所需的結果。

 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 
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' 
+0

如果我選擇兩個日期而不是「Where t0.brandname ='budwieser'」to「where t0.cdt> ='2013-11-14'and t0.cdt <='2013-11-15'」它會將brandid_average始終作爲brandid,brandid_min作爲brandid,brandid_max作爲brandid,brandid_count將始終爲1,這是不正確的。如果你檢查我上面提供的答案,你將where子句更改爲任何你會得到完美結果的平均值,最小值,最大值和數值......這僅僅是爲了您的信息,希望您同意,如果我錯了,請糾正我 – Tan

相關問題