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 6528643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:33:13+00:00 2026-05-25T09:33:13+00:00

A table contains something like the following: ID: 4 DATE: 040910 COUNT: 42 If

  • 0

A table contains something like the following:

   ID: 4 
 DATE: 040910 
COUNT: 42

If the ID and DATE already exist in the table, increase the count by one.
If the specific ID and DATE do not already exist, create with a count of 1.
‘ID’ is not unique – it may be duplicated in the table as long as the date code changes.

Everytime I think of a solution, it seems to incur possible race conditions. I thought about BEGIN/END transaction, but that doesn’t seem 100% reliable either. I can’t just do the insert and if it fails then update unless I merge the ID field into DATE and make it a unique column.

Ideas?

Edit: I’ve just thought of this:

INSERT INTO table (...) VALUES (...)
WHERE NOT EXISTS (SELECT 1 FROM table WHERE table.id == x AND table.date == y)

UPDATE table SET count = count + 1 WHERE table.id == x AND table.date == y

So the update is always executed, but the insert depends on the current value of the table, but I’m concerned that still 2 records could appear with the same id & date.

  • 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-05-25T09:33:13+00:00Added an answer on May 25, 2026 at 9:33 am

    What is wrong with transactions? Unless there’s a bug in SQL Server, they will work as described. You can also set the ISOLATION LEVEL of a transaction to suit your needs.

    BEGIN TRANSACTION;
    
    UPDATE YourTable
       SET Count = Count + 1
     WHERE ID = X AND Date = Y;
    
    IF @@rowcount = 0
        INSERT INTO YourTable (ID, Date, Count)
        VALUES (X, Y, 1);
    
    COMMIT TRANSACTION;
    

    You should also have a unique constraint on both columns (ie: UNIQUE(ID, DATE))

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Settings table contains always one row with lot of long-caption columns. Opening this in
create table foo (id int(11) auto_increment, value varchar(255)) The table contains roughly 10,00,000 rows
I'm mapping a objectified relationship (the many->many mapping table contains properties), following this guide.
I've got a question regarding a SQL-select-query: The table contains several columns, one of
I have three tables something like the following: Customer (CustomerID, AddressState) Account (AccountID, CustomerID,
A hypothetical table contains the following coloumns: id integer, name char(6), status integer And
I have a table that contains the following three rows name ---------------------------------------------- user -
I have an html table where one of the columns contains <span> s with
There is 'team_sector' table with following fields: Id, team_id, sect_id, size, level It contains
I have a postgres table that has the following fields start_date,duration duration contains any

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.