Home » SQL & PL/SQL » SQL & PL/SQL » count of tests (10)
count of tests [message #645096] Thu, 26 November 2015 06:36 Go to next message
pratik4891
Messages: 73
Registered: February 2011
Member
Hello ,

Please find the ddls below

create table fact_test 
(
order_no number ,
Orderable varchar2(10) , 
package varchar2(10) ,
test varchar2(10)
 )


insert into fact_test  (order_no , orderable , package , test ) values (101,'EEEVP','EEEV','EEEV') ;
insert into fact_test  (order_no , orderable , package , test ) values (101,'EEEVP','G6PD','G6PD_RAW') ;
insert into fact_test  (order_no , orderable , package , test ) values (101,'EEEVP','G6PD','G6PD_') ;
insert into fact_test  (order_no , orderable , package , test ) values (101,'EEEVP','GPI','GPI_') ;
insert into fact_test  (order_no , orderable , package , test ) values (101,'EEEVP','GPI','GPI_RAW') ;
insert into fact_test  (order_no , orderable , package , test ) values (101,'EEEVP','PK','PK1') ;
insert into fact_test  (order_no , orderable , package , test ) values (101,'EEEVP','PK','PK2') ;
insert into fact_test  (order_no , orderable , package , test ) values (102,'G6PD','G6PD','G6PD_RAW') ;
insert into fact_test  (order_no , orderable , package , test ) values (102,'G6PD','G6PD','G6PD_') ;
insert into fact_test  (order_no , orderable , package , test ) values (103,'EEEV','EEEV','EEEV') ;


commit


Table values

order_no	Orderable	package	test
101.00	EEEVP	PK	PK2
101.00	EEEVP	GPI	GPI_
101.00	EEEVP	GPI	GPI_RAW
101.00	EEEVP	PK	PK1
101.00	EEEVP	EEEV	EEEV
101.00	EEEVP	G6PD	G6PD_
101.00	EEEVP	G6PD	G6PD_RAW
102.00	G6PD	G6PD	G6PD_RAW
102.00	G6PD	G6PD	G6PD_
103.00	EEEV	EEEV	EEEV


requirement is to take the count of tests which can appear either in orderable or in package
please not same code can appear both in orderable and package but we have to count once

scenario 1

So the count of EEEVP will be 1 because the distinct count of eeevp combined both in orderable and package is 1
scenario 2

count of G6PD will be 2 because G6PD appears under order 101 as a package and under order 102 the count is 1 even though its appeared twice but distinct value is 1

Can anyone please help me out with the logic
Re: count of tests [message #645097 is a reply to message #645096] Thu, 26 November 2015 07:33 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

What does "distinct mean here? What is distinct? For ma all lines are distinct.
Post the final result you want and align your columns.

Re: count of tests [message #645098 is a reply to message #645096] Thu, 26 November 2015 07:37 Go to previous messageGo to next message
Rishab_le_noob
Messages: 12
Registered: November 2015
Location: Kolkata
Junior Member
As far as I understood.

Quote:

scenario 1

So the count of EEEVP will be 1 because the distinct count of eeevp combined both in orderable and package is 1
scenario 2

count of G6PD will be 2 because G6PD appears under order 101 as a package and under order 102 the count is 1 even though its appeared twice but distinct value is 1


This can be the query
select distinct  nvl(a.orderable,b.package),nvl(count_order,0)+ nvl(count_package,0) count
from 
(select order_no, count(distinct  orderable) count_order, orderable
from   fact_test
group by order_no, orderable) a
full outer join
(select order_no, count(distinct  package) count_package, package
from   fact_test 
group by order_no,  package) b
on a.orderable=b.package;


But can you clarify a little more as I cannot see any value as 'EEEVP' or 'G6PD' in the column test.
Just as Michel said.

[Updated on: Thu, 26 November 2015 07:38]

Report message to a moderator

Re: count of tests [message #645101 is a reply to message #645098] Thu, 26 November 2015 08:38 Go to previous messageGo to next message
pratik4891
Messages: 73
Registered: February 2011
Member
Thanks for helping me out.

lets say we have to take the counts EEEVP and G6PD .
First we don't have to consider test column at all .We have to pull the count from orderable and package column for test codes like EEEVP , G6PD .

Rule of taking the count below -

a )the count will be per order id ,so if a test code exists for a particular order either in orderable or package column even though it occurs multiple time we have to take the distinct number of counts
example for order id 101 EEEVP appears 7 times , for 102 and 103 EEEVP doesnt exist so the count will be 1 (because the seven appearances of EEEVP is for same order id )

b) if the test code both presents in orderable or package even for different order id we have to take the count as the summation of their distinct appearances
say G6PD exists for order 101 twice as a package and should be counted as 1 + it also exists for order 102 as orderable twice which also should be counted as 1
so the total count for G6PD is 2




order_no   Orderable	package	   test
101.00	   EEEVP	PK	   PK2
101.00	   EEEVP        GPI        GPI_
101.00	   EEEVP	GPI	   GPI_RAW
101.00	   EEEVP	PK	   PK1
101.00	   EEEVP	EEEV	   EEEV
101.00	   EEEVP	G6PD	   G6PD_
101.00	   EEEVP	G6PD	   G6PD_RAW
102.00	   G6PD	        G6PD	   G6PD_RAW
102.00	   G6PD	        G6PD	   G6PD_
103.00	   EEEV	        EEEV	   EEEV



Please let me know if you need further clarification



Re: count of tests [message #645103 is a reply to message #645101] Thu, 26 November 2015 09:04 Go to previous messageGo to next message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
What if the code is in both orderable and package for a given order_no?
Re: count of tests [message #645104 is a reply to message #645101] Thu, 26 November 2015 09:35 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
First we don't have to consider test column at all .


So why is it in the test case?

Quote:
the count will be per order id


There is no order id. Take care of what you write. If the first words of specification is false then how could we believe the rest of it?

Quote:
even though it occurs multiple time we have to take the distinct number of counts


This is meaningless. What is the distinct number of counts when there is only one count: the number of times it appears?

Quote:
we have to take the count as the summation of their distinct appearances


This is not clear. See cookiemonster's question.

We still not have the final result you want.

Re: count of tests [message #645110 is a reply to message #645101] Thu, 26 November 2015 13:28 Go to previous messageGo to next message
Rishab_le_noob
Messages: 12
Registered: November 2015
Location: Kolkata
Junior Member
Then doesnt my query do as you need?
Is there anything wrong/different that you want? Cuz as per your scenario requirement it seems like the ORDER is the driving force here.
You basically want to know, in how many ORDERS an ITEM is present, irrespective of the number of times its repeated.

Confused We're all a little confused now. Confused

[Updated on: Thu, 26 November 2015 13:30]

Report message to a moderator

Re: count of tests [message #645111 is a reply to message #645103] Thu, 26 November 2015 15:07 Go to previous messageGo to next message
pratik4891
Messages: 73
Registered: February 2011
Member
then we have to consider orderable because its on the highest level
Re: count of tests [message #645112 is a reply to message #645104] Thu, 26 November 2015 15:10 Go to previous messageGo to next message
pratik4891
Messages: 73
Registered: February 2011
Member


Quote:
So why is it in the test case?

Please ignore that column


Quote:
the count will be per order id
order_no

Quote:
This is meaningless. What is the distinct number of counts when there is only one count: the number of times it appears?

count should be 1
Re: count of tests [message #645113 is a reply to message #645112] Thu, 26 November 2015 15:11 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
We still not have the final result you want.
Re: count of tests [message #645114 is a reply to message #645110] Thu, 26 November 2015 15:12 Go to previous messageGo to next message
pratik4891
Messages: 73
Registered: February 2011
Member
Quote:
Then doesnt my query do as you need?


yes its working , thanks !!
I will check with few other rows and will let you know

Thanks,
Re: count of tests [message #645115 is a reply to message #645114] Thu, 26 November 2015 15:27 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Michel Cadot wrote on Thu, 26 November 2015 22:11

Quote:
We still not have the final result you want.

[Updated on: Thu, 26 November 2015 15:27]

Report message to a moderator

Re: count of tests [message #645116 is a reply to message #645115] Thu, 26 November 2015 18:54 Go to previous messageGo to next message
pratik4891
Messages: 73
Registered: February 2011
Member


Test Code count
EEEVP 1
PK 1
GPI 1
EEEV 2
G6PD 2
Re: count of tests [message #645119 is a reply to message #645116] Fri, 27 November 2015 00:23 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
then we have to consider orderable because its on the highest level


What does this mean? Highest? higher of what? How does this change the specification?
If we add
101.00 G6PD EEEVP
How the result is changed? Why?

Re: count of tests [message #645129 is a reply to message #645119] Fri, 27 November 2015 01:10 Go to previous messageGo to next message
pratik4891
Messages: 73
Registered: February 2011
Member
My bad , if it falls in both we can consider either one (orderable or package ).
So the result won't change
Re: count of tests [message #645131 is a reply to message #645129] Fri, 27 November 2015 01:35 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

So, in short, you want the number of orders (i.e. distinct order_no) for each value found in orderable or package columns:
SQL> select decode(col, 1,orderable, 2,package) test,
  2         count(distinct order_no) nb
  3  from fact_test,
  4       (select 1 col from dual union all select 2 from dual)
  5  group by decode(col, 1,orderable, 2,package)
  6  order by 1
  7  /
TEST               NB
---------- ----------
EEEV                2
EEEVP               1
G6PD                2
GPI                 1
PK                  1

For information, in 11g, you can use UNPIVOT clause:
SQL> select "Test", count(distinct order_no) "Nb"
  2  from fact_test
  3       unpivot ("Test" for
  4                test_type in (orderable as 'Orderable', package as 'Package'))
  5  group by "Test"
  6  order by 1
  7  /
Test               Nb
---------- ----------
EEEV                2
EEEVP               1
G6PD                2
GPI                 1
PK                  1

[Updated on: Fri, 27 November 2015 01:35]

Report message to a moderator

Re: count of tests [message #645133 is a reply to message #645131] Fri, 27 November 2015 01:58 Go to previous message
pratik4891
Messages: 73
Registered: February 2011
Member
Thanks so much!!
Previous Topic: Decode expression help
Next Topic: SQL Query Help
Goto Forum:
  


Current Time: Sun Jul 19 14:18:14 CDT 2026