December 20, 2017 at 10:28 am
I will check that part Jeff. I was checking all the possible scenarios and I think the query does not work for the following set of data:
create table VALUATION ( col1 VARCHAR(10), col2 VARCHAR(10), MTM numeric(10));
INSERT INTO VALUATION VALUES
('A','B',30),
('A','B',-80),
('a','B',43),
('A','B',85),
('B','A',-150),
('B','A',45),
('b','a',95),
('B','A',-5),
('A','C',200),
('A','D',100),
('B','F',200),
('d','K',400),
('K','D',-20),
('C','D',-50),
('D','C',-60),
('X','Y',-25),
('P','Q',-20),
('N','M',-15),
('Y','X',-25);
Row ('P','Q',-20) should be reported as Q P 20. There are no more trades between P and Q and the sign switch occurs only when col1 >= col2.
December 20, 2017 at 10:39 am
my apologies for the name confusion J. Drew.
December 20, 2017 at 2:31 pm
ger_serrano - Wednesday, December 20, 2017 10:28 AMI will check that part Jeff. I was checking all the possible scenarios and I think the query does not work for the following set of data:
create table VALUATION ( col1 VARCHAR(10), col2 VARCHAR(10), MTM numeric(10));
INSERT INTO VALUATION VALUES
('A','B',30),
('A','B',-80),
('a','B',43),
('A','B',85),
('B','A',-150),
('B','A',45),
('b','a',95),
('B','A',-5),
('A','C',200),
('A','D',100),
('B','F',200),
('d','K',400),
('K','D',-20),
('C','D',-50),
('D','C',-60),
('X','Y',-25),
('P','Q',-20),
('N','M',-15),
('Y','X',-25);
Row ('P','Q',-20) should be reported as Q P 20. There are no more trades between P and Q and the sign switch occurs only when col1 >= col2.
You're moving the goalposts. P|Q|-20 is equivalent to Q|P|20. The code works. This is merely a presentation issue. See if you can add to the code to get it into the desired presentation.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
December 21, 2017 at 7:34 am
Drew thank you very much for your help. Really apreciated. I just had to test all the possible data scenarios. My real table has more than 160 million records. However as you said I already modified the code to get the desired result.
December 21, 2017 at 7:53 am
ger_serrano - Wednesday, December 20, 2017 7:34 AMAwesome Jeff!!. Thanks a lot. I think the following query is also rdbms agnostic:
with newtab (col1,col2, mtm)
as
(SELECT upper(col1), upper(col2), mtm FROM sampledata
WHERE upper(col1) < upper(col2)
UNION ALLSELECT upper(col2), upper(col1), -mtm FROM sampledata
WHERE upper(col1) >= upper(col2)
)
select col1, col2, sum(mtm) as mtm from newtab
group by col1, col2;
To be sure, that was actually Drew.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 16 through 19 (of 19 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy