在 sql select 语句中使用 case
考虑一个具有列金额的表,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您的具体需求进行修改
Modify as needed for your exact needs
您的意思是这样,一个单独的列来指示金额的类型吗?
Do you mean like this, a separate column to indicate the type of the amount?
我可能会创建一个用户定义的函数来使这更容易一些。
例如。
然后在 Select 语句中使用它,如下所示:
I'd probably create a User-Defined Function to make this a bit easier.
eg.
Then use it in your Select statement like this: