2017-04-09 3 views
1

個人や企業のデータを保持するテーブルがあり、個人や企業のデータが見つからない場合はデフォルト値を取得したいとします。私が見つけていないが、この場合には, the same for companies, if the CompanyCustomerAccountId third rowある個人のためのデフォルト値を取得する場合は、それを取得するテーブルで見つかっIndividualCustomerAccountId場合は二つのパラメータIndividualCustomerAccountIdCompanyCustomerAccountIdをとる関数を作成したい表結果が見つからない場合はsqlがデフォルトの行を選択します

CustomerAccountId CustomerAccountType  DueDays  IsAdjustable isDefaultForIndividual isDefaultForCompany  
1     Individual    10   true   false     false 
2     Company     20   false   false     false 
null    null     5   false   true     false 
null    null     30   true   false     true 

を持っていますこの場合はis found retrieve it, if not get the default value for the companies which is第4行目となる。

なし会社のでfirst and fourth rowsを返す必要があり、我々は2番目のパラメータとして

を最初のパラメータとCompanyCustomerAccountIdとしてIndividualCustomerAccountIdを受け入れる目的球を作成したサンプル入力

MyFunc(1 , 2)first and second rows

MyFunc(1 , 50)を返すべきであると仮定テーブルにはCustomerAccountId50がありますので、cのデフォルト値を取得します顧客のアカウントID 100とは個人が見つからないのでsecond and third rowsを返す必要がありますfourth row

MyFunc(100 , 2)あるompaniesので、我々はthird rowある個人のためのデフォルト値を取得し、我々はcustomerAccountId 2で会社を持っているので、我々は単にretriveことができますそれはテーブルから。

私は、これらの結果に

+0

[この役立つかもしれない](http://stackoverflow.com/a/285823/5707687) –

+0

私はあなたのデータを正規化することは選択肢ではないと仮定するつもりですか?より優れた設計であれば、照会が容易になります。 – Crowcoder

+0

@Crowcoderあなたは正しかったですが残念ながらDB設計を変更することはできません –

答えて

2

あなたは、SQL関数を試みることができる

CREATE TABLE Customer 
(
    CustomerAccountId int, 
    CustomerAccountType varchar(20), 
    DueDays int, 
    IsAdjustable bit, 
    isDefaultForIndividual bit, 
    isDefaultForCompany bit 
) 

INSERT INTO Customer VALUES 
(1, 'Individual', 10, 1,0,0), 
(2, 'Company', 20, 0,0,0), 
(null, null, 5, 0,1,0), 
(null, null, 30, 1,0,1) 

GO 

CREATE FUNCTION MyFunc 
(
@IndividualCustomerAccountId int, 
@CompanyCustomerAccountId int 
) 
RETURNs @result TABLE 
(
    CustomerAccountId int, 
    CustomerAccountType varchar(20), 
    DueDays int, 
    IsAdjustable bit, 
    isDefaultForIndividual bit, 
    isDefaultForCompany bit 
) 
BEGIN 

    INSERT INTO @result 
    SELECT CustomerAccountId , CustomerAccountType, DueDays, IsAdjustable, isDefaultForIndividual,isDefaultForCompany 
    FROM Customer c 
    WHERE (CustomerAccountId = @IndividualCustomerAccountId AND CustomerAccountType = 'Individual') 
      OR (CustomerAccountId = @CompanyCustomerAccountId AND CustomerAccountType = 'Company') 

    IF(NOT EXISTS (SELECT 1 FROM Customer c 
        WHERE CustomerAccountId = @IndividualCustomerAccountId AND CustomerAccountType = 'Individual')) 
    BEGIN 
     INSERT INTO @result 
     SELECT CustomerAccountId , CustomerAccountType, DueDays, IsAdjustable, isDefaultForIndividual,isDefaultForCompany 
     FROM Customer c 
     WHERE CustomerAccountId IS NULL AND isDefaultForIndividual = 1 
    END 

    IF(NOT EXISTS (SELECT 1 FROM Customer c 
        WHERE CustomerAccountId = @CompanyCustomerAccountId AND CustomerAccountType = 'Company')) 
    BEGIN 
     INSERT INTO @result 
     SELECT CustomerAccountId , CustomerAccountType, DueDays, IsAdjustable, isDefaultForIndividual,isDefaultForCompany 
     FROM Customer c 
     WHERE CustomerAccountId IS NULL AND isDefaultForCompany = 1 
    END 

    RETURN; 
END 

GO 


SELECT * from dbo.MyFunc(1,2) 
SELECT * from dbo.MyFunc(1,50) 
SELECT * from dbo.MyFunc(100,2) 
SELECT * from dbo.MyFunc(100,50) 
--DROP TABLE Customer 

デモへのリンク:Rextester

0

を達成するためにLINQクエリまたはSQL関数のいずれかを作成する基本的に、あなたはどのようなデータがあるように起こっているかどうかを確認するためにクエリで存在する場合に実行することができます。もしそうなら、あなたの質問を実行してください。そうでない場合は、デフォルトの行を選択します。

If Exists (your query) 

     (your query) 

Else 

     SELECT 'query for default row' 

私は、あなたの問題を明確にすることを願っています。
ハッピーCoding.Thanks

関連する問題