Solved

Sample Clause  After Filtering With A WHERE Clause with Table Joins

Posted on 2008-10-20
7
325 Views
Last Modified: 2013-12-18
Hello -

Im attempting to pull a 5% sample from the resulting dataset of a table join.  However, when trying to run this code I get an error.  I believe its because the sample clause is getting confused as to which table to sample.  Is there a way to get around this? What is the smartest way to get around this limitation of the Sample Clause?

Sample code below.

Thanks in advance.
SELECT a.Contact a.ContactNum b.Address c.Phone

FROM a.ExecInfo, b.Location, c.Demographics

SAMPLE(5)

WHERE a.LinkID = b.LinkID

   AND b.LinkID = c.LINKID;

Open in new window

0
Comment
Question by:Keenetic
  • 2
  • 2
7 Comments
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 125 total points
ID: 22759646

Try this:

WITH Q0 AS (

SELECT a.Contact a.ContactNum b.Address c.Phone

FROM a.ExecInfo, b.Location, c.Demographics

WHERE a.LinkID = b.LinkID

   AND b.LinkID = c.LINKID)

SELECT * FROM Q0 SAMPLE(5);

Open in new window

0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 22759746

-- Or --
 Maybe you need to sample the largest table only:

WITH Q0 AS (

SELECT Contact, ContactNum, LinkID

  FROM ExecInfo SAMPLE(5))

SELECT a.Contact, a.ContactNum, b.Address c.Phone

  FROM Q0 a, b.Location, c.Demographics

 WHERE a.LinkID = b.LinkID

   AND b.LinkID = c.LINKID);

Open in new window

0
 

Expert Comment

by:dxladner
ID: 22760302
The SAMPLE clause in ORACLE is basically:
SAMPLE(the number that you want )

The are some limitations to this clause: 1. Discrepancies in actual percentage in the returned data2. Using views and certain joins.  
0
 
LVL 4

Expert Comment

by:hqassap
ID: 22764949
Try this:

SELECT *

  FROM (SELECT a.contact, a.contactnum, b.address, c.phone

          FROM a.execinfo, b.LOCATION, c.demographics

         WHERE a.linkid = b.linkid AND b.linkid = c.linkid)

 WHERE ROWNUM < 6

Open in new window

0
 

Expert Comment

by:dxladner
ID: 22767013
The RowNum clause will get you the first 6 rows of data only. If that is all you are looking for  then use hgassap's example. But using the RowNum will not pull RANDOM sampling throughout the database. You must use the SAMPLE keyword.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now