I need a query that will return all women within the group.
Example data table (Oracle 11g):
with t as (
select 'K' as GROUP_NAME, 1 as ELEMENT_ID from dual
union
select 'K' as GROUP_NAME, 2 as ELEMENT_ID from dual
union
select 'K' as GROUP_NAME, 3 as ELEMENT_ID from dual
union
select 'L' as GROUP_NAME, 1 as ELEMENT_ID from dual
union
select 'L' as GROUP_NAME, 2 as ELEMENT_ID from dual
union
select 'L' as GROUP_NAME, 3 as ELEMENT_ID from dual
union
select 'P' as GROUP_NAME, 1 as ELEMENT_ID from dual
union
select 'P' as GROUP_NAME, 2 as ELEMENT_ID from dual
union
select 'P' as GROUP_NAME, 3 as ELEMENT_ID from dual
)
select * from t
K 1
K 2
K 3
L 1
L 2
L 3
P 1
P 2
P 3
I need to generate a tree with all the combinations without repeating them in groups (group K, L, P)
A tree must not contain a group of trees, i.e. there must not be a tree with such a branch: K1->L1->K2
K1
+-- L1
+-- P1
+-- P2
+-- P3
+-- L2
+-- P1
+-- P2
+-- P3
+-- L3
+-- P1
+-- P2
+-- P3
K2
+-- L1
+-- P1
+-- P2
+-- P3
+-- L2
+-- P1
+-- P2
+-- P3
+-- L3
+-- P1
+-- P2
+-- P3
K3
+-- L1
+-- P1
+-- P2
+-- P3
+-- L2
+-- P1
+-- P2
+-- P3
+-- L3
+-- P1
+-- P2
+-- P3
L1
+-- K1
+-- P1
+-- P2
+-- P3
+-- K2
....
SELECT ltrim(sys_connect_by_path(group_name || element_id,'-'),'-') FROM t
START WITH t.group_name = 'K'
CONNECT BY ( t.group_name = 'L' AND PRIOR t.group_name = 'K' ) OR ( t.group_name = 'P'
AND PRIOR t.group_name = 'L');
+-----------+
| TREE_PATH |
+-----------+
| K1 |
| K1-L1 |
| K1-L1-P1 |
| K1-L1-P2 |
| K1-L1-P3 |
| K1-L2 |
| K1-L2-P1 |
| K1-L2-P2 |
| K1-L2-P3 |
| K1-L3 |
| K1-L3-P1 |
| K1-L3-P2 |
| K1-L3-P3 |
| K2 |
| K2-L1 |
| K2-L1-P1 |
| K2-L1-P2 |
| K2-L1-P3 |
| K2-L2 |
| K2-L2-P1 |
| K2-L2-P2 |
| K2-L2-P3 |
| K2-L3 |
| K2-L3-P1 |
| K2-L3-P2 |
| K2-L3-P3 |
| K3 |
| K3-L1 |
| K3-L1-P1 |
| K3-L1-P2 |
| K3-L1-P3 |
| K3-L2 |
| K3-L2-P1 |
| K3-L2-P2 |
| K3-L2-P3 |
| K3-L3 |
| K3-L3-P1 |
| K3-L3-P2 |
| K3-L3-P3 |
+-----------+
If you didn't want a tree and just wanted all combinations, that would be a 'CROSS JOIN'. Like so:
select * from t k CROSS JOIN t l CROSS JOIN t p
where k.group_name = 'K'
AND l.group_name = 'L'
and p.group_name = 'P'
+------------+------------+--------------+--------------+--------------+--------------+
| GROUP_NAME | ELEMENT_ID | GROUP_NAME_1 | ELEMENT_ID_1 | GROUP_NAME_2 | ELEMENT_ID_2 |
+------------+------------+--------------+--------------+--------------+--------------+
| K | 1 | L | 1 | P | 1 |
| K | 1 | L | 1 | P | 2 |
| K | 1 | L | 1 | P | 3 |
| K | 1 | L | 2 | P | 1 |
| K | 1 | L | 2 | P | 2 |
| K | 1 | L | 2 | P | 3 |
| K | 1 | L | 3 | P | 1 |
| K | 1 | L | 3 | P | 2 |
| K | 1 | L | 3 | P | 3 |
| K | 2 | L | 1 | P | 1 |
| K | 2 | L | 1 | P | 2 |
| K | 2 | L | 1 | P | 3 |
| K | 2 | L | 2 | P | 1 |
| K | 2 | L | 2 | P | 2 |
| K | 2 | L | 2 | P | 3 |
| K | 2 | L | 3 | P | 1 |
| K | 2 | L | 3 | P | 2 |
| K | 2 | L | 3 | P | 3 |
| K | 3 | L | 1 | P | 1 |
| K | 3 | L | 1 | P | 2 |
| K | 3 | L | 1 | P | 3 |
| K | 3 | L | 2 | P | 1 |
| K | 3 | L | 2 | P | 2 |
| K | 3 | L | 2 | P | 3 |
| K | 3 | L | 3 | P | 1 |
| K | 3 | L | 3 | P | 2 |
| K | 3 | L | 3 | P | 3 |
+------------+------------+--------------+--------------+--------------+--------------+
Related
I have a table with 3 columns:
| A | B | C |
--------------
| 1 | C1 | N |
| 1 | C1 | N |
| 1 | C1 | N |
| 2 | C1 | N |
| 2 | C1 | Y |
| 2 | C1 | N |
| 3 | C1 | N |
| 3 | C2 | N |
| 3 | C3 | N |
| 4 | C1 | Y |
| 4 | C2 | N |
| 4 | C2 | N |
| 4 | C3 | N |
I want to group by A as below:
| A | B | C |
--------------
| 1 | C1 | N |
| 2 | C1 | Y |
| 3 | C3 | N |
| 4 | C3 | N |
For each unique A, keep row with maximum B and C in front of that B. And if all B for a unique A are same, but C has "Y" then then keep this row.
The code is in MS Access and I need the very SQL query.
How can I write an SQL query (DB2) that will run on this table:
| A | B | C | V |
+---+---+---+----+
| | | | |
| 1 | 1 | 1 | k1 |
| | | | |
| 1 | 1 | 2 | k1 |
| | | | |
| 1 | 2 | 3 | k2 |
| | | | |
| 2 | 3 | 4 | k2 |
| | | | |
| 1 | 2 | 3 | k3 |
| | | | |
| 1 | 3 | 5 | k3 |
| | | | |
| 1 | 4 | 6 | k3 |
+---+---+---+----+
and produce this result
+---+---+---+----+
| A | B | C | V |
+---+---+---+----+
| | | | |
| 1 | 1 | 2 | k1 |
| | | | |
| 2 | 3 | 4 | k2 |
| | | | |
| 1 | 4 | 6 | k3 |
+---+---+---+----+
that is it will select rows based on a max of a "tuple" (A,B,C) in a group:
or for two rows R1, R2 :
if R1.A <> R2.A return Row where A = Max(R1.A,R2.A)
if R2.B <> R2.B return Row where B = Max(R1.B,R2.B)
return Row where C = Max(R1.C,R2.C)
I think row_number() does what you want -- if by "group" you mean V:
select t.*
from (select t.*,
row_number() over (partition by v order by a desc, b desc, c desc) as seqnum
from t
) t
where seqnum = 1;
Given a table of roles, companies and a employee table where we store for each employee which role he/she has at each company.
I'm trying to create a view which indicates for each combination of role and company and employee by a ‘Y’ or ‘N’ in the “checked_yn” column, whether this employee has this role at this company.
company table
----------------
|ID | name |
-----------------
| 1 | A |
| 2 | B |
-----------------
roles table
-------------
|ID | role |
-------------
| 1 | X |
| 2 | Y |
| 3 | Z |
-------------
employee table
----------------------------------------------
|ID | company_id | role_id | employee_log_id |
---------------------------------------------|
| 1 | 1 | 1 | 1 |
| 2 | 1 | 2 | 1 |
| 3 | 2 | null | 1 |
----------------------------------------------
The desired outcome is this:
EMPLOYEE_ROLES_VW view
------------------------------------------------------------------------
|Id |company_id | role_id | Checked_yn | employee_id | employee_log_id |
|----------------------------------------------------------------------|
| 1 | 1 | 1 | Y | 1 | 1 |
| 2 | 1 | 2 | Y | 2 | 1 |
| 3 | 1 | 3 | N | null | 1 |
| 4 | 2 | 1 | N | null | 1 |
| 5 | 2 | 2 | N | null | 1 |
| 6 | 2 | 3 | N | null | 1 |
------------------------------------------------------------------------
This is my current query:
with ROLES_X_COMP as (SELECT ROL.ID AS X_ROLE_ID,
COM.ID AS X_COMPANY_ID,
FROM ROLES ROL
CROSS JOIN COMPANY COM)
SELECT ROWNUM AS ID,
EMP.ID AS SMCR_EMPLOYEE_ID,
EMP.EMPLOYEE_LOG_ID AS EMPLOYEE_LOG_ID,
ROLES_X_COMP.X_ROLE_ID ,
EMP.ROLE_ID AS ROLE_ID,
ROLES_X_COMP.X_COMPANY_ID,
EMP.COMPANY_ID AS COMPANY_ID,
CASE
WHEN ROLES_X_COMP.X_ROLE_ID = SE.ROLE_ID AND ROLES_X_COMP.X_COMPANY_ID =
SE.COMPANY_ID THEN 'Y'
ELSE 'N' END AS CHECKED_YN
FROM ROLES_X_COMP
LEFT OUTER JOIN EMPLOYEE EMP ON ROLES_X_COMP.X_COMPANY_ID = EMP.COMPANY_ID
Because of the join on EMPLOYEE “finds” the company with id=1 twice it joins twice with the cross join of role and company table. So I'm getting this result:
------------------------------------------------------------------------
|Id |company_id | role_id | Checked_yn | employee_id | employee_log_id |
|----------------------------------------------------------------------|
| 1 | 1 | 1 | Y | 1 | 1 |
| 2 | 1 | 2 | N | 1 | 1 |
| 3 | 1 | 3 | N | 1 | 1 |
| 4 | 1 | 1 | N | 2 | 1 |
| 5 | 1 | 2 | Y | 2 | 1 |
| 6 | 1 | 3 | N | 2 | 1 |
| 7 | 2 | 1 | N | 3 | 1 |
| 8 | 2 | 2 | N | 3 | 1 |
| 9 | 2 | 3 | N | 3 | 1 |
------------------------------------------------------------------------
I think a JOIN might be the wrong option here and a UNION more appropriate but I can't figure it out.
Use a partitioned outer join:
Query:
SELECT ROWNUM AS id,
e.company_id,
r.id AS role_id,
NVL2( e.role_id, 'Y', 'N' ) AS CheckedYN,
e.role_id AS employee_id,
e.employee_log_id
FROM roles r
LEFT OUTER JOIN
employee e
PARTITION BY ( e.company_id, e.employee_log_id )
ON ( r.id = e.role_id )
or (depending on how you want to partition and join the data):
SELECT ROWNUM AS id,
c.id AS company_id,
r.id AS role_id,
NVL2( e.role_id, 'Y', 'N' ) AS CheckedYN,
e.role_id AS employee_id,
e.employee_log_id
FROM roles r
CROSS JOIN
company c
LEFT OUTER JOIN
employee e
PARTITION BY ( e.employee_log_id )
ON ( c.id = e.company_id AND r.id = e.role_id )
Output:
Both output the same for the test data but may give differing results depending on your actual data.
ID | COMPANY_ID | ROLE_ID | CHECKEDYN | EMPLOYEE_ID | EMPLOYEE_LOG_ID
-: | ---------: | ------: | :-------- | ----------: | --------------:
1 | 1 | 1 | Y | 1 | 1
2 | 1 | 2 | Y | 2 | 1
3 | 1 | 3 | N | null | 1
4 | 2 | 1 | N | null | 1
5 | 2 | 2 | N | null | 1
6 | 2 | 3 | N | null | 1
db<>fiddle here
AND ROLES_X_COMP.X_ROLE_ID = EMP.ROLE_ID
Is missing at the end of your query
But the outcome will be
EMPLOYEE_ROLES_VW view
------------------------------------------------------------------------
|Id |company_id | role_id | Checked_yn | employee_id | employee_log_id |
|----------------------------------------------------------------------|
| 1 | 1 | 1 | Y | 1 | 1 |
| 2 | 1 | 2 | Y | 2 | 1 |
| 3 | 1 | 3 | N | null | null |
| 4 | 2 | 1 | N | null | null |
| 5 | 2 | 2 | N | null | null |
| 6 | 2 | 3 | N | null | null |
------------------------------------------------------------------------
I'm having issues on building a tree starting from a table.
I'm following the tutorial from D.Fontaine but something is wrong in my query.
here is a subset of rows from this table:
abs=# select * from myproj_loparentrelation where root_id = '57b2e67b-5862-499a-a471-0f2f6b23440e' order by level ASC;
id | root_id | level | display_sequence | lo_id | parent_id
--------+--------------------------------------+-------+------------------+--------------------------------------+--------------------------------------
90468 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 2 | 1 | 5c51558b-1180-495f-88c3-f7af49bafcf3 | 57b2e67b-5862-499a-a471-0f2f6b23440e
55209 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 2 | 16 | 3962f997-9e14-4cac-a95f-dc20c077a531 | 57b2e67b-5862-499a-a471-0f2f6b23440e
14890 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 3 | 17 | 0f335cd1-74d0-4a5e-831d-1aba0c0dc396 | 3962f997-9e14-4cac-a95f-dc20c077a531
116513 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 3 | 4 | 78b6cac3-307f-449a-bb3c-fe4442f4d1e8 | 5c51558b-1180-495f-88c3-f7af49bafcf3
122691 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 3 | 6 | 7ecb01da-078a-49b9-b488-86dc6bb24ab4 | 5c51558b-1180-495f-88c3-f7af49bafcf3
95729 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 3 | 14 | 62e889fe-b0e6-4fd4-a89f-512a1f31e210 | 5c51558b-1180-495f-88c3-f7af49bafcf3
141390 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 3 | 2 | 932353b7-bb63-4bad-87e7-70bcebcbbc98 | 5c51558b-1180-495f-88c3-f7af49bafcf3
96022 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 3 | 26 | 6346657d-a41e-45ab-87bc-abf32a8c616c | 3962f997-9e14-4cac-a95f-dc20c077a531
61116 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 22 | 3f07cece-0a7c-411e-b7d1-0370d18c6405 | 0f335cd1-74d0-4a5e-831d-1aba0c0dc396
72097 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 5 | 49c43039-84e6-4f36-a8a1-5f57ad1bc67e | 78b6cac3-307f-449a-bb3c-fe4442f4d1e8
81260 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 15 | 51e9ce01-9c90-437a-b17c-8b0b19c3a2b4 | 62e889fe-b0e6-4fd4-a89f-512a1f31e210
84177 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 12 | 553150c1-ff43-44e0-a52d-d6361813de73 | 7ecb01da-078a-49b9-b488-86dc6bb24ab4
89331 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 24 | 5b1ede40-66a6-4320-be79-69f0a4d0340e | 0f335cd1-74d0-4a5e-831d-1aba0c0dc396
111377 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 21 | 73f05e3c-012e-4b41-acc0-14791c83f710 | 0f335cd1-74d0-4a5e-831d-1aba0c0dc396
114868 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 33 | 778bf627-e83f-4385-bbb9-3537d45d18b6 | 6346657d-a41e-45ab-87bc-abf32a8c616c
129785 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 27 | 8650b402-bb6f-4be2-9585-800d189ded83 | 6346657d-a41e-45ab-87bc-abf32a8c616c
151263 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 10 | 9f29806f-d04d-4952-9743-9b2eedeecfee | 7ecb01da-078a-49b9-b488-86dc6bb24ab4
152546 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 34 | a0958fb5-21c2-440b-8dc9-02659b34691e | 6346657d-a41e-45ab-87bc-abf32a8c616c
158562 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 13 | a7f96063-e78e-4248-a512-68d726b0ff09 | 7ecb01da-078a-49b9-b488-86dc6bb24ab4
165779 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 7 | aeacbddd-37d6-4eda-9695-bace3406f415 | 7ecb01da-078a-49b9-b488-86dc6bb24ab4
177880 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 28 | bc18337c-899c-416b-91e4-93e4c7699d58 | 6346657d-a41e-45ab-87bc-abf32a8c616c
188650 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 11 | c6a82ea7-2156-492e-9f0f-a73c36afba70 | 7ecb01da-078a-49b9-b488-86dc6bb24ab4
204117 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 30 | d7924927-55c3-4085-bdd6-5420ef7162a9 | 6346657d-a41e-45ab-87bc-abf32a8c616c
10528 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 19 | 0be4c51c-3d52-499e-98fb-3dac9b2b1e49 | 0f335cd1-74d0-4a5e-831d-1aba0c0dc396
223079 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 3 | ebb92498-a730-4b61-b1dd-44696aae4a8e | 932353b7-bb63-4bad-87e7-70bcebcbbc98
11987 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 29 | 0ccd9922-85fa-45de-bd87-257b77e2a5a3 | 6346657d-a41e-45ab-87bc-abf32a8c616c
22808 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 32 | 17f12743-e0c4-4f32-a1d6-0e680fa762d5 | 6346657d-a41e-45ab-87bc-abf32a8c616c
28627 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 31 | 1eb6653a-de11-42a2-9766-4222996ff75e | 6346657d-a41e-45ab-87bc-abf32a8c616c
35421 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 20 | 26418eb9-22d4-41f4-a117-ae1884987080 | 0f335cd1-74d0-4a5e-831d-1aba0c0dc396
36110 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 25 | 27198280-5cff-462c-87be-5fb8b6a7b309 | 0f335cd1-74d0-4a5e-831d-1aba0c0dc396
46312 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 23 | 319838f6-aebc-460e-ae77-009858f2f858 | 0f335cd1-74d0-4a5e-831d-1aba0c0dc396
48878 | 57b2e67b-5862-499a-a471-0f2f6b23440e | 4 | 9 | 33bf5a6f-5b72-4bf0-9e1b-a68f7ad69a64 | 7ecb01da-078a-49b9-b488-86dc6bb24ab4
root is 57b2e67b-5862-499a-a471-0f2f6b23440e
under the root, there are two children 5c51558b-1180-495f-88c3-f7af49bafcf3 and 3962f997-9e14-4cac-a95f-dc20c077a531
each one of those has other children and so on.
the expected result would be something like:
[
{'Name': '57b2e67b-5862-499a-a471-0f2f6b23440e',
'Sub Classes': [
{'Name': '5c51558b-1180-495f-88c3-f7af49bafcf3',
'Subclasses': [
]},
{'Name': '3962f997-9e14-4cac-a95f-dc20c077a531',
'Subclasses': [
{'Name': '5c51558b-1180-495f-88c3-f7af49bafcf3',
'Subclasses': [
...
]}
]}
}
]
but it's not so I'm messing up something. here's the query:
with recursive rels_from_parents as
(
select sel.lo_id, '{}'::uuid[] as parents, sel.level as _level
from (select * from myproj_loparentrelation where root_id = '57b2e67b-5862-499a-a471-0f2f6b23440e') as sel
where sel.parent_id = sel.root_id
union all
select c.lo_id, parents || c.parent_id, c.level as _level
from rels_from_parents p
join myproj_loparentrelation c
on c.parent_id = p.lo_id
where not c.lo_id = any(parents)
),
rels_from_children as
(
select c.parent_id,
json_agg(jsonb_build_object('lo_id', c.lo_id::text))::jsonb as js
from rels_from_parents tree
join myproj_loparentrelation c using(lo_id)
where level > 0 and not lo_id = any(parents)
group by c.parent_id
union all
select c.parent_id,
jsonb_build_object('Name', c.lo_id::text)
|| jsonb_build_object('Sub Classes', js) as js
from rels_from_children tree
join myproj_loparentrelation c on c.lo_id = tree.parent_id
)
select jsonb_pretty(jsonb_agg(js))
from rels_from_children;
can anyone provide help with that?
demo: db<>fiddle
WITH RECURSIVE tree(lo_id, ancestor, child, path, json) AS (
SELECT
t1.lo_id,
NULL::text,
t2.lo_id,
'{Subclasses}'::text[] || (row_number() OVER (PARTITION BY t1.lo_id ORDER BY t2.display_sequence) - 1)::text,
jsonb_build_object('Name', t2.lo_id, 'Subclasses', array_to_json(ARRAY[]::text[]))
FROM test t1
LEFT JOIN test t2 ON t1.lo_id = t2.parent_id
WHERE t1.parent_id IS NULL
UNION
SELECT
t1.lo_id,
t1.parent_id,
t2.lo_id,
tree.path || '{Subclasses}' || (row_number() OVER (PARTITION BY t1.lo_id ORDER BY t2.display_sequence) - 1)::text,
jsonb_build_object('Name', t2.lo_id, 'Subclasses', array_to_json(ARRAY[]::text[]))
FROM test t1
LEFT JOIN test t2 ON t1.lo_id = t2.parent_id
INNER JOIN tree ON (t1.lo_id = tree.child)
WHERE t1.parent_id = tree.lo_id
)
SELECT
child as lo_id, path, json
FROM tree
WHERE child IS NOT NULL ORDER BY path
Since you cannot fill a final JSON object in a recursive structure, you'll need a function for holding a global variable:
CREATE OR REPLACE FUNCTION json_tree() RETURNS jsonb AS $$
DECLARE
_json_output jsonb;
_temprow record;
BEGIN
SELECT
jsonb_build_object('Name', lo_id, 'Subclasses', array_to_json(ARRAY[]::text[]))
INTO _json_output
FROM test
WHERE parent_id IS NULL;
FOR _temprow IN
-- <the query from above>
LOOP
SELECT jsonb_insert(_json_output, _temprow.path, _temprow.json) INTO _json_output;
END LOOP;
RETURN _json_output;
END;
$$ LANGUAGE plpgsql;
Because the solution is quiet similar as this one, please look HERE for further explanation.
I have a table looks like this:
+---+---+---+---+---+---+
| | a | b | c | d | e |
+---+---+---+---+---+---+
| f | 1 | 3 | 3 | 2 | 2 |
| g | 3 | 1 | 3 | 2 | 1 |
| h | 3 | 3 | 1 | 3 | 3 |
| i | 2 | 2 | 3 | 1 | 2 |
| j | 2 | 1 | 3 | 2 | 1 |
+---+---+---+---+---+---+
And I want it to be like this:
+---+---+---+
| a | f | 1 |
| a | g | 3 |
| a | h | 3 |
| a | i | 2 |
| a | j | 2 |
| b | f | 3 |
| b | g | 1 |
| b | h | 3 |
| b | i | 2 |
| b | j | 1 |
| c | f | 3 |
| c | g | 3 |
| c | h | 1 |
| c | i | 3 |
| c | j | 3 |
| d | f | 2 |
| d | g | 2 |
| d | h | 3 |
| d | i | 1 |
| d | j | 2 |
| e | f | 2 |
| e | g | 1 |
| e | h | 3 |
| e | i | 2 |
| e | j | 1 |
+---+---+---+
I'm using this macro:
Sub ColumnCopy()
Sheets("test").Cells.Clear
Dim tRow As Long
Dim source As String
Dim target As String
source = "test1" 'Set your source sheet here
target = "test" 'Set the Target sheet name
'tRow = 2 'Define the start row of the target sheet
'Get Last Row and Column
lastRow = Sheets(source).Range("A" & Rows.Count).End(xlUp).Row
lastCol = Sheets(source).Cells(1, Columns.Count).End(xlToLeft).Column
tRow = 2
colBase = 2
Do While colBase < lastCol
For iRow = 2 To lastRow
Sheets(target).Cells(tRow, 1) = Sheets(source).Cells(1, colBase)
Sheets(target).Cells(tRow, 2) = Sheets(source).Cells(iRow, 1)
Sheets(target).Cells(tRow, 3) = Sheets(source).Cells(iRow, colBase)
tRow = tRow + 1
Next iRow
colBase = colBase + 1
Loop
End Sub
But i'm getting result missing the "column e":
+---+---+---+
| a | f | 1 |
| a | g | 3 |
| a | h | 3 |
| a | i | 2 |
| a | j | 2 |
| b | f | 3 |
| b | g | 1 |
| b | h | 3 |
| b | i | 2 |
| b | j | 1 |
| c | f | 3 |
| c | g | 3 |
| c | h | 1 |
| c | i | 3 |
| c | j | 3 |
| d | f | 2 |
| d | g | 2 |
| d | h | 3 |
| d | i | 1 |
| d | j | 2 |
+---+---+---+
I can't find what's causing this issue. I'm really new to excel macro.
Thanks for the help!
change this: Do While colBase < lastCol to this:
Do While colBase <= lastCol