在 sql select 语句中使用 case

发布于 2024-09-24 03:44:47 字数 802 浏览 1 评论 0原文

考虑一个具有列金额的表,

Amount
-1235.235
1356.45
-133.25
4565.50
5023
-8791.25

我希望我的结果窗格是这样的,

Debit   Credit
  0     -1235.235
1356.45  0 
  0     -133.25

这是我的存储过程,

USE [HotelBI_CustomDB]
GO
  SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[SP_GetJVReport](
@p_FromDate datetime,
@p_ToDate datetime
)
AS
BEGIN
select jv.AccountNo,jv.AccountNoTitle,(select JV_GroupsHead.GroupTitle
    from JV_GroupsHead where JV_GroupsHead.Id=jv.GroupId) as 'GroupName'
,jv.Revenue --" This column i need to check and have to split it into two 
               column "credeit,debit""

from JVFunction1(@p_FromDate,@p_ToDate) as jv

END

如何编写一个选择语句,使其使用像 amount>=0 这样的情况作为信用和 amount <0 作为借方?

Consider a table with a column amount,

Amount
-1235.235
1356.45
-133.25
4565.50
5023
-8791.25

I want to my result pane to be like this,

Debit   Credit
  0     -1235.235
1356.45  0 
  0     -133.25

Here is my stored procedure,

USE [HotelBI_CustomDB]
GO
  SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[SP_GetJVReport](
@p_FromDate datetime,
@p_ToDate datetime
)
AS
BEGIN
select jv.AccountNo,jv.AccountNoTitle,(select JV_GroupsHead.GroupTitle
    from JV_GroupsHead where JV_GroupsHead.Id=jv.GroupId) as 'GroupName'
,jv.Revenue --" This column i need to check and have to split it into two 
               column "credeit,debit""

from JVFunction1(@p_FromDate,@p_ToDate) as jv

END

How to write a select statement such that it uses a case like amount>=0 as credit and amount <0 as debit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葵雨 2024-10-01 03:44:47

根据您的具体需求进行修改

SELECT
   CASE WHEN Amount < 0 THEN ABS(Amount) ELSE NULL AS Debit,
   CASE WHEN Amount >= 0 THEN Amount ELSE NULL AS Credit
FROM
   SomeTable

Modify as needed for your exact needs

SELECT
   CASE WHEN Amount < 0 THEN ABS(Amount) ELSE NULL AS Debit,
   CASE WHEN Amount >= 0 THEN Amount ELSE NULL AS Credit
FROM
   SomeTable
傲世九天 2024-10-01 03:44:47

您的意思是这样,一个单独的列来指示金额的类型吗?

SELECT Amount, CASE WHEN Amount < 0 THEN 'Debit' ELSE 'Credit' END AS Type
FROM SomeTable

Do you mean like this, a separate column to indicate the type of the amount?

SELECT Amount, CASE WHEN Amount < 0 THEN 'Debit' ELSE 'Credit' END AS Type
FROM SomeTable
丶情人眼里出诗心の 2024-10-01 03:44:47

我可能会创建一个用户定义的函数来使这更容易一些。

例如。

create function dbo.CreditOrDebit (@amount decimal(9,2), @type char(6))
returns decimal(9,2)
as
begin
declare @output decimal(9,2);
select @output =
    case @type
    when 'debit' then
        case 
            when @amount < 0.00 then ABS(@amount) 
            else 0
        end
    else
        case 
            when @amount >= 0.00 then ABS(@amount) 
            else 0
        end
    end;
    return @output;
end

然后在 Select 语句中使用它,如下所示:

select jv.AccountNo,jv.AccountNoTitle,(select JV_GroupsHead.GroupTitle
    from JV_GroupsHead where JV_GroupsHead.Id=jv.GroupId) as 'GroupName'
,dbo.CreditOrDebit(jv.Revenue,'debit') as debit
,dbo.CreditOrDebit(jv.Revenue,'credit') as credit

I'd probably create a User-Defined Function to make this a bit easier.

eg.

create function dbo.CreditOrDebit (@amount decimal(9,2), @type char(6))
returns decimal(9,2)
as
begin
declare @output decimal(9,2);
select @output =
    case @type
    when 'debit' then
        case 
            when @amount < 0.00 then ABS(@amount) 
            else 0
        end
    else
        case 
            when @amount >= 0.00 then ABS(@amount) 
            else 0
        end
    end;
    return @output;
end

Then use it in your Select statement like this:

select jv.AccountNo,jv.AccountNoTitle,(select JV_GroupsHead.GroupTitle
    from JV_GroupsHead where JV_GroupsHead.Id=jv.GroupId) as 'GroupName'
,dbo.CreditOrDebit(jv.Revenue,'debit') as debit
,dbo.CreditOrDebit(jv.Revenue,'credit') as credit
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文