简体   繁体   English

如何选择彼此对应的列值?

[英]How do I select column values to be corresponding to each other?

I have a resultset where for the same ID I have three rows because there are different FirstName, LastName and BranchName. 我有一个结果集,其中对于相同的ID我有三行,因为有不同的FirstName,LastName和BranchName。

For example: 例如:

ID  FirstName  LastName  BranchName Balance
101   Debra      Hays      Dayton   200
101   Daniel     Brinkman  Lynden   250
101   Daniel     Brinkman  HZBK     300

I want one row for this ID showing any BranchName but it should show corresponding FirstName and LastName for that BranchName and (sum of Balance) not random combination ie it can be Debra Hays with Dayton as branchname not Lynden as BranchName. 我想要此ID的一行显示任何BranchName,但应该为该BranchName显示相应的FirstName和LastName,并且(余额之和)不是随机组合,即它可以是Debra Hays,Dayton为Branchname,而不是Lynden作为BranchName。

Basically the FirstName, LastName and BranchName should be corresponding to each other not any first or lastname combination. 基本上,FirstName,LastName和BranchName应该彼此对应,而不是任何姓氏或姓氏组合。 I was wondering how can we achieve that? 我想知道我们该如何实现?

Expected output: 预期产量:

ID  FirstName LastName BranchName Sum(balance) 
101 Debra     Hays     Dayton     750 

Or 要么

ID  FirstName LastName BranchName Sum(balance) 
101 Daniel    Brinkman Lynden     750 
DECLARE @data TABLE(ID INT,FirstName VARCHAR(128),LastName VARCHAR(128),BranchName VARCHAR(128),Balance INT);
INSERT INTO @data(ID,FirstName,LastName,BranchName,Balance)VALUES
    (101,'Debra','Hays','Dayton',200),
    (101,'Daniel','Brinkman','Lynden',250),
    (101,'Daniel','Brinkman','HZBK',300);

;WITH cte AS (
    SELECT
        ID,
        FirstName,
        LastName,
        BranchName,
        rn=ROW_NUMBER() OVER(PARTITION BY ID ORDER BY FirstName,LastName,BranchName),
        Balance=SUM(Balance) OVER (PARTITION BY ID)
    FROM
        @data
)
SELECT
    ID,
    FirstName,
    LastName,
    BranchName,
    Balance
FROM
    cte
WHERE
    rn=1;

Output: 输出:

+-----+-----------+----------+------------+---------+
| ID  | FirstName | LastName | BranchName | Balance |
+-----+-----------+----------+------------+---------+
| 101 | Daniel    | Brinkman | HZBK       |     750 |
+-----+-----------+----------+------------+---------+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我有 3 个其他对应列时,如何获得特定对应列的最大值? - How do I get max value for a specific corresponding column when I have 3 other corresponding columns? Athena 获取每组中的最小值和对应的其他列值 - Athena get the minimum value in each group and corresponding other column values 如何为其他列中的每个值选择具有5个不同值的行? - How do I select rows with 5 distinct values for each value in other columns? 我怎样才能 select 行对应于 PostgreSQL 中另一列的最高值的唯一列值对? - How can I select rows corresponding to the unique pair of column values with the highest value of another column in PostgreSQL? 从一列中只为另一个列中的每个值选择一个值,以及对应的其他列值 - Select only one value from a column for each value in another column, together with corresponding other columns value 在标准SQL中,如何选择行,以便对于一列中的每个唯一值,另一列中的所有值都是指定值? - In standard SQL, how do I select rows such that for each unique value in one column, all of the values in another column are a specified value? 如何在 SQL 中将一列堆叠在一起? - How do I stack a column next to each other in SQL? 如何在SQL的相应列中选择不同值的计数? - How to select count of different values in the corresponding column in SQL? 如何选择对应行中前 5 个列的五个值? - How to select five values of a column with the top 5 in their corresponding row? 如何按月计算列中的值? - How do I count the values in a column by each month?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM