【问题标题】:how to divided one column into two column in sql server? [closed]如何在sql server中将一列分成两列? [关闭]
【发布时间】:2020-03-23 12:44:58
【问题描述】:

我正在使用 SQL,我想在 SQL 中将一列分成两列

图片:

数据库图:

表和查询

在我的程序中,两个用户共享同一个帐户,我想显示他们为这个帐户做了什么

我是单字段交易管理,分为贷记和借记两栏:

我的逻辑是什么

//如果用户有> 0表示贷方,用户有

Select transactionid,
      transactiondate,
      case WHEN transactionamount > 0 Then 'Credit' As Credit,
      case WHEN transactionamount < 0 Then 'Debit' As Debit,
      totalbalance,
      userid
      from transactions where userid = 1

例如我想要的:

其中用户 ID = 1

transactionid  credit debit  totalbalance   userid
2              5000   -        5000          1
5               -     -1500    3500          1   

其中用户 ID = 2

transactionid   credit   debit  totalbalance   userid
3                -      -1000    4000            2
4                2000     -      6000            2   

目前,报错As附近的语法不正确

如何解决这个问题?帮助

【问题讨论】:

  • 您的 case 表达式缺少 end
  • @HoneyBadger 没有解决问题Then END 'Credit' As Credit
  • 请研究如何正确使用case。先做自己的研究,然后提出问题

标签: sql sql-server tsql case


【解决方案1】:

您希望不同列中的金额取决于它们是正数还是负数。您可以为此使用两个 case 表达式:

select 
    transactionid,
    case when transactionamount >= 0 then transactionamount end credit,
    case when transactionamount <  0 then transactionamount end debit,
    totalbalance,
    userid
from transactions
where userid = ?
order by transactionid

请注意,当事务没有落入正确的存储桶而不是'-'(这是一个字符串,需要额外的操作)时,这会产生null

如果您正在运行 MySQL(或其他与 SQL Server 不同,支持 LEAST()GREATEST() 的数据库):

select 
    transactionid,
    greatest(transactionamount, 0) credit,
    least(transactionamount, 0) debit,
    totalbalance,
    userid
from transactions
where userid = ?
order by transactionid

【讨论】:

  • 这是如果一个条件或其他条件你可以告诉我运行哪个条件then transactionamount end credit if condition run or else condition run
  • @rajubhai:因为没有else,所以当条件不满足时会产生null
【解决方案2】:

你可以这样做:

select
  transactionid,
  transactiondate,
  case WHEN transactionamount > 0 Then 'Credit' end As Credit,
  case WHEN transactionamount < 0 Then 'Debit' end As Debit,
  totalbalance,
  userid
from transactions where userid = 1

【讨论】:

    【解决方案3】:

    您正在尝试使用错误的语法。以下是 SQL Server 中CASE (Transact-SQL) 的详细信息。

    语法

    简单的 CASE 表达式:

    CASE input_expression   
         WHEN when_expression THEN result_expression [ ...n ]   
         [ ELSE else_result_expression ]   
    END   
    Searched CASE expression:  
    CASE  
         WHEN Boolean_expression THEN result_expression [ ...n ]   
         [ ELSE else_result_expression ]   
    END
    

    供您查询。

    SELECT transactionid
        ,transactiondate
        ,CASE 
            WHEN ISNULL(transactionamount, 0) > 0
                THEN 'Credit'
            END AS Credit
        ,CASE 
            WHEN ISNULL(transactionamount, 0) < 0
                THEN 'Debit'
            END AS Debit
        ,totalbalance
        ,userid
    FROM transactions
    WHERE userid = 1
    

    【讨论】:

      【解决方案4】:

      您不想显示字符串“Credit”或“Debit”,而是在“credit”和“debit”两列中显示交易金额。此外,您缺少带有 CASE 表达式的关键字 END

      select
        transactionid,
        transactiondate,
        case when transactionamount > 0 then transactionamount end as credit,
        case when transactionamount < 0 then transactionamount end as debit,
        totalbalance,
        userid
      from transactions
      where userid = 1
      order by transactiondate;
      

      【讨论】:

        猜你喜欢
        • 2021-08-20
        • 2017-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-18
        • 2014-10-02
        • 1970-01-01
        相关资源
        最近更新 更多