1

I found a solution to my query in this site. I need to get the count(*) value from multiple tables Select count(*) from multiple tables

My other problem is to get the DATE values of those multiple tables that returns > 1 row count. I have to compare the dates and get the latest. I have to query from 12 tables. Assuming I got > 1 value for tables 1, 2, 3, 4, 5, I will need to compare their DATE values. Sample code:

SELECT(SELECT COUNT(*) FROM   table1) AS count1,
(SELECT COUNT(*) FROM   table2) AS count2,
(SELECT COUNT(*) FROM   table3) AS count3 FROM dual

count1 count2 count3
  3       2     2

I need to select the MOST RECENT date in these tables.

Select date from table1 order by date desc
Select date from table2 order by date desc
Select date from table3 order by date desc

How am I supposed to do that without table scanning?

EDIT: Okay. The instructions said "Get matching records from tables 1-12 using the id as the key". If there are no records, display "No record found" Else: get the record with the latest date by comparing data_encoded from all tables.

3
  • 2
    I can't tell what you're after. Some example output would be helpful. Commented Jun 23, 2011 at 5:42
  • Yes, I was looking for the most recent date for 12 tables. I will also try the code below given by Bohemian Commented Jun 23, 2011 at 6:02
  • @OMG Ponies. No, 12 tables with different date column values. Commented Jun 23, 2011 at 6:09

2 Answers 2

3

It's pretty hard to tell what you're after, but here's a guess:

SELECT
    (SELECT MAX(date) FROM table1) AS latest1,
    (SELECT MAX(date) FROM table2) AS latest2,
    (SELECT MAX(date) FROM table3) AS latest3,
    -- etc
FROM dual;

You can avoid table scans by having indexes on date, in which case the optimizer should do index-only scans (very fast).

Sign up to request clarification or add additional context in comments.

1 Comment

Got this working. Now I have to get the MAX() date of the results that has an output. thanks so much.
0

have you try to group them and order it and select the first date

select ID, Date, count(date) from table1 t1 inner join table2 t2 where t1.id = t2.id inner join table3 t3 where t1.id = t3.id etc.. group by date order by date desc

something long that line

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.