2017-06-26 27 views
-1

2つのテーブルPartnerCountryとComparerCountryを以下のようにします。フィルタ条件を作成:2つのテーブルに基づいて

更新されたテーブルの構造

PartnerTable 
Country     TradeValue 
---------------   ----------- 
Germany     | 100 
France     | 2000 


ComparerTable 
Country     TradeValue 
---------------   ----------- 
India     | 100 
Korea     | 5000 

古いテーブル構造

PartnerTable 
Germany    France 
---------------  ----------- 
100     | 2000 


ComparerTable 
India     Korea 
---------------  ----------- 
100     | 5000 

条件必須

case when ( [Germany]>[India] 
         or 
       [Germany]>[Rep. of Korea]) 

      or 
     (
       [france]>[India] 
         or 
        [france]>[Rep. of Korea]) then '1' else '0' end as 'Status' 

の表は、より多くのcountries.The VALUを持つことができますどちらもパートナー表からの任意の国のeは、比較表のいずれよりも大きくなければなりません。

新しいテーブル定義に基づいて上記の達成方法を教えてください。 条件を準備するために動的クエリを作成する予定でした。

+0

を比較します。 –

+0

あなたのコードには何が問題なのですか? –

+1

テーブルの定義が間違っていると思います。国は列ではなく行であってはなりません。これは、テーブルの列スキーマを取得し、クエリで使用するよう強制します。典型的には、単純なクエリがより複雑なものになる理由です。 – derloopkat

答えて

2

まず-------------そして、あなただけの古いテーブル構造を持っている場合にはUNPIVOTを使用してデータを、新しい変換テーブル構造

CREATE TABLE ParentTable 
(
Germany decimal(16,2), 
France decimal(16,2) 
) 



INSERT INTO ParentTable VALUES(100,2000) 


CREATE TABLE CompareTable 
(
India decimal(16,2), 
Korea decimal(16,2) 
) 

INSERT INTO CompareTable VALUES(100,5000) 

にデータを挿入します----------- NEWテーブルの構造の場合にDYMANIC列名を持つUNPIVOTを使用した

CREATE TABLE ParentTable_NEW 
    (
    Country varchar(100), 
    TradeValue decimal(16,2) 
    ) 


    CREATE TABLE CompareTable_NEW 
    (
    Country varchar(100), 
    TradeValue decimal(16,2) 
    ) 

-------------------- YOUその他の国を追加することがあります。親のテーブル

declare @cols nvarchar(max) 
select @cols = coalesce(@cols+N',', N'') + quotename(c.name) from syscolumns c 
inner join sysobjects o on c.id = o.id and o.xtype = 'u' 
where o.name = 'ParentTable' -- order by c.colid 

declare @query nvarchar(max) 


select @query = N' 
select Country,Value 
from 
    (
    select ' + @cols + ' 
    from ParentTable 

    ) as cp 
    unpivot 
    (
    Value for Country in (' + @cols + ') 
    ) as up 
' 

INSERT INTO ParentTable_NEW 
exec sp_executesql @query 
私は私の質問を編集した@derloopkatあなたがより多くの国を追加することができた場合のDYMANIC列名を持つUNPIVOTを使用した

--------------------はTABLE

declare @cols2 nvarchar(max) 
select @cols2 = coalesce(@cols2+N',', N'') + quotename(c.name) from syscolumns c 
inner join sysobjects o on c.id = o.id and o.xtype = 'u' 
where o.name = 'CompareTable' -- order by c.colid 


declare @query2 nvarchar(max) 

select @query2 = N' 
select Country,Value 
from 
    (
    select ' + @cols2 + ' 
    from CompareTable 

    ) as cp 
    unpivot 
    (
    Value for Country in (' + @cols2 + ') 
    ) as up 
' 

INSERT INTO CompareTable_NEW 
exec sp_executesql @query2 



----------Final query 

SELECT part.Country AS partner_country, 
     part.TradeValue AS parter_value, 
     comp.Country AS comp_country, 
     comp.TradeValue AS comp_value, 
     CASE WHEN part.TradeValue > comp.TradeValue THEN 1 ELSE 0 END AS 'Status' 
FROM ParentTable_NEW part 
CROSS APPLY CompareTable_NEW comp 
ORDER BY part.Country 
+0

古いテーブル構造ではどうすれば実現できますか? –

+1

UNPIVOTを使用して古いテーブル構造を新しいテーブル構造に変換し、最初に列を取得して新しいテーブル構造にデータを挿入します –

+0

感謝。:)。 –

関連する問題