Solved

Oracle 10g query Need Help!

Posted on 2011-03-16
11
375 Views
Last Modified: 2012-05-11
Im fetching  books or publication  from oracle 10g but issue is author profile could be 2, 3 or more  if I need 10 record so it could be 13 or 14 it depend on author some books may have only one author and some books may have 2 or three I need 10 records of books with 10 records maybe authors could be more then 10  please have a look my query what im writing in this query im getting 10 records but im losing some books coz, of 10 limit …


select * from (
SELECT
USA_BOOKS.PUB_ID id,
USA_BOOKS.DETAIL_FILE_URL detailFileURL,
USA_BOOKS.PUB_FIRST_EDN_YEAR editionYear ,
USA_BOOKS.TITLE,
USA_BOOKS.WHAT_IS_NEW_IMAGE WHATISNEWIMAGE,
PRF.PRF_ID AS prfId,
PRF.PRF_NAME AS prfName,
PRF.PRF_IS_CLICKABLE AS prfIsClickable,
profiles.PRF_TYPE prfType
FROM
USA_BOOKS_en USA_BOOKS,
USA_BOOKS_PROFILES_en profiles,
PROFILE_en PRF
WHERE
(USA_BOOKS.IS_USA LIKE '1' ||'%' OR USA_BOOKS.IS_USA LIKE '0%') AND
USA_BOOKS.PUB_ID = profiles.PUB_ID AND
PRF.PRF_ID = profiles.PRF_ID
ORDER BY TO_DATE(USA_BOOKS.PUB_POSTED_DATE, 'MM dd yyyy') DESC , USA_BOOKS.PUB_ID ASC, profiles.PRF_TYPE ASC
)
where rownum <11
0
Comment
Question by:xeondxb
11 Comments
 
LVL 7

Accepted Solution

by:
MrNed earned 250 total points
ID: 35154029
I'm assuming USA_BOOKS_PROFILES contains the authors - so one book may have multiple rows here?

First get your top 10 rows, then grab the associated author details. If I didn't mess it up it will be something like:

SELECT x.id,
 x.detailFileURL,
 x.editionYear ,
 x.TITLE,
 x.WHATISNEWIMAGE,
PRF.PRF_ID AS prfId,
PRF.PRF_NAME AS prfName,
PRF.PRF_IS_CLICKABLE AS prfIsClickable,
profiles.PRF_TYPE prfType
FROM (
 select * from (
 SELECT
 USA_BOOKS.PUB_ID id,
 USA_BOOKS.DETAIL_FILE_URL detailFileURL,
 USA_BOOKS.PUB_FIRST_EDN_YEAR editionYear ,
 USA_BOOKS.TITLE,
 USA_BOOKS.WHAT_IS_NEW_IMAGE WHATISNEWIMAGE,
 USA_BOOKS.IS_USA
 FROM
 USA_BOOKS_en USA_BOOKS
 ORDER BY TO_DATE(USA_BOOKS.PUB_POSTED_DATE, 'MM dd yyyy') DESC , USA_BOOKS.PUB_ID ASC
 )
 where rownum <11
) x,
USA_BOOKS_PROFILES_en profiles,
PROFILE_en PRF
WHERE
(x.IS_USA LIKE '1' ||'%' OR x.IS_USA LIKE '0%') AND
x.id = profiles.PUB_ID AND
PRF.PRF_ID = profiles.PRF_ID
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35154053
Can you explain with some sample data on what exactly are you looking for?
0
 

Author Comment

by:xeondxb
ID: 35154153
if you check highlighted records you will see there is 2 authors and im not using  
SELECT
USA_BOOKS.PUB_ID id,
USA_BOOKS.DETAIL_FILE_URL detailFileURL,
USA_BOOKS.PUB_FIRST_EDN_YEAR editionYear ,
USA_BOOKS.TITLE,
USA_BOOKS.WHAT_IS_NEW_IMAGE WHATISNEWIMAGE,
PRF.PRF_ID AS prfId,
PRF.PRF_NAME AS prfName,
PRF.PRF_IS_CLICKABLE AS prfIsClickable,
profiles.PRF_TYPE prfType
FROM
USA_BOOKS_en USA_BOOKS,
USA_BOOKS_PROFILES_en profiles,
PROFILE_en PRF
WHERE
(USA_BOOKS.IS_USA LIKE '1' ||'%' OR USA_BOOKS.IS_USA LIKE '0%') AND
USA_BOOKS.PUB_ID = profiles.PUB_ID AND
PRF.PRF_ID = profiles.PRF_ID
ORDER BY TO_DATE(USA_BOOKS.PUB_POSTED_DATE, 'MM dd yyyy') DESC , USA_BOOKS.PUB_ID ASC, profiles.PRF_TYPE ASC

Open in new window

screen.jpg
0
 

Author Comment

by:xeondxb
ID: 35154654
@MrNed:  i tried your given query it is giving me only 2 results :(
0
 
LVL 7

Expert Comment

by:MrNed
ID: 35154844
OK, not sure why. Do you understand what I am trying to do with my query? It's not clear what you want, I'm assuming you want 10 distinct books, each with one row per author. So the final output will be at least 10 rows, maybe more.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:xeondxb
ID: 35154872
@MrNed:
10 books records but one book author can be more then 1 so records will be more then 10 which book have more then one author for example "AB book" have 2 author  1:XY  2:YZ except this book all books written by one author so record will be 11 because of "AB book"  because its duplicating ...
0
 
LVL 7

Expert Comment

by:MrNed
ID: 35154891
So what does the inner part of my query give you:

 select * from (
 SELECT
 USA_BOOKS.PUB_ID id,
 USA_BOOKS.DETAIL_FILE_URL detailFileURL,
 USA_BOOKS.PUB_FIRST_EDN_YEAR editionYear ,
 USA_BOOKS.TITLE,
 USA_BOOKS.WHAT_IS_NEW_IMAGE WHATISNEWIMAGE,
 USA_BOOKS.IS_USA
 FROM
 USA_BOOKS_en USA_BOOKS
 ORDER BY TO_DATE(USA_BOOKS.PUB_POSTED_DATE, 'MM dd yyyy') DESC , USA_BOOKS.PUB_ID ASC
 )
 where rownum <11
0
 

Author Comment

by:xeondxb
ID: 35155112
SELECT
USA_BOOKS.PUB_ID id,
USA_BOOKS.DETAIL_FILE_URL detailFileURL,
USA_BOOKS.PUB_FIRST_EDN_YEAR editionYear ,
USA_BOOKS.TITLE,
USA_BOOKS.WHAT_IS_NEW_IMAGE WHATISNEWIMAGE,
PRF.PRF_ID AS prfId,
PRF.PRF_NAME AS prfName,
PRF.PRF_IS_CLICKABLE AS prfIsClickable,
profiles.PRF_TYPE prfType
FROM
USA_BOOKS_en USA_BOOKS,
USA_BOOKS_PROFILES_en profiles,
PROFILE_en PRF
WHERE
(USA_BOOKS.IS_USA LIKE '1' ||'%' OR USA_BOOKS.IS_USA LIKE '0%') AND
USA_BOOKS.PUB_ID = profiles.PUB_ID AND
PRF.PRF_ID = profiles.PRF_ID
ORDER BY TO_DATE(USA_BOOKS.PUB_POSTED_DATE, 'MM dd yyyy') DESC , USA_BOOKS.PUB_ID ASC, profiles.PRF_TYPE ASC


when im writing this im getting result which u can see screen shoot above from that result i want 10 records but as i told u the condition 10 records should be books but it'll not apply on authors 10 books but if more then one author of one book it could be 12 records or 11 records because of author record
0
 
LVL 11

Assisted Solution

by:yuching
yuching earned 250 total points
ID: 35390296
hi xeondxb:,
in this case you can only get one author, or you will need to create a function to return all the author in the form of "Bilal Barakat, Anne Goujon"

Else you can try the below to return the max author details
Select USA_BOOKS.PUB_ID id,
	USA_BOOKS.DETAIL_FILE_URL detailFileURL,
	USA_BOOKS.PUB_FIRST_EDN_YEAR editionYear ,
	USA_BOOKS.TITLE,
	USA_BOOKS.WHAT_IS_NEW_IMAGE WHATISNEWIMAGE,
	max(PRF.PRF_ID) AS prfId,
	max(PRF.PRF_NAME) AS prfName,
	max(PRF.PRF_IS_CLICKABLE) AS prfIsClickable,
	max(profiles.PRF_TYPE) prfType
From USA_BOOKS_en USA_BOOKS,
	USA_BOOKS_PROFILES_en profiles,
	PROFILE_en PRF
WHERE
(USA_BOOKS.IS_USA LIKE '1' ||'%' OR USA_BOOKS.IS_USA LIKE '0%') AND
USA_BOOKS.PUB_ID = profiles.PUB_ID AND
PRF.PRF_ID = profiles.PRF_ID
Group By USA_BOOKS.PUB_ID,  USA_BOOKS.DETAIL_FILE_URL, 
    USA_BOOKS.PUB_FIRST_EDN_YEAR, USA_BOOKS.TITLE, USA_BOOKS.WHAT_IS_NEW_IMAGE
ORDER BY TO_DATE(USA_BOOKS.PUB_POSTED_DATE, 'MM dd yyyy') DESC , USA_BOOKS.PUB_ID ASC, 9 ASC

Open in new window

0
 

Author Closing Comment

by:xeondxb
ID: 35726062
it did not work but thanks guys....  just closing question
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

743 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

12 Experts available now in Live!

Get 1:1 Help Now