将包含相同 Id 但不同数据的两行合并为一行
我有一个非常棘手的问题要问大家
假设我有以下表格
表AccountType:
Id Type
1 Checking
2 Savings
表Customer_Account:
Customer_Id Account_Type_Id
450 1
450 2
451 1
表Customer_Account包含客户列表' ID 及其帐户类型 ID。 Account_Type_Id 是来自 AccountType.Id 的外键。
假设在 Customer_Account 表中,名为 Josh (id 450) 的客户可以同时拥有支票帐户和储蓄帐户,如上所示。我可以通过在 AccountType 表上进行两次 LEFT JOIN 来输出该客户的 ID 和帐户类型:
SELECT CustAccount.Customer_Id AS Id, Account1.Type AS Account_Type_1, Account2.Type AS Account_Type_2
FROM Customer_Account CustAccount
LEFT JOIN AccountType Account1
ON Account1.Id = CustAccount.Account_Type_Id
LEFT JOIN AccountType Account2
ON Account2.Id = CustAccount.Account_Type_Id
输出将是:
Id Account_Type_1 Account_Type_2
450 Checking Checking
450 Savings Savings
451 Checking Checking
我想要做的是,如果客户喜欢乔什(id 450)同时有一个支票帐户和一个储蓄帐户,我想将上面的两行数据输出到一行中,如下所示:
Id Account_Type_1 Account_Type_2
450 Checking Savings
而且,如果客户只有一种类型的帐户(就像有 id 的客户451),我只希望该类型的帐户出现在相应的列下,如下所示:
Id Account_Type_1 Account_Type_2
451 Checking
或者,如果 id 为 451 的客户只有一个储蓄帐户,则输出应该是:
Id Account_Type_1 Account_Type_2
451 Savings
我希望“Checking”仅出现在 Accoun_Type_1 和“Savings”下在 Account_Type_2 下。如果我执行 GROUP BY CustAccount.Customer_Id,我会得到以下信息:
Id Account_Type_1 Account_Type_2
450 Checking Checking
451 Checking Checking
任何专家的任何帮助都将非常感激。
谢谢。
I have a very tricky question for you all
Suppose I have the following tables
Table AccountType:
Id Type
1 Checking
2 Savings
Table Customer_Account:
Customer_Id Account_Type_Id
450 1
450 2
451 1
The table Customer_Account contains a list of Customers' Ids with their Account type Ids. The Account_Type_Id is a foreign key coming from the AccountType.Id.
Suppose in the Customer_Account table, a customer named Josh (id 450) can have both a checking and a savings account as shown above. I can output this customer with his id and type of account like so by having a LEFT JOIN twice on the AccountType table:
SELECT CustAccount.Customer_Id AS Id, Account1.Type AS Account_Type_1, Account2.Type AS Account_Type_2
FROM Customer_Account CustAccount
LEFT JOIN AccountType Account1
ON Account1.Id = CustAccount.Account_Type_Id
LEFT JOIN AccountType Account2
ON Account2.Id = CustAccount.Account_Type_Id
The output will be:
Id Account_Type_1 Account_Type_2
450 Checking Checking
450 Savings Savings
451 Checking Checking
What I'm trying to do is that if a customer like Josh (id 450) has both a checking and a savings account, I want to output the two rows of data above into one row like so:
Id Account_Type_1 Account_Type_2
450 Checking Savings
And also, if a customer has only one type of account (like customer with id 451 here), I want only that type of account to appear under the corresponding column like so:
Id Account_Type_1 Account_Type_2
451 Checking
Or if customer with id 451 had only a Savings account the ouput should be:
Id Account_Type_1 Account_Type_2
451 Savings
I want 'Checking' to only appear under Accoun_Type_1 and 'Savings' under Account_Type_2. If I do a GROUP BY CustAccount.Customer_Id, I get this:
Id Account_Type_1 Account_Type_2
450 Checking Checking
451 Checking Checking
Any help from any expert will be very appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这看起来像是 FULL OUTER JOIN 的直接应用程序:
如果您的 DBMS 不支持 FULL OUTER JOIN 但支持 LEFT OUTER JOIN,那么您可以使用:
第一个子查询生成存在的客户 ID 列表;第二个生成帐户类型 1(支票)的列表;第三个生成帐户类型 2(储蓄)的列表。连接可确保每个帐户都得到正确识别。
This looks like a straight-forward application for a FULL OUTER JOIN:
If your DBMS does not support FULL OUTER JOIN but does support LEFT OUTER JOIN, then you can use:
The first sub-query generates the list of customer IDs that exist; the second generates the list for account type 1 (Checking); the third generates the list for account type 2 (Saving). The joins ensure that every account is properly recognized.
我认为这个应该对你有帮助
1:Sql 选择查询问题。合并行或者
这应该对你有帮助
i think this should help you
1: Sql Select Query Question. Merge Rows or else
this should help you
向 ON 子句添加更多条件:
将导致输出包含客户持有的账户。如果他们只有一个帐户,则另一种帐户类型将为 NULL。
编辑:抱歉,我没有意识到您没有顾客表。您可以创建一个包含不同 Customer_Id 值列表的临时表,并将其用作 JOIN 中的第一个表。
或者更直接的:
Adding more conditions to your ON clauses:
will result in output that includes the account(s) held by the customer. If they only have one account then the other account type will be NULL.
Edit: Sorry, I failed to appreciate that you don't have a table of customers. You can create a temporary table with a list of distinct Customer_Id values and use that as the first table in your JOINs.
Or the much more straightforward: