locked
Problem in Query? RRS feed

  • Question

  • User-1026236167 posted

    hello where is error in my query where userid column  count userid + 1 and provide result

    please execute them?


    SELECT MAX(user_id) count + 1 as user_id FROM tbl_dummy

    Sunday, June 7, 2020 9:00 AM

Answers

  • User-939850651 posted

    Hi, prabhjot1313

    The main error of your query is on the count keyword, which is a sql function used to count the number of records.

    So your syntax is wrong, and your query cannot be executed correctly.

    More details, you could refer this document.

    On the other hand, because I don’t know the structure of the data table and the data in the table, based on your query, I guess you might want to do this:

    • Query the maximum number of user_id records and add 1 to the number of records.

             If this is the case,the query should like this:

    select count(user_id)+1 as [count] from tbl_dummy where id = (select max(user_id) from  tbl_dummy)
    • Query the value of the largest user_id and add 1 to this value

             If this is the case,the query should like this:

    select max(user_id)+1 as user_id from tbl_dummy

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 8, 2020 7:26 AM

All replies

  • User475983607 posted

    hello where is error in my query where userid column  count userid + 1 and provide result

    please execute them?


    SELECT MAX(user_id) count + 1 as user_id FROM tbl_dummy

    I think you are looking for the following syntax.

    SELECT MAX(user_id) + 1 AS user_id 
    FROM tbl_dummy

    However, you probably should be using an IDENTITY column for user_id.  

    https://docs.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property?view=sql-server-ver15

    Sunday, June 7, 2020 11:19 AM
  • User-939850651 posted

    Hi, prabhjot1313

    The main error of your query is on the count keyword, which is a sql function used to count the number of records.

    So your syntax is wrong, and your query cannot be executed correctly.

    More details, you could refer this document.

    On the other hand, because I don’t know the structure of the data table and the data in the table, based on your query, I guess you might want to do this:

    • Query the maximum number of user_id records and add 1 to the number of records.

             If this is the case,the query should like this:

    select count(user_id)+1 as [count] from tbl_dummy where id = (select max(user_id) from  tbl_dummy)
    • Query the value of the largest user_id and add 1 to this value

             If this is the case,the query should like this:

    select max(user_id)+1 as user_id from tbl_dummy

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 8, 2020 7:26 AM