How to use the result of a sql query for a max () function

Hi
I wrote a query on which i wrote
"select max(id) from users "
how can i use the returned value.
if i made the var name ="userid"
can it be userid.rows[0] or what.
thnx for any help

Hi!
The result of this query will be the max ID of users' IDs.
Let say we have:
String sql="select max(users.id) from users";
Statement st = ctx.conn.createStatement();
ResultSet rs = st.executeQuery(sql);
rs.next();     
So you can get the max Id in the following way:     
int maxId=rs.getInt("id");
Regards,
Rossi

Similar Messages

  • Use the results of an SQL query to create another query

    I am working on a bidding/allocation system using C# and MySQL, and I am currently having difficulty implementing the “post-allocation” mechanics of the system. Let me explain how it works, before introducing the code:
    When the bids are made on the system, they all have the position 0, so the first thing the system does is order them according to priority.
     (bid table)
    In this case, 5 bidders are interested in what is in plot 15, but we have not assigned them a position yet.
     (bid table)
    The system then organises the bids according to the order in which they are going to processed.
    The bids are then allocated, but unfortunately there are only 3 spaces in plot 15 that can be allocated. As a result, we use the priority listing to determine which bidder gets what.
     (booking table)
    At this stage, I can say that I am stuck, there are two things that I want to do, both of which involve reusing the results of the query at step 2.
     (bid table)
    The bidders that have their job allocated should see: their bid.status updated
    from Queued to Allocated,
    the bid.position should
    be set to 0,
    and their job_id should
    be set to the booking they have been allocated (i.e. booking.id).
    The bidders that have not had their jobs allocated should see: their bid.status remain
    as Queued, and their bid.position should
    be set to 0 in
    preparation for the method to run again
    The difficulty I am having is in stage 3, how do I determine which jobs have been allocated and which ones have not, and then run the necessary SQL queries in order to make the updates?
    My code so far is as follows:
    // STEP 1a - SELECT BIDS
    string query =
    "SELECT t1.operator_id, t1.datetime, t1.plot_id, t1.position, t2.market_access FROM bid t1 " +
    "JOIN operator t2 ON t1.operator_id = t2.id WHERE t1.status='Queued' AND t1.postcode=@postcode " +
    "ORDER BY t2.market_access ASC, t1.datetime ASC";
    var bidList = new List<BidList>();
    var cmd = new MySqlCommand(query, _connection);
    cmd.Parameters.AddWithValue(("@postcode"), _plot);
    MySqlDataReader dataReader = cmd.ExecuteReader();
    while (dataReader.Read())
    var item = new BidList
    OperatorId = dataReader["operator_id"] + "",
    PlotId = dataReader["plot_id"] + "",
    Position = dataReader["position"] + "",
    Datetime = dataReader["datetime"] + "",
    MarketAccess = dataReader["market_access"] + "",
    bidList.Add(item);
    dataReader.Close();
    // STEP 1b - SET PRIORITIES
    for (var i = 0; i < bidList.Count; i++)
    var position = i + 1;
    bidList[i].Position = position.ToString();
    query = "UPDATE bid SET position=@position WHERE status='Queued' AND postcode=@postcode AND operator_id=@operator_id;";
    cmd = new MySqlCommand(query, _connection);
    cmd.Parameters.AddWithValue(("@position"), position);
    cmd.Parameters.AddWithValue(("@postcode"), _plot);
    cmd.Parameters.AddWithValue(("@operator_id"), bidList[i].OperatorId);
    cmd.ExecuteNonQuery();
    dataReader.Close();
    // STEP 2 - ALLOCATE JOBS ACCORDING TO PRIORITY
    foreach (var t in bidList)
    query = "SELECT operator_id, plot_id, status FROM booking " +
    "WHERE status='open' AND postcode=@postcode AND operator_id='0'" +
    "ORDER BY datetime ASC;" +
    "UPDATE booking SET operator_id=@operator_id, status='Allocated' " +
    "WHERE (plot_id=@plot_id AND operator_id='0' AND status='Open') LIMIT 1;";
    cmd = new MySqlCommand(query, _connection);
    cmd.Parameters.AddWithValue(("@operator_id"), t.OperatorId);
    cmd.Parameters.AddWithValue(("@postcode"), _plot);
    cmd.Parameters.AddWithValue(("@plot_id"), t.PlotId);
    cmd.ExecuteNonQuery();
    dataReader.Close();
    // STEP 3
    CloseConnection();

    I can't tell.  When modifying row(s) you have to be able to uniquely be able to identify the row(s) in the database you want to modify.  That is why I recommended the other day to use DataAdapter instead of the DataReader.  You can with the
    reader, but you need to unique identify the rows.
    I can't tell from the limited amount of data you posted if in table 2 that there is only one row with the same operator id and plot id.  It looks like in table 2 the 'id' number is unique.  When working with a database with multiple tables you
    need to be able to link tables together with primary keys (columns wit unique values).  If the database isn't designed properly then it is impossible to do some operations.  So you always have to make sure you design a database properly.
    jdweng
    When you say table 2 are you referring to the bid or booking table?
    Here is the table structure for the bid table
    Here is the table structure for the booking table
    Hope it helps makes things clearer!

  • How to Use the Procedures in a Sql Query

    Hi Friends,
    Can anyone help me out whether can we use the procedure in the sql query..
    if yes help me out with an example
    my requirement is
    i have one sql query .. in which i need to use the procedure which returns multiple values... how can i overcome it,can anyone help me out for this..
    for your reference i am pasting the sql query
    SELECT paf.person_id
    FROM per_all_assignments_f paf START WITH paf.person_id = p_person_id
    AND paf.primary_flag = 'Y'
    AND paf.assignment_type IN('E', 'C')
    AND l_effective_date BETWEEN paf.effective_start_date
    AND paf.effective_end_date
    CONNECT BY PRIOR paf.supervisor_id = paf.person_id
    AND paf.primary_flag = 'Y'
    AND paf.assignment_type IN('E', 'C')
    AND l_effective_date BETWEEN paf.effective_start_date
    AND paf.effective_end_date
    and paf.person_id not in (>>>I HAVE TO USE THE PROCEDURE HERE<<<<);
    Thanks in advance

    We never saw your procedure, but maybe you could wrap it in a function
    SQL> create or replace procedure get_members(in_something IN number, out_members OUT sys_refcursor)
    is
    begin
      open out_members for
        'select level member_id from dual connect by level <= :num' using in_something;
    end get_members;
    Procedure created.
    SQL> create or replace type numbers as table of number;
    Type created.
    SQL> create or replace function members(in_something IN number)
    return numbers
    as
      member_cur sys_refcursor;
      members numbers;
    begin
      get_members(in_something, member_cur);
      fetch member_cur bulk collect into members;
      close member_cur;
      return members;
    end;
    Function created.
    SQL> select * from  table(members(4));
    COLUMN_VALUE
               1
               2
               3
               4
    4 rows selected.Variant on same using piplined function
    SQL> create or replace function members_piped(in_something IN number)
    return numbers pipelined
    as
      member_cur sys_refcursor;
      rec number;
    begin
      get_members(in_something, member_cur);
      loop
         fetch member_cur into rec;
         exit when member_cur%notfound;
         pipe row(rec);
      end loop;
      close member_cur;
      return;
    end;
    Function created.
    SQL> select * from  table(members_piped(4));
    COLUMN_VALUE
               1
               2
               3
               4
    4 rows selected.
    SQL> drop function members_piped;
    Function dropped.
    SQL> drop function members;
    Function dropped.
    SQL> drop type numbers;
    Type dropped.
    SQL> drop procedure get_members;
    Procedure droppedEdit:
    Sorry Blu, had not seen you already posted similar thing
    Edited by: Peter on Jan 27, 2011 5:38 AM

  • Pass the result of a SQL Query as table_name  for another SQL Query

    Hi All,
    How to pass the result of a SQL Query as parameter to another SQL Query
    Eg: I am doing the steps below.
    1) select distinct table_name as TAB1 from all_tab_cols where table_name like 'T_%' and column_name like '%XYZ'
    2) I want to pass the table_name from step 1 above as parameter to another query "select * from TAB1"
    Thanks

    Naveen B wrote:
    Hi All,
    How to pass the result of a SQL Query as parameter to another SQL Query
    Eg: I am doing the steps below.
    1) select distinct table_name as TAB1 from all_tab_cols where table_name like 'T_%' and column_name like '%XYZ'
    2) I want to pass the table_name from step 1 above as parameter to another query "select * from TAB1"
    ThanksYou should craete PL/SQL code with cursor which will accept a parameter and call that cursor inside the first one
    But if the first sql returns only one row, you can do it with simple sql code
    select * from (select distinct table_name as TAB1 from all_tab_cols where table_name like 'T_%' and column_name like '%XYZ')- - - - - - - - - - - - - - - - - - - - -
    Kamran Agayev A. (10g OCP)
    http://kamranagayev.wordpress.com
    [Step by Step install Oracle on Linux and Automate the installation using Shell Script |http://kamranagayev.wordpress.com/2009/05/01/step-by-step-installing-oracle-database-10g-release-2-on-linux-centos-and-automate-the-installation-using-linux-shell-script/]

  • How to use the selection profile and status profile for production order?

    Hi expert,
       I want to know how to use the selection profile and status profile for production order. what's the usage for these two selection profile and status profile ?
      Please help me.
      thanks in advance.
      george.shi

    Hi George,
    There are are two types of statuses.One is system status and second one is user status.These statuses will tell us current situation of an order.
    We can't change system statuses.But we can create our own statuses through status profile.With this profile we can control user statuses.
    In this status profile,
    1.We define the sequence in which user statuses can be activated,
    2.We define initial statuses
    3. Allow or prohibit certain business transactions.
    Selection profiles are used to select the objects (say production orders) with different status combinations.We assign status profiles to selection profiles in BS42 T-Code.
    Regards,
    Raja.
    Edited by: Rajarao on Oct 30, 2008 6:21 AM
    Edited by: Rajarao on Oct 30, 2008 6:22 AM

  • End the result of an sql query by Email as an excel file attachement

    Hi,
    I would like to create a PL/SQL function that send the result of an sql query by Email as an excel file attachement.
    i'm newbie in pl/sql an d i dont know if it's possible to do such task.
    regards,

    i think a regular expression is he way to go in your case...
    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com

  • HT1343 how to use the options with F10, F11, F11 for turning the sound up or down or mute?

    Hi, I'm Hannah. I'm using a Mac. Can you show me how to use the options with F10, F11, F12 for turning the sound up, or down or mute? Thank you very much

    Normally simply pressing them should do what you want, F10 to mute; F11 to decrease volume; F12 to increase volume. However, it's possible that you have a box ticked in Keyboard preferences which modifies the behaviour of the keys, requiring you to also hold down the Fn key (bottom left key on the keyboard) to enable the function.
    Check System Preferences>Keyboard to makes sure the box indicated in the image isn't ticked.

  • How to export the result from executing sql statement to excel file ?

    HI all,
    Great with Oracle SQL Developer, but I have have a trouble as follwing :
    I want to export the result from executing sql statement to excel file . I do easily like that in TOAD ,
    anyone can help me to do that ? Thanks so much
    Sigmasvn

    Hello Sue,
    I just tried to export to excel with the esdev extension and got java.lang.NumberFormatException. I found the workaround at Re: Windows Multi-language env, - how do I set English for application lang?
    open the file sqldeveloper\jdev\bin\sqldeveloper.conf and add the following two lines:
    AddVMOption -Duser.language=en
    AddVMOption -Duser.country=USyet now my date formats in excel are 'american-style' instead of german. For example 01-DEC-01 so excel does not recognize it as date and therefore I can not simply change the format.
    When export to excel will be native to 1.1 perhaps someone can have a look at this 'feature'
    Regards
    Marcus

  • How to use the result from a taglib?

    Hi people.
    I've been looking arround for some feedback on how to use the output generated by a taglib on jsp code but I haven't been able to find any.
    Could somebody tell me how to do that please?

    It works something like this:
    The tab library consists of a class library and a tag libraray descriptor file. In your jsp header you include a taglib directive which associated athe TLD with a particular name prefix. Then you can include XML tags with that prefix and the tag library class to which they are connected will be invoked from the servlet that is generated from the JSP. This tag code can do pretty much anything, but usually what it mostly does is write HTML to the response stream. Opening tags and closing tags can both add whatever text they like to the stream which is sent to the client.
    So including the tag in you JSP will usually suffice to cause generated output, it's up to the taglib.

  • How to Suppress The Output of a SQL Query In Oracle 11gR2

    Hi Friends,
    I am using oracle version 11.2.0.1, I have set a cronjob which will run on every 15 minutes and give us a log file mentioning the execution time taken for that SQL query:-
    For example:
    SQL> set timing on;
    SQL> SELECT objProp FROM aradmin.arschema WHERE (schemaId = 175);
    OBJPROP+
    --------------------------------------------------------------------------------+
    *6\60006\4\0\\60008\40\0\60009\4\0\\60010\4\0\\60018\4\0\\600*
    *22\4\68\1\63\AR:jRL#*
    Elapsed: 00:00:00.00
    The above query will return the output as well as the time taken for execution of the query. I want to suppress the output of the query and only want the time taken to be printed. Is it possible by set commands. I have marked the output as bold and made it Italic.
    Please help me at the earliest.
    Regards,
    Arijit

    >
    I am using oracle version 11.2.0.1, I have set a cronjob which will run on every 15 minutes and give us a log file mentioning the execution time taken for that SQL query:-
    The above query will return the output as well as the time taken for execution of the query. I want to suppress the output of the query and only want the time taken to be printed. Is it possible by set commands. I have marked the output as bold and made it Italic.
    >
    How would that even be useful?
    A query from a tool such as sql*plus is STILL going to send the output to the client and the client. You can keep sql*plus from actually displaying the data by setting autotrace to trace only.
    But that TIME TAKEN is still going to include the network time it takes to send ALL rows that the query returns across the network.
    That time is NOT the same as the actual execution time of the query. So unless you are trying to determine how long it takes to send the data over the network your 'timing' method is rather flawed.
    Why don't you tell us WHAT PROBLEM you are trying to solve so we can help you solve it?

  • How to use the update mechanism in Photoshop CS10 for Camera Raw?

    Good Evenning!
    I would like to open .NEF files with Adobe Photoshop CS10 (RAW for Nikon D5200) but my version of Camera Raw is too old.
    I need at least the 7.3 version.
    When I try to download it, I have this message: Please use the update mechanism in Photoshop CS10 for Camera Raw. But how?? When I go to "Help" tab, to "Updates", it says that no update available.
    Can please someone help me?
    Thanks a lot!!
    Kinds Regards
    Vanessa

    Which version of photoshop or photoshop elements are you using?
    Which operating system?
    There is no such version as cs10.
    Do you mean photoshop shop elements 10?
    If you go to Help>About Photoshop Elements (windows) or Photoshop Elements Editor>About Photoshop Elements (mac), that should tell you which version you have.

  • How to view the result of any SQL script

    Hi
    I am working on Oracle 10 g Express Edition.I have an explicit cursor code ,a very basic one.I want to know how can i view result of the script.When i Save and run the script i get nothign on the window.
    I am pasting the script for the Reference.
    Code
    Declare
    CURSOR c_p IS
    select p_id from PRODUCTS;
    v_pid PRODUCTS.p_id%type;
    Begin
    OPEN c_p;
    Loop
    FETCH c_p into v_pid;
    Exit when c_p%NOTFOUND;
    Dbms_output.put_line(v_pid);
    End loop;
    CLOSE c_p;
    End;
    Prod_id Prod_category Prod_name Prod_price
    123456 Games Sony PS3 599.99
    234567 Games Sony PSP 249.99
    345678 Games Nintendo Wii 249.99
    456789 Games Microsoft Xbox 360 349.99
    567890 Computer Microsoft Vista 349.99

    Hi,
    Try like this:
    SQL> set serveroutput on
    SQL> DECLARE
      2  CURSOR c_p IS
      3  select first_name from EMP;
      4  v_name EMP.first_name%type;
      5  Begin
      6  OPEN c_p;
      7  Loop
      8  FETCH c_p into v_name;
      9  Exit when c_p%NOTFOUND;
    10  Dbms_output.put_line(v_name);
    11  End loop;
    12  CLOSE c_p;
    13  End;
    14
    15
    16  /
    Steven
    Neena
    Lex
    Alexander
    Bruce
    David
    Valli
    Diana
    Nancy
    Daniel
    John
    Ismael
    Jose Manuel
    Luis
    Den
    Alexander
    Shelli
    Sigal
    Guy
    Karen
    Matthew
    Adam
    Payam
    Shanta
    Kevin
    Julia
    Irene
    James
    Steven
    Laura
    Mozhe
    James
    TJ
    Jason
    Michael
    Ki
    Hazel
    Renske
    Stephen
    John
    Joshua
    Trenna
    Curtis
    Randall
    Peter
    John
    Karen
    Alberto
    Gerald
    Eleni
    Peter
    David
    Peter
    Christopher
    Nanette
    Oliver
    Janette
    Patrick
    Allan
    Lindsey
    Louise
    Sarath
    Clara
    Danielle
    Mattea
    David
    Sundar
    Amit
    Lisa
    Harrison
    Tayler
    William
    Elizabeth
    Sundita
    Ellen
    Alyssa
    Jonathon
    Jack
    Kimberely
    Charles
    Winston
    Jean
    Martha
    Girard
    Nandita
    Alexis
    Julia
    Anthony
    Kelly
    Jennifer
    Timothy
    Randall
    Sarah
    Britney
    Samuel
    Vance
    Alana
    Kevin
    Donald
    Douglas
    Jennifer
    Michael
    Pat
    Susan
    Hermann
    Shelley
    William
    PL/SQL procedure successfully completed.
    SQL>Cheers,

  • How to Use the 'Result' line to caculate a special value?

    Hi experts:
            For example,the dimension is product (a ,b c ,d),and key figure is revenue,
    then I want to show the value -->the percentage of the revenue of each product .
       so that's the problem , how I can get it,or how i can get the sum of all products.
    can I get it from the result line or is there a better way to get it?
    thx
    chan

    keep your Product in Rows, Revenue in Colomns.
    You can get Sum of each Product Revenue by settings Product Charecterstic Properties.
    Coming to % Share of Revenue for each product. Create a Calculated KF or Formula in Colomns. In the Formula or CKF definition screen, you can see Percentage Functions along with Mathemetical Functions.  Select % Share of Over all Result (%GT) under Percentage Functions .
    Take a look into this link if you have any Questions.<a href="http://help.sap.com/saphelp_nw04s/helpdata/en/e2/16f13a2f160f28e10000000a114084/content.htm">http://help.sap.com/saphelp_nw04s/helpdata/en/e2/16f13a2f160f28e10000000a114084/content.htm</a>
    Hope this helps.
    Nagesh Ganisetti.
    Assign points if it helps.

  • How to use the result of simple shell script?

    The shell script below retrieves the length of an audio file:
    set aFile to choose file
    do shell script "afinfo " & quoted form of (POSIX path of aFile) & "|grep duration"
    I'm wondering, how can I copy the result to the clipboard or set the value of a variable to it?
    Total newbie question. I have no idea about shell scripts - I just found the script above online.
    Thank you so much!

    Here:
    set the clipboard to (do shell script "afinfo " & quoted form of (POSIX path of aFile) & "|grep duration")
    or:
    set A to do shell script "afinfo " & quoted form of (POSIX path of aFile) & "|grep duration"
    (53997)

  • How to use the cell editor in FICO reports for YTD

    HI experts,
    I am working with BI7.0, in the below report, i have displayed result based on user input.but how to calculate the YTD values. User will give the input like 072007, results will be display one year from 072007 to 062006 (one yearback) and one more column is YTD
    Report structure is :
         user input(single value): 072007
              AUG06....JAN07....JULY07....YTD
    KEYFIGURE-1        453      -
    777     -
       232       -
      777 (Only Jan07 value of Keyfigure-1)   
    KEYFIGURE-2        879      -
    233        -
    123       -
      ???? (only sum from Jan07 to July07)
    KEYFIGURE-3        212            -
    879      -
    989                -
    KEYFIGURE-4        234            -
    656      -
    788                   -
    KEYFIGURE-5        345            -
    878      -
    878                 -
    KEYFIGURE-6        767            -
    767       -
    323                 -
    KEYFIGURE-7        879            -
    878     -
    999                -
    999 (Only last value of keyfigure-7 (July07)
    in the above report, total 7 keyfigures so 7rows of YTD column
    1) in the first YTD column, how will display only one value (keyfigure-1) of Jan07?
    2) in the last YTD Column, how will display only one value (keyfigure-7) of July07?
    3) from 2 to 6 columns of YTD, how will display the sum from Jan07 to July07?
    Note: months will be changed based on user input(single Value)
    how can use cell editor for above the senior.
    if any option is availabel please let me know
    Thanks
    kishore

    I think the following should work:
    Context:
    Rows (node,c=0:n)
    --- rowIndex (integer)
    selectedRowIndex (integer)
    Bind the "selectedKey" property of the radio button (cell editor) to attribute "selectedRowIndex" (outside table data source node) and bind "keyToSelect" to attribute "Rows.rowIndex". Make sure that the "rowIndex" attribute will contain the index of the node element in node "Rows".
    Armin

Maybe you are looking for

  • Decimal places on forms

    The decimal places in General Settings>Display is set to 6 for Amounts and Prices.  They would like only 2 decimal places to acutally print on the SO and AR Invoice.  How can we do this?

  • Top Five Reasons on how to not get your iPod Stolen In N.Y.C.

    Ok Im doing this because you constantly hear about ppl getting robbed for their iPods, well here's how not to. 5. Keep Your iPod Out Of Sight! Listen to your Inner Voice if you feel Like someone's watching you Leave The iPod hidden. 4. Keep the Volum

  • How can I move pages around in an iPhoto book?

    Is there a way to move the pages around in an almost completed iBook?

  • How to switch between views in iPhone SDK

    I have been tasked to learn the iPhone SDK. I know Actionscript, Javascript and some Java, but I cannot understand the iPhone SDK at all. I cannot understand the syntax or how the multitude of files work together. Also, there are no tutorials for com

  • Order combination in Outbound delivery

    Hi, I have a problem. We have a lot of Sales orders with different Sold-to-Parties but all have the same Ship-to-party so we want all the orders to be combined in to one delivery when creating the deliveries in VL10A. But that is not the case, I get