Home » SQL & PL/SQL » SQL & PL/SQL » How to calculate in percent in sql query?
How to calculate in percent in sql query? [message #631364] Tue, 13 January 2015 02:18 Go to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
Select count(total ms1) MS1 from A where launch_date = trunc(sysdate)
union all
Select count(total ms2)) MS2 from A where launch_date = trunc(sysdate -1)

I want out put like

MS1     MS2   Diff with (MS1 - MS2)
1000    400   600



But I need Diff with (MS1 - MS2) in percent not number.

[Updated on: Tue, 13 January 2015 02:20]

Report message to a moderator

Re: How to calculate in percent in sql query? [message #631367 is a reply to message #631364] Tue, 13 January 2015 02:43 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

select ms1, ms2, 100*(ms1-ms2)/ms2 "%diff"
from ( <your query> ) 
/


(I let you handle the case ms2 is 0.)

Re: How to calculate in percent in sql query? [message #631369 is a reply to message #631367] Tue, 13 January 2015 02:49 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
Thanks Michel, Please tell me some more, if %diff is greater than 95 then show 'alert'.
Re: How to calculate in percent in sql query? [message #631371 is a reply to message #631369] Tue, 13 January 2015 02:51 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

What about "CASE"?
You have to work a little bit to learn, haven't you?

Re: How to calculate in percent in sql query? [message #631376 is a reply to message #631367] Tue, 13 January 2015 03:32 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
I am confuse here caused ms1 hold sysdate data & ms2 hold sysdate -1 data. Please clear more query.

Michel Cadot wrote on Tue, 13 January 2015 02:43

select ms1, ms2, 100*(ms1-ms2)/ms2 "%diff"
from ( <your query> ) 
/


(I let you handle the case ms2 is 0.)


Re: How to calculate in percent in sql query? [message #631378 is a reply to message #631376] Tue, 13 January 2015 03:34 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Use two inline views, one for each ms value.
Re: How to calculate in percent in sql query? [message #631406 is a reply to message #631376] Tue, 13 January 2015 10:17 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
aaditya321 wrote on Tue, 13 January 2015 04:32
I am confuse here caused ms1 hold sysdate data & ms2 hold sysdate -1 data.


Which is bad since you read same table twice. Use something like:

with t as (
           select  case
                     when abs((count(*) - lead(count(*)) over(order by launch_date desc)) / count(*)) > .95 then 'alert'
                   end alert
             from  A
             where launch_date in (trunc(sysdate),trunc(sysdate - 1))
             group by launch_date
          )
select  alert
  from  t
  where alert is not null
/


For example:

SQL> with a as (
  2              select  trunc(sysdate) launch_date
  3                from  dual
  4                connect by level <= 5
  5             union all
  6              select  trunc(sysdate - 1) launch_date
  7                from  dual
  8                connect by level <= 7
  9            ),
 10       t as (
 11             select  case
 12                       when abs((count(*) - lead(count(*)) over(order by launch_date desc)) / count(*)) > .95 then 'alert'
 13                     end alert
 14               from  A
 15               where launch_date in (trunc(sysdate),trunc(sysdate - 1))
 16               group by launch_date
 17            )
 18  select  alert
 19    from  t
 20    where alert is not null
 21  /

no rows selected

SQL> with a as (
  2              select  trunc(sysdate) launch_date
  3                from  dual
  4                connect by level <= 5
  5             union all
  6              select  trunc(sysdate - 1) launch_date
  7                from  dual
  8                connect by level <= 10
  9            ),
 10       t as (
 11             select  case
 12                       when abs((count(*) - lead(count(*)) over(order by launch_date desc)) / count(*)) > .95 then 'alert'
 13                     end alert
 14               from  A
 15               where launch_date in (trunc(sysdate),trunc(sysdate - 1))
 16               group by launch_date
 17            )
 18  select  alert
 19    from  t
 20    where alert is not null
 21  /

ALERT
-----
alert

SQL> 


SY.
Re: How to calculate in percent in sql query? [message #631408 is a reply to message #631406] Tue, 13 January 2015 11:21 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
Thank you SY.
Re: How to calculate in percent in sql query? [message #631409 is a reply to message #631408] Tue, 13 January 2015 11:35 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
My query is like output:
ID   MS1     MS2   Diff with (MS1 - MS2)   Percent of Diff   Action
01   1000    400      600     


where MS1 show sysdate data and MS2 show sysdate-1 data from same table A, if percent of difference is greater than 95% than show action alert otherwise show action normal.
Re: How to calculate in percent in sql query? [message #631410 is a reply to message #631409] Tue, 13 January 2015 11:37 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

And?
You didn't have enough information including queries to lead you to your query?
I remind you the rules:
With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.
Use SQL*Plus and copy and paste what you already tried.

[Updated on: Tue, 13 January 2015 11:38]

Report message to a moderator

Re: How to calculate in percent in sql query? [message #631443 is a reply to message #631410] Wed, 14 January 2015 01:56 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
Now I want to difference between current date & any other date, like sysdate & sysdate-1 or sysdate & sysdate-2 or like more..
and need difference in percent.

create table Diff(id number(4),
                  ms number(6),
                  date Date);

Insert into Diff(id , ms, date)
            value(01, 10002, 'sysdate');

Insert into Diff(id , ms, date)
            value(02, 10003, 'sysdate');

Insert into Diff(id , ms, date)
            value(03, 10004, 'sysdate');

Insert into Diff(id , ms, date)
            value(04, 10005, 'sysdate');



Insert into Diff(id , ms, date)
            value(01, 10000, 'sysdate-1');
Insert into Diff(id , ms, date)
            value(02, 10001, 'sysdate-1');
Insert into Diff(id , ms, date)
            value(03, 10002, 'sysdate-1');
Insert into Diff(id , ms, date)
            value(04, 10000, 'sysdate-1');


Insert into Diff(id , ms, date)
            value(01, 10003, 'sysdate-2');
Insert into Diff(id , ms, date)
            value(02, 10004, 'sysdate-2');
Insert into Diff(id , ms, date)
            value(03, 10005, 'sysdate-2');
Insert into Diff(id , ms, date)
            value(04, 10006, 'sysdate-2');
Re: How to calculate in percent in sql query? [message #631446 is a reply to message #631443] Wed, 14 January 2015 02:00 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
Now I want to difference between current date & any other date,


Show us what should be the result for the data you gave.

Re: How to calculate in percent in sql query? [message #631459 is a reply to message #631446] Wed, 14 January 2015 02:38 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
My output like:


ID   ms of sysdate     ms of sysdate-1      Diff. of ms between two date   Percent of Diff. in two date   


likewise 

ID     ms of sysdate     ms of sysdate-2      Diff. of ms between two date   Percent of Diff. in two date
or
ID     ms of sysdate     ms of sysdate-3      Diff. of ms between two date   Percent of Diff. in two date
or
ID     ms of sysdate     ms of sysdate-4      Diff. of ms between two date   Percent of Diff. in two date


Means we can calculate between any two dated difference & difference should be in percent
Re: How to calculate in percent in sql query? [message #631460 is a reply to message #631459] Wed, 14 January 2015 02:40 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
So don't you just need to replace sysdate and sysdate -1 in the query with variables that hold the dates you're interested in?
Not sure why you've got a problem here.
Re: How to calculate in percent in sql query? [message #631464 is a reply to message #631460] Wed, 14 January 2015 02:45 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
Hello Cookiemonster,

Please write query here.
Re: How to calculate in percent in sql query? [message #631465 is a reply to message #631464] Wed, 14 January 2015 02:46 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Please write the output you want there and the query should return.

Re: How to calculate in percent in sql query? [message #631468 is a reply to message #631464] Wed, 14 January 2015 02:49 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
aaditya321 wrote on Wed, 14 January 2015 08:45
Hello Cookiemonster,

Please write query here.


Are you really saying you need me to rewrite the above query to replace sysdate with a variable?
If you can't manage that you're in the wrong job.
Re: How to calculate in percent in sql query? [message #631469 is a reply to message #631468] Wed, 14 January 2015 02:54 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
ID   ms of sysdate     ms of sysdate-1      Diff. of ms between two date   Percent of Diff. in two date 
01     1001                1000                 1                                  %.1


I want output like above.
or
if some one say to calculate sysdate & sysdate-2 diff. then output like below

ID   ms of sysdate     ms of sysdate-2      Diff. of ms between two date   Percent of Diff. in two date 
01     1002                1000                 2                                  %.2

[Updated on: Wed, 14 January 2015 02:57]

Report message to a moderator

Re: How to calculate in percent in sql query? [message #631472 is a reply to message #631469] Wed, 14 January 2015 02:59 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

OK, you already have this query, it has been posted (or at least so much hint you could wrkite it).

And we still have not a valid test case (why didn't you test it before posting it?)
SQL> create table Diff(id number(4),
  2                    ms number(6),
  3                    date Date);
                  date Date)
                  *
ERROR at line 3:
ORA-00904: : invalid identifier
Re: How to calculate in percent in sql query? [message #631473 is a reply to message #631469] Wed, 14 January 2015 03:00 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
if some one say to calculate sysdate & sysdate-2 diff. then output like below


Just replace 1 by 2 in the query.

Re: How to calculate in percent in sql query? [message #631476 is a reply to message #631473] Wed, 14 January 2015 03:09 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
Please give me trick so that I can do my query. please don't send me in test case.
Re: How to calculate in percent in sql query? [message #631479 is a reply to message #631476] Wed, 14 January 2015 03:17 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
please don't send me in test case.


What does this mean?

Re: How to calculate in percent in sql query? [message #631484 is a reply to message #631479] Wed, 14 January 2015 03:30 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
This means we need only logic of query.
Re: How to calculate in percent in sql query? [message #631485 is a reply to message #631484] Wed, 14 January 2015 03:33 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
aaditya321 wrote on Wed, 14 January 2015 15:00
This means we need only logic of query.


And that is what has been provided to you, unlimited hints. and if that's not the case, why did you ask CM to write the query for you? Contradicting statements Shocked

[Updated on: Wed, 14 January 2015 03:34]

Report message to a moderator

Re: How to calculate in percent in sql query? [message #631486 is a reply to message #631484] Wed, 14 January 2015 03:34 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Take original query, change from union all to two inline views (do you know what an inline view is?) so you can get both ms amounts in one row. Then add percentage calculation and case statement for alert.
Try it.
Re: How to calculate in percent in sql query? [message #631490 is a reply to message #631486] Wed, 14 January 2015 03:52 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
I feel my query will not solve, how to explain you guys.
Re: How to calculate in percent in sql query? [message #631492 is a reply to message #631490] Wed, 14 January 2015 03:53 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

First, posting your query.

Re: How to calculate in percent in sql query? [message #631494 is a reply to message #631492] Wed, 14 January 2015 04:03 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
Actually I am unable to write our query only we can give our desire output.
Re: How to calculate in percent in sql query? [message #631495 is a reply to message #631494] Wed, 14 January 2015 04:10 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
So which of the steps I listed are you struggling with?
Re: How to calculate in percent in sql query? [message #631496 is a reply to message #631494] Wed, 14 January 2015 04:10 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
cookiemonster wrote on Wed, 14 January 2015 10:34
Take original query, change from union all to two inline views (do you know what an inline view is?) so you can get both ms amounts in one row. Then add percentage calculation and case statement for alert.
Try it.


Michel Cadot wrote on Tue, 13 January 2015 18:37

...I remind you the rules:
...
Use SQL*Plus and copy and paste what you already tried.

Re: How to calculate in percent in sql query? [message #631559 is a reply to message #631496] Thu, 15 January 2015 06:16 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
one more query here, please give me guide.

Suppose today(current date)publish English Book then now we have to find out when published English book prior current date.
Re: How to calculate in percent in sql query? [message #631560 is a reply to message #631559] Thu, 15 January 2015 06:21 Go to previous message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
aaditya321 wrote on Thu, 15 January 2015 17:46
one more query here, please give me guide.

Suppose today(current date)publish English Book then now we have to find out when published English book prior current date.


Michel Cadot wrote on Tue, 13 January 2015 23:07

I remind you the rules:
With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.
Use SQL*Plus and copy and paste what you already tried.



And this seems to be a different question, please open a new topic.
Previous Topic: pl sql to XML conversion
Next Topic: cursor_for_loop allows PL/SQL SELECT..INTO statement
Goto Forum:
  


Current Time: Fri Apr 19 20:13:28 CDT 2024