Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a question about SQL Server.

Table: Dimemp
empkey |  empid | name |loc
1      |  100   | abc  |Hyd
2      |  102   | def  |chen
3      |  -1    | NA   |NA


Table2 : dimdept
deptkey  |  deptno  | deptname
1        |  10      |Hr
2        |  20      |ceo
3        |  -1      |NA


Transaction table: tranemp
empid    |  deptno   |  projectname |codes
100      |   20      |  test        |100
104      |   10      |  deve        |101
102      |   10      |  test        |107
106      |   40      |  sap         |103
101      |           |  Ca          |100
         |   10      |  manual      |201
100      |   60      |  quality     |100


Here the transedmp table data look into dimensions table if records match exact record then retrieve corresponding keys .if records not match then we need to fill defaule values -1 corresponding key value common columns for traneemp and dimension tables. empid(tranemp)=dimemp(empid) and tranemp(deptno)=dimdept(deptno) here I need to maintain scd type1 concept in Facttranemptable.

when I ran script at first time data look like in the below Based on tranemp table I want loade data in facttranemp and data look like below facttranemp table table structure like below : factkey is indenity(1,1),empkey--int,deptkey--int,projectname varchar(50),codes--int

factkey|empkey| deptkey| projectname   | codes  
1      |  1   |  2     |   test        |100
2      |  3   |  1     |   deve        |101
3      |  2   |  1     |   test        |107
4      |  3   |  3     |    sap        |103
5      |  3   |  3     |    Ca         |100
6      |  3   |  1     |   manual      |201
7      |  1   |  3     |  quality      |100


I tried like this:
SQL
insert into facttranemp
 select ISNULL(a.empkey, (select empkey from Dimemp where empid = -1)),
       ISNULL(b.deptkey, (select deptkey from dimdept where deptno = -1)), 
       c.projectname, c.codes 
from  traneemp c 
left join dimemp a on a.empid=c.empid
left join dimdept b on b.deptno=c.deptno


second day transaction table : tranemp data look like below
empid    |  deptno   |  projectname |codes
100      |   20      |   test123    |100 (Old)----here projectname changed test to test123
104      |   10      |   deve       |101
102      |   10      |  test        |107
106      |   40      |   sap        |105 (0ld) -------here codes changed from 103 t0 105
101      |           |  Ca          |100
         |   10      |  manual      |201
100      |   60      |  quality     |100
100      |   10      |  manual      |200 ( new) --new recordcame

Based on tranemp table I want loade data in facttranemp and data look like below
factkey|empkey| deptkey| projectname   | codes  
1      |  1   |  2     |   test123     |100
2      |  3   |  1     |   deve        |101
3      |  2   |  1     |   test        |107
4      |  3   |  3     |    sap        |105
5      |  3   |  3     |    Ca         |100
6      |  3   |  1     |   manual      |201
7      |  1   |  3     |  quality      |100
8      |  1   |  1     |   manual      |200

I am not able to implement scd type1 tranemp table with facttranemp and corresponding keys. I tried another way with merge statment like below

SQL
merge into facttranemp target 
       using( select ISNULL(a.empkey, (select empkey from Dimemp where empid = -1)) as empkey,
       ISNULL(b.deptkey, (select deptkey from dimdept where deptno = -1)) deptkey, 
       c.projectname, c.codes 
from  tranemp c 
left join dimemp a on a.empid=c.empid
left join dimdept b on b.deptno=c.deptno)source  on target.empkey=source.empkey and target.deptkey=source.deptkey
when not matched 
then 
insert ( empkey,deptkey,projectname,codes) values(source.empkey,source.deptkey,source.projectname,source.codes)
when matched then
update  set  
      target.projectname= source.projectname
      ,target.codes    =source.codes
;


That time I got error like below
Msg 8672, Level 16, State 1, Line 1
The MERGE statement attempted to UPDATE or DELETE the same row more than once. 
This happens when a target row matches more than one source row.
 A MERGE statement cannot UPDATE/DELETE the same row of the target table multiple times.
 Refine the ON clause to ensure a target row matches at most one source row, or use the GROUP BY clause to group the source rows.

Here I take unique combination empkey and deptkey .

I am not able to get expected result. Please tell me how to write query to achieve this task in SQL Server.
Posted
Updated 2-Dec-15 20:15pm
v3
Comments
_Asif_ 3-Dec-15 3:46am    
It seems like you are writing some ETL for BI analysis. Why can't you refresh your facttranemp by purging it entirely and regenerating on each day?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900