2016-03-21 23 views
0

私の選択クエリは、t0.brandID = nullを削除しています。重要ではないので、1つのテーブルでクエリの結果を取得するだけです。2つのクエリの結果を1つのテーブルに表示する方法

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 

上記のクエリの結果は、画像以下のように2つの異なるテーブルにある:

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はNULL'(使用'は '使用する必要があります'NULLでないか' IS NULLではない - '= nullではない)。 –

+0

あなたの予想される出力が、18バドワイザーレコードではなく8バドワイザーレコードの計算値を持つ理由を説明できますか?あなたの質問を読んでください私はすべてのバドワイザーの記録に同じ価値があることを期待していました。どのようにして2つのクエリの結果を関連/ペアにしたいですか? –

+0

ありがとう、私は選択したレコードの平均、最小、最大、カウントを計算する必要があります。私のテーブルで私はbranid(8,18,23、...、92)でbudgiserエントリーを持っています。平均は46に近い(8 + 18 + 23 + ..)/ 7 = 46.07です。 8、選択されたレコードの最大ブランドIDは92、レコード数は7です。これらの値をbranid、brandname、..、brandid_average、branid_min、brandid_max、brandid_countの選択値とともに表示する必要があります。これはテストシナリオであり、実際の使用は異なるインバータ間で電圧を計算するために使用されます。これはExcelにレポートとしてエクスポートされます。 – Tan

答えて

0

このシナリオでは、2つのテーブル結果を組み合わせる必要がある場合は、両方のテーブルから選択することで簡単に行うことができます。 たとえば、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

「t0.brandname = 'budwieser'」から「where t0.cdt> = '2013-11-14'」および「t0.cdt <= '2013-11-15'」の代わりに2つの日付の間で選択すると、 brandid_averageにブランディッド、brandid_minは常にブランドID、brandid_maxはブランドID、brandid_countは常に1になります。あなたが上記の私が提供する答えをチェックするならば、あなたはavg、min、max、countの完璧な結果を得る何にでもwhere句を変更します...これはあなたの情報のためだけです。 – Tan

関連する問題