Link to home
Start Free TrialLog in
Avatar of gvamsimba
gvamsimbaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

TSQL Data Comparision

HI, I have two columns A, B and C for which the sample
data is below.

Basically I need to get only those columns in a single
output table in my stored proc where the value in columns
A and B for currentperiod has doubled since previousperiod.

So in this case, I will need a output with only
Column A since the figures have doubled in current period
as compared to previous period.

Can any one please give me that TSQL ?

Many Thanks

A        B            C
200      400         Currentperiod
100      300         Previousperiod
Avatar of ralmada
ralmada
Flag of Canada image

looks to me that you need to do a self join, something along this line:

select t1.* from yourtable t1
inner join yourtable t2 on t1.a = t2.a * 2 and t1.b = t2.b * 2 and t1.c = t2.c - 30

Please note that the - 30 there is just an example, but you need define your criteria as to determine which one is current and which one is previous period
Avatar of gvamsimba

ASKER

HI Ralmada,
                   Thanks but may I know what is -30 used for ? Column C is a flag which determines
current and previous period...

                    Can you please send ur query again ?

Many Thanks
I'm not familiar with your data, so I've assumed that column C was just a datetime column, so the -30 was just to say current period - 30 days.

The idea is that you have to indicate the way to join the current and the previous period. If this is still not clear, please post some more meaningful sample data and the expected result.
Avatar of Lowfatspread
no you need to tell us what is the key for a row...

how you can tell what is current/previous

exactly what the output is you expect...

you talk about doubling... is that exactly or at least....
you then say you only want a single column in the output..


provide an actual set of example source and corresponding output data covering your requirements
ASKER CERTIFIED SOLUTION
Avatar of TSQLGuy30
TSQLGuy30

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Provide some more sample data with expected result. Do you want to display only those columns where the amounts get doubled? That means do you want to have your SELECT query dynamically constructed?
column C is a flag. I have already derived these current
and previous figures in my stored proc.
I have asked for it, Lowfatspread and Sharath too... Please post meaningful sample data and the expected result. Basically what you have currently and what you want to achieve. We cannot help with the information provided.
I have provided you with that solution. Can you please advise why you have accepted that comment?