Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Employee Department List</title>
<style>
#empdep {
font-family: Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
#empdep td, #empdep th {
border: 1px solid #ddd;
padding: 8px;
}
#empdep tr:nth-child(even){background-color: #eee;}
#empdep tr:hover {background-color: #ddd;}
#empdep th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: lightblue;
}
</style>
</head>
<body>
<table id="empdep"></table>
<script >
var data = [
{"empid":1,"deptname":"dept1"},
{"empid":1,"deptname":"dept2"},
{"empid":2,"deptname":"dept1"},
{"empid":2,"deptname":"dept2"},
{"empid":3,"deptname":"dept3"}
];
var tab, headerrow
tab = document.getElementById('empdep');
headerrow= tab.appendChild(document.createElement('tr'));
header = headerrow.appendChild(document.createElement('th'));
header.innerHTML = 'Employee';
var row,maxdep=0,emp=0,dep=0
data.forEach(function(e){
if (e.empid!==emp)
{
row = tab.appendChild(document.createElement('tr'));
emp = e.empid;
dep=0
cell = row.appendChild(document.createElement('td'));
cell.innerHTML = emp;
}
cell = row.appendChild(document.createElement('td'));
cell.innerHTML = e.deptname;
if (++dep>maxdep) {
maxdep=dep;
header = headerrow.appendChild(document.createElement('th'));
header.innerHTML = maxdep+'. department';
}
});
</script>
</body>
</html>
Declare @empdep as Table (empid int, deptname varchar(20));
Insert into @empdep values (1,'dept1'),(1,'dept2'),(2,'dept1'),(2,'dept2'),(3,'dpet3')
[b]Select * from @empdep for json path[/b]
select * from mytable
EMPID EMPDEPT
1 dept1
1 dept2
2 dept1
2 dept2
with temp (empdept) as (
select
distinct empdept
from mytable
),
temp_numbered (empdept, empid) as (
select
empdept, rownumber() over() empid
from temp
)
select
*
from temp_numbered
EMPDEPT EMPID
dept2 1
dept1 2
with temp (empdept) as (
select
distinct empdept
from mytable
),
temp_numbered (empdept, empid) as (
select
empdept, rownumber() over() empid
from temp
)
select
t1.empid, t1.empdept, t2.empdept
from mytable t1 inner join
temp_numbered t2 on t1.empid = t2.empid and
t1.empdept != t2.empdept
EMPID EMPDEPT EMPDEPT
1 dept1 dept2
2 dept2 dept1
[COLOR=#804040][b]create[/b][/color] [COLOR=#804040][b]or[/b][/color] replace [COLOR=#6a5acd]function[/color] MYFUNCTION ()
returns [COLOR=#6a5acd]table[/color](
EMPID [COLOR=#6a5acd]DECIMAL[/color] ([COLOR=#ff00ff]3[/color],[COLOR=#ff00ff]0[/color]),
EMPDEPTA [COLOR=#2e8b57][b]CHAR[/b][/color]([COLOR=#ff00ff]10[/color]),
EMPDEPTB [COLOR=#2e8b57][b]CHAR[/b][/color]([COLOR=#ff00ff]10[/color])
)
language SQL
specific MYUDTF01
modifies SQL data
no external action
deterministic
disallow parallel
[COLOR=#6a5acd]begin[/color]
declare FETCH_NR INT [COLOR=#6a5acd]default[/color] [COLOR=#ff00ff]0[/color];
declare MY_ID [COLOR=#6a5acd]decimal[/color] ([COLOR=#ff00ff]3[/color],[COLOR=#ff00ff]0[/color]);
declare MY_DEPT [COLOR=#2e8b57][b]char[/b][/color]([COLOR=#ff00ff]10[/color]);
declare MY_DEPTA [COLOR=#2e8b57][b]char[/b][/color]([COLOR=#ff00ff]10[/color]);
declare MY_DEPTB [COLOR=#2e8b57][b]char[/b][/color]([COLOR=#ff00ff]10[/color]);
declare EOF INT [COLOR=#6a5acd]default[/color] [COLOR=#ff00ff]0[/color];
declare C1 [COLOR=#6a5acd]cursor[/color] [COLOR=#6a5acd]for[/color]
[COLOR=#804040][b]select[/b][/color]
EMPID, EMPDEPT
[COLOR=#6a5acd]from[/color]
MYTABLE
[COLOR=#6a5acd]order[/color] [COLOR=#6a5acd]by[/color] EMPID, EMPDEPT
;
declare continue handler [COLOR=#6a5acd]for[/color] [COLOR=#804040][b]not[/b][/color] found
[COLOR=#804040][b]set[/b][/color] EOF = [COLOR=#ff00ff]1[/color];
[COLOR=#0000ff]-- temporary table in QTEMP [/color]
[COLOR=#804040][b]create[/b][/color] [COLOR=#804040][b]or[/b][/color] replace [COLOR=#6a5acd]table[/color] QTEMP.RESULTTAB (
EMPID [COLOR=#6a5acd]DECIMAL[/color] ([COLOR=#ff00ff]3[/color],[COLOR=#ff00ff]0[/color]),
EMPDEPTA [COLOR=#2e8b57][b]CHAR[/b][/color]([COLOR=#ff00ff]10[/color]),
EMPDEPTB [COLOR=#2e8b57][b]CHAR[/b][/color]([COLOR=#ff00ff]10[/color])
);
[COLOR=#0000ff]-- empty temporary table[/color]
[COLOR=#804040][b]delete[/b][/color] [COLOR=#6a5acd]from[/color] QTEMP.RESULTTAB;
open C1;
while EOF = [COLOR=#ff00ff]0[/color] do
[COLOR=#804040][b]set[/b][/color] FETCH_NR = FETCH_NR + [COLOR=#ff00ff]1[/color];
fetch C1 [COLOR=#6a5acd]into[/color] MY_ID, MY_DEPT;
[COLOR=#6a5acd]if[/color] MOD(FETCH_NR, [COLOR=#ff00ff]2[/color]) != [COLOR=#ff00ff]0[/color] [COLOR=#6a5acd]then[/color]
[COLOR=#0000ff]-- odd fetch[/color]
[COLOR=#804040][b]set[/b][/color] MY_DEPTA = MY_DEPT;
[COLOR=#6a5acd]else[/color]
[COLOR=#0000ff]-- on the even fetch do following[/color]
[COLOR=#804040][b]set[/b][/color] MY_DEPTB = MY_DEPT;
[COLOR=#804040][b]insert[/b][/color] [COLOR=#6a5acd]into[/color] QTEMP.RESULTTAB [COLOR=#6a5acd]values[/color](MY_ID, MY_DEPTA, MY_DEPTB);
[COLOR=#6a5acd]end[/color] [COLOR=#6a5acd]if[/color];
[COLOR=#6a5acd]end[/color] while;
close C1;
[COLOR=#6a5acd]return[/color]
[COLOR=#804040][b]select[/b][/color] EMPID, EMPDEPTA, EMPDEPTB [COLOR=#6a5acd]from[/color] QTEMP.RESULTTAB;
[COLOR=#6a5acd]end[/color]
;
EMPID EMPDEPT
1 dept1
1 dept2
2 dept1
2 dept2
10 deptX
10 deptY
20 deptA
20 deptB
100 dept100-01
100 dept100-02
200 dept200A
200 dept200B
select * from table(MYFUNCTION()) as T
EMPID EMPDEPTA EMPDEPTB
1 dept1 dept2
2 dept1 dept2
10 deptX deptY
20 deptA deptB
100 dept100-01 dept100-02
200 dept200A dept200B