我有一个表格,可以为客户提供多种帐户类型。这些帐户有各种帐户类型和数据,数据库是MS SQL Server
例如:
Account_id Account_Name, Activated_Date, Account_Type,Account_Balance,Pending_Withdrawals.... etc
1 Samuuel Lemin 03/01/2009 Savings
2 Samuel Lemin 01/12/2013 Way2Save 2,000, 231,
3 Samuel Lemin 01/12/2013 Checking
4 David Kay 11/08/2005 Savings
5 David Kay 11/08/2004 Way2Save 1200 0.0
6 David Kay 09/05/2003 Checking 8000 1000
...
我想创建一个带有SQL Select Query的报告,如果Account_Balance,Pending_Withdrwals的值不为null,但是如果客户没有支票账户,即选择了被视为主账户的支票账户的所有列。列值Account_Balance,Pending_Withdrwals等为空检查Account_Type(即主要)然后
选择具有帐户信息的任何其他帐户,即并非所有这些列都为空。目标是使用空格在case语句中连接帐户详细信息。如果不能找到所有列都不为空的帐户,则在选择查询中返回''
“SELECT查询”的算法应为:
if a type=='Checking'
then concat(Account_Name, Account_Name,Activated_Date... etc.)
else if here is an account with account details
concat(Account_Name,...
空列应该在选择查询中连接为“。”
SQL select查询应如下所示:
select case CustomerAccount=
when exists(
select Account_Balance,Pending_Withdrawals from Accounts where accountType='Checking'
)
then
concat(select top 1.Account_Id from Account where Account_Type='Checking',
select top 1.Account_Name from Account where Account_Type='Checking',
other columns...
when exists(
select Account_Balance,Pending_Withdrawals from Accounts where accountType='Savings'
)
then
concat(....)
when exists(
select Account_Balance,Pending_Withdrawals from Accounts where accountType='Way2Save'
)
then concat(...)
如何编写返回串行列的单行的select case查询作为String,其中Account_Type不是所有列值都为null或''如果所有Account_Types列都为null?
例如,根据上面的表格,我想从Samuel Lemin的select查询返回串联的字符串
'2 Samuel Lemin 01/12/2013 Way2Save 2,000,231'
因为对于Samuel Lemin,Account_Balance,Pending_Withdrwals在检查时为空
David Kay。
'David Kay 09/05/2003检查8000 1000'
因为支票帐户类型具有Account_Balance和Pending_Withdrawal值,而且它是我们想要首先显示的主帐户