Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7734531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:16:23+00:00 2026-06-01T07:16:23+00:00

I think that is not easy to understand what I need reading title, so

  • 0

I think that is not easy to understand what I need reading title, so I’ll explain better.
My database is like:

Table1Table1Table2Table2Table3Table3 and other tables like these…

Users will insert into textfield a value like 1 and I will show them only the name that contains 1. I do this in this way:

ArrayList<String> found = new ArrayList<String>();
ArrayList<String> allTable = getAllTableInDB();

for(String table:allTable){
    try {
        ResultSet rs = stat.executeQuery("SELECT * FROM '"+table+"'");
        while(rs.next()){
            if(!found.contains(rs.getString("name")) && rs.getString("name").equals(WhatUserInserts))
                found.add(rs.getString("name"));
        }
    } catch (SQLException ex) {
        ex.printStackTrace();
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
    }
}

What I want to do now is to calculate the “best” of this name found ordering by average of column v.

For one table I know that I can use something like:

SELECT name FROM table GROUP BY name ORDER BY AVG(v) DESC

How can I do the same on this limit of value of some table?

I hope I explain my question well

SOLVED in this way THANKS TO Dan P:

ArrayList<String> tables = getAllTableInDB();
String query = "SELECT * FROM (";
String union = " UNION ";

for(int i=0;i<tables.size();i++){
    query+="SELECT * FROM '"+tables.get(i)+"' WHERE name LIKE '%"+find+"%'";
    query = i==tables.size()-1?query:query+union;
}

query+=") GROUP BY nome ORDER BY AVG(venduti) DESC";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:16:24+00:00Added an answer on June 1, 2026 at 7:16 am

    Assuming you want the Max or Avg of all tables this will get you the max V value for name. If you want Avgerage use AVG. I didn’t exactly understand the question but I think this what you want.

    SELECT Name, Max(V)
    FROM
    (
      SELECT Name, v
      FROM TABLE1
      UNION
      SELECT Name, v
      FROM TABLE2
      UNION
      SELECT Name, v
      FROM TABLE3
    ) AS AllTables
    GROUP BY Name
    WHERE Name = 'Jon Doe'
    

    After commenting you asked if you could enhance performance. Yes by including the name in the where clause for each table assuming there is an index / primary key on the name. You should probably used a table valued function if you go this route.

    DECLARE @Name NVARCHAR(50)
    SET @Name = 'Jon Doe' 
    
    SELECT Name, Max(V)
    FROM
    (
      SELECT Name, v
      FROM TABLE1
      WHERE Name = @Name
      UNION
      SELECT Name, v
      FROM TABLE2
      WHERE Name = @Name
      UNION
      SELECT Name, v
      FROM TABLE3
      WHERE Name = @Name
    ) AS AllTables
    GROUP BY Name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First thing i want to say that it's not an easy question to explain,
OK so that title sucks a little but I could not think of anything
I'm still trying to load my image from database. But that's not so easy
Currently I think that processor only has two states: run and not run. If
Currently in our enterprise we have a situation that i think it's not very
I want to make something that makes much sense, but I think its not
There is one thing that I do not understand... Imagine you have a text
the lazy thread-safe singleton instantion is kinda not easy to understand to every coder,
GLSL has a full C-style preprocessor. The only thing that does not work is
I'm working on a small roguelike game, and for any object/thing that is not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.