【问题标题】:sql select a default row if the result is not foundsql 如果没有找到结果,则选择默认行
【发布时间】:2017-04-09 12:57:18
【问题描述】:

假设我有一个包含个人和公司数据的表,如果没有找到个人或公司的数据,我想检索默认值,例如,假设我有一个表

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

我想创建一个函数,它接受两个参数IndividualCustomerAccountIdCompanyCustomerAccountId,如果在表中找到IndividualCustomerAccountId,则检索它,如果未找到,则检索个人的默认值,即third row,在这种情况下为@在这种情况下为 987654327@CompanyCustomerAccountIdis found retrieve it, if not get the default value for the companies which isfourth row`。

假设我们创建了一个函数,它接受IndividualCustomerAccountId 作为第一个参数,CompanyCustomerAccountId 作为第二个参数

样本输入

MyFunc(1 , 2) 应该返回 first and second rows

MyFunc(1 , 50) 应该返回first and fourth rows,因为在表中没有找到CustomerAccountId 50 的公司,因此请检索公司的默认值fourth row

MyFunc(100 , 2) 应该返回 second and third rows,因为没有找到客户帐户 ID 为 100 的个人,所以我们得到个人的默认值 third row,我们有一个 customerAccountId 2 的公司,所以我们可以简单地从表中检索它。

我想创建一个 LINQ 查询或 SQL 函数来实现这些结果

【问题讨论】:

  • 我会假设规范化您的数据不是一种选择?更好的设计会更容易查询。
  • @Crowcoder 你是对的,但不幸的是我们现在不能改变数据库设计

标签: c# sql sql-server linq


【解决方案1】:

你可以试试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

【讨论】:

    【解决方案2】:

    基本上,您可以在查询中运行 IF Exists 以查看是否会有任何数据。如果是这样,请继续运行您的查询。如果没有,请选择默认行。

    If Exists (your query)
    
           (your query)
    
    Else
    
            SELECT 'query for default row'
    

    我希望它可以解决您的问题。
    快乐编码。谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-01
      • 1970-01-01
      • 2019-04-12
      • 2023-04-04
      • 1970-01-01
      相关资源
      最近更新 更多