2014-11-24 64 views
0

我有一組數據如下。我需要公式列名「FEBRUARY」

表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 |   
+----+----------+------+-------++-------+-------+ 

我需要在列名公式 「二月」 。此公式將找到其匹配在片2

回答

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 
+0

列「Table2.ID_ENTRY」在選擇列表中無效。 - 得到這個錯誤 – 2014-11-24 02:09:16

+0

你有沒有檢查過它是否在組中 - 就像我在上面的回答中顯示的那樣?我確實有一個小的語法錯誤 - 現在已修復。 – 2014-11-24 02:10:58

+0

謝謝,我明白了。還有一個問題可以計算2個字段爲例(T2.Name&T2.LastName) – 2014-11-24 03:08:22

1

BUILD樣本數據

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 

SOLUTION

;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 

d因爲它不是在聚合函數或GROUP BY子句中包含ROP樣本數據

drop table table1 
drop table table2