我的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
您不能使用常規的相等運算符檢查'NULL' - 你需要使用'to.BrandID IS NULL'(使用' IS NULL'或'IS NOT NULL' - 不是'= null')。 –
你能解釋爲什麼你的預期產量有8個百威啤酒記錄的計算值,而不是18個百威啤酒記錄?閱讀你的問題,我期待在百威啤酒的所有記錄中都有相同的價值觀。你如何關聯/配對2個查詢的結果? –
謝謝,我必須計算選定記錄的平均值,最小值,最大值。在我的表格中,我在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。這是測試場景,實際使用將用於計算不同逆變器的電壓。這將作爲報告導出爲excel。 – Tan