August 21, 2018 at 11:43 am
Hi,
I am trying to build sql statement, but got struck. Any inputs please?
Table A | Table B | ||||||
Column1 | Column2 | Active | Column1 | Column2 | Count | ||
a | C1 | Y | a | C1 | 1 | ||
a | C2 | N | a | C2 | 2 | ||
b | C1 | N | b | C1 | 3 | ||
b | C2 | Y | b | C2 | 4 | ||
c | C1 | Y | c | C1 | 5 | ||
c | C2 | Y | c | C2 | 6 | ||
d | C1 | N | d | C1 | 7 | ||
d | C2 | N | d | C2 | 8 | ||
Output | |||||||
Make C2 = C1 | |||||||
Column1 | Column2 | Active | Count | ||||
a | C1 | Y | 1 | ||||
b | C1 | Y | 4 | ||||
c | C1 | Y | 5 | ||||
c | C1 | Y | 6 | ||||
d | C1 | N | 7 | ||||
d | C1 | N | 8 |
August 21, 2018 at 11:51 am
You're going to learn a lot more if you do your own homework.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
August 21, 2018 at 12:09 pm
drew.allen - Tuesday, August 21, 2018 11:51 AMYou're going to learn a lot more if you do your own homework.Drew
Drew, Thank You for the reply. It's not homework, I am new to SQL and started to learn from w3schools.
This is something I got from one of the online tutorial and struck building it.
August 21, 2018 at 12:22 pm
venuvedham16 - Tuesday, August 21, 2018 11:43 AMHi,I am trying to build sql statement, but got struck. Any inputs please?
Table A Table B Column1 Column2 Active Column1 Column2 Count a C1 Y a C1 1 a C2 N a C2 2 b C1 N b C1 3 b C2 Y b C2 4 c C1 Y c C1 5 c C2 Y c C2 6 d C1 N d C1 7 d C2 N d C2 8 Output Make C2 = C1 Column1 Column2 Active Count a C1 Y 1 b C1 Y 4 c C1 Y 5 c C1 Y 6 d C1 N 7 d C1 N 8
I didn't understand your output. How count 2 and 3 are excluded from the expected results.
Saravanan
August 21, 2018 at 12:29 pm
saravanatn - Tuesday, August 21, 2018 12:22 PMvenuvedham16 - Tuesday, August 21, 2018 11:43 AMHi,I am trying to build sql statement, but got struck. Any inputs please?
Table A Table B Column1 Column2 Active Column1 Column2 Count a C1 Y a C1 1 a C2 N a C2 2 b C1 N b C1 3 b C2 Y b C2 4 c C1 Y c C1 5 c C2 Y c C2 6 d C1 N d C1 7 d C2 N d C2 8 Output Make C2 = C1 Column1 Column2 Active Count a C1 Y 1 b C1 Y 4 c C1 Y 5 c C1 Y 6 d C1 N 7 d C1 N 8 I didn't understand your output. How count 2 and 3 are excluded from the expected results.
Count 2 and 3 are inactive(active = n) for a given column 1 and column 2.
As we are changing c2=c1 in column 2, we are making sure active status of column 1& column 2 is consistent.
August 21, 2018 at 12:56 pm
venuvedham16 - Tuesday, August 21, 2018 12:09 PMdrew.allen - Tuesday, August 21, 2018 11:51 AMYou're going to learn a lot more if you do your own homework.Drew
Drew, Thank You for the reply. It's not homework, I am new to SQL and started to learn from w3schools.
This is something I got from one of the online tutorial and struck building it.
If you got it from a tutorial, then it's homework. Go back to the lesson where you got this, and it should give you an idea about how to approach this, assuming that the tutorial is well written.
If you are still having problems, show us what you've tried and we can help you.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
August 21, 2018 at 1:22 pm
venuvedham16 - Tuesday, August 21, 2018 11:43 AMHi,I am trying to build sql statement, but got struck. Any inputs please?
Table A Table B Column1 Column2 Active Column1 Column2 Count a C1 Y a C1 1 a C2 N a C2 2 b C1 N b C1 3 b C2 Y b C2 4 c C1 Y c C1 5 c C2 Y c C2 6 d C1 N d C1 7 d C2 N d C2 8 Output Make C2 = C1 Column1 Column2 Active Count a C1 Y 1 b C1 Y 4 c C1 Y 5 c C1 Y 6 d C1 N 7 d C1 N 8
Hi,
Welcome to SQLcentral. Drew is absolutely right and try to solve on your own . Also post create and insert statement so will help create test data and consume time.
create table table_a
(
Column1 varchar(200),
Column2 varchar(200),
active varchar(200)
);
insert into table_a values('a','c1','y');
insert into table_a values('a','c2','n');
insert into table_a values('b','c1','n');
insert into table_a values('b','c2','y');
insert into table_a values('c','c1','y');
insert into table_a values('c','c2','y');
insert into table_a values('d','c1','n');
insert into table_a values('d','c2','n');
create table table_b
(
Column1 varchar(200),
Column2 varchar(200),
count int
);
insert into table_b values('a','c1',1);
insert into table_b values('a','c2',2);
insert into table_b values('b','c1',3);
insert into table_b values('b','c2',4);
insert into table_b values('c','c1',5);
insert into table_b values('c','c2',6);
insert into table_b values('d','c1',7);
insert into table_b values('d','c2',8);
select Column1,Column2,active,count
from
(
select distinct Column1,Column2,active,count,
rank()over(partition by Column1,Column2 order by active desc) as rnk
from
(
select act.Column1,
case when act.Column2='c2' then 'c1' else act.column2 end as Column2,
act.active,
cnt.count
from table_a act
inner join table_b cnt
on act.Column1=cnt.Column1
and act.Column2=cnt.Column2
)ranking
)eli
where rnk=1
Output:
Column1 | Column2 | active | count |
---|---|---|---|
a | c1 | y | 1 |
b | c1 | y | 4 |
c | c1 | y | 5 |
c | c1 | y | 6 |
d | c1 | n | 7 |
d | c1 | n | 8 |
6 rows
Saravanan
August 21, 2018 at 1:59 pm
saravanatn, worked like a charm. Thank You!!
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply