将包含相同 Id 但不同数据的两行合并为一行

发布于 2024-12-02 17:27:21 字数 2013 浏览 0 评论 0原文

我有一个非常棘手的问题要问大家

假设我有以下表格

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 技术交流群。

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

发布评论

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

评论(3

你的背包 2024-12-09 17:27:21

这看起来像是 FULL OUTER JOIN 的直接应用程序:

SELECT COALESCE(ac1.id, ac2.id) AS id, ac1.Account_Type_1, ac2.Account_Type_2
  FROM (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_1
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 1) AS ac1
  FULL OUTER JOIN
       (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_2
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 2) AS ac2
    ON ac1.Id = ac2.Id;

如果您的 DBMS 不支持 FULL OUTER JOIN 但支持 LEFT OUTER JOIN,那么您可以使用:

SELECT ac0.id, ac1.Account_Type_1, ac2.Account_Type_2
  FROM (SELECT DISTINCT c.Customer_ID AS Id FROM Customer_Account AS c) AS ac0
  LEFT OUTER JOIN
       (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_1
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 1) AS ac1
    ON ac0.id = ac1.id
  LEFT OUTER JOIN
       (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_2
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 2) AS ac2
    ON ac0.Id = ac2.Id;

第一个子查询生成存在的客户 ID 列表;第二个生成帐户类型 1(支票)的列表;第三个生成帐户类型 2(储蓄)的列表。连接可确保每个帐户都得到正确识别。

This looks like a straight-forward application for a FULL OUTER JOIN:

SELECT COALESCE(ac1.id, ac2.id) AS id, ac1.Account_Type_1, ac2.Account_Type_2
  FROM (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_1
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 1) AS ac1
  FULL OUTER JOIN
       (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_2
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 2) AS ac2
    ON ac1.Id = ac2.Id;

If your DBMS does not support FULL OUTER JOIN but does support LEFT OUTER JOIN, then you can use:

SELECT ac0.id, ac1.Account_Type_1, ac2.Account_Type_2
  FROM (SELECT DISTINCT c.Customer_ID AS Id FROM Customer_Account AS c) AS ac0
  LEFT OUTER JOIN
       (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_1
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 1) AS ac1
    ON ac0.id = ac1.id
  LEFT OUTER JOIN
       (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_2
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 2) AS ac2
    ON ac0.Id = ac2.Id;

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.

智商已欠费 2024-12-09 17:27:21

我认为这个应该对你有帮助

1Sql 选择查询问题。合并行或者

应该对你有帮助

i think this should help you

1: Sql Select Query Question. Merge Rows or else

this should help you

酷遇一生 2024-12-09 17:27:21

向 ON 子句添加更多条件:

ON Account1.Id = CustAccount.Account_Type_Id and Account1.Account_Type_Id = 1

ON Account2.Id = CustAccount.Account_Type_Id and Account2.Account_Type_Id = 2

将导致输出包含客户持有的账户。如果他们只有一个帐户,则另一种帐户类型将为 NULL。

编辑:抱歉,我没有意识到您没有顾客表。您可以创建一个包含不同 Customer_Id 值列表的临时表,并将其用作 JOIN 中的第一个表。

select distinct Customer_Id into #Customers
    from Customer_Account

或者更直接的:

select distinct C.Customer_Id,
    ( select 'Checking' from Customer_Account where Customer_Id = C.CustomerId and Account_type_Id = 1 ) as Account_Type_1,
    ( select 'Savings' from Customer_Account where Customer_Id = C.CustomerId and Account_type_Id = 2 ) as Account_Type_2,
    from Customer_Account as C

Adding more conditions to your ON clauses:

ON Account1.Id = CustAccount.Account_Type_Id and Account1.Account_Type_Id = 1

ON Account2.Id = CustAccount.Account_Type_Id and Account2.Account_Type_Id = 2

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.

select distinct Customer_Id into #Customers
    from Customer_Account

Or the much more straightforward:

select distinct C.Customer_Id,
    ( select 'Checking' from Customer_Account where Customer_Id = C.CustomerId and Account_type_Id = 1 ) as Account_Type_1,
    ( select 'Savings' from Customer_Account where Customer_Id = C.CustomerId and Account_type_Id = 2 ) as Account_Type_2,
    from Customer_Account as C
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文