我需要列名“FEBRUARY”中的公式

时间:2014-11-24 00:42:05

标签: excel-formula

我有一组数据如下。

第1页

                +------+-------+
                |    JANUARY   |
                +------+-------+
+----+----------+------+-------+
| ID | NAME     |COUNT | PRICE |
+----+----------+------+-------+
| 1  |  ALFRED  |  11  |  150  |
| 2  |  ARIS    |  22  |  120  |
| 3  |  JOHN    |  33  |  170  |
| 4  |  CHRIS   |  22  |  190  |
| 5  |  JOE     |  55  |  120  |
| 6  |  ACE     |  11  |  200  |
+----+----------+------+-------+

SHEET2

+----+----------+------+-------+
| ID | NAME     |COUNT | PRICE |
+----+----------+------+-------+
| 1  |  CHRIS   |  13  |  123  |
| 2  |  ACE     |  26  |  165  |
| 3  |  JOE     |  39  |  178  |
| 4  |  ALFRED  |  21  |  198  |
| 5  |  JOHN    |  58  |  112  |
| 6  |  ARIS    |  11  |  200  |
+----+----------+------+-------+

结果在sheet1中应如下所示:

                +------+-------++------+-------+
                |    JANUARY   |   FEBRUARY    |
                +------+-------++------+-------+
+----+----------+------+-------++-------+-------+
| ID | NAME     |COUNT | PRICE || COUNT | PRICE |
+----+----------+------+-------++-------+-------+
| 1  |  ALFRED  |  11  |  150  ||  21   |  198  |       
| 2  |  ARIS    |  22  |  120  ||  11   |  200  |       
| 3  |  JOHN    |  33  |  170  ||  58   |  112  |       
| 4  |  CHRIS   |  22  |  190  ||  13   |  123  |       
| 5  |  JOE     |  55  |  120  ||  39   |  178  |       
| 6  |  ACE     |  11  |  200  ||  26   |  165  |         
+----+----------+------+-------++-------+-------+

我需要列名为“FEBRUARY”的公式。这个公式将在表2中找到它的匹配

2 个答案:

答案 0 :(得分:1)

构建示例数据

create table table1(
    id int,
    id_entry varchar(10),
    tag int,
    tag2 int
)
create table table2(
    id int,
    name varchar(50),
    lastname varchar(50),
    age int,
    tel int
)
insert into table1
select 1, 'A1', 11, 12 union all
select 2, 'C2', 22, 13 union all
select 3, 'S5', 33, 14 union all
select 4, 'C2', 22, 13 union all
select 5, 'B6', 55, 16 union all
select 6, 'A1', 11, 12

insert into table2
select 1, 'ALFRED', 'DAVE', 21, 555 union all
select 2, 'FRED', 'SMITH', 22, 666 union all
select 3, 'MANNY', 'PAC', 23, 777 union all
select 4, 'FRED', 'DAVE', 22, 666 union all
select 5, 'JOHN', 'SMITH', 25, 999 union all
select 6, 'ALFRED', 'DAVE', 21, 555

<强>解

;with cte as(
    select
        t1.id_entry,
        t1.tag,
        t1.tag2,
        t2.name,
        t2.lastname,
        t2.age,
        t2.tel,
        cc = count(*) over(partition by t1.id_entry),
        rn = row_number() over(partition by t1.id_entry order by t2.lastname desc)
    from table1 t1
    inner join table2 t2
        on t2.id = t1.id
)
select
    id_entry,
    tag,
    tag2,
    name,
    lastname,
    age,
    tel
from cte
where 
    cc > 1
    and rn = 1

DROP SAMPLE DATA

drop table table1
drop table table2

答案 1 :(得分:0)

试试这个

SELECT T2.ID_ENTRY, T1.TAG, T1.TAG2, T2.Name, T2.LastName, T2.Age, T2.Tel
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.ID = T2.ID
GROUP BY T2.ID_ENTRY, T1.TAG, T1.TAG2, T2.Name, T2.LastName, T2.Age, T2.Tel
HAVING Count(T2.ID_ENTRY) > 1