473,320 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to count the number of records in one table and update the count to another table

1
I'm using MS SQL and I'm challenged with how update a table based on the count of records from another. I have a couple transactions for an id and I want to count total number and multiply by 4 or what I was trying to do below was to add 4 for each instance of a record with the same id. The code below is only adding 4 once if there are two transactions for an id instead of multiple times if there is more than one transaction.

==
update tmp
set issuecount = tmp.issuecount + 4, lastpayment = t.transaction_date
from (name n inner join trans t on t.st_id = n.id) inner join #temp_memdir tmp on tmp.id = n.id
where t.product_code like 'jh%' and t.transaction_date between '6/16/2006' and '8/13/2006'
==
Sep 4 '07 #1
1 3757
azimmer
200 Expert 100+
I'm using MS SQL and I'm challenged with how update a table based on the count of records from another. I have a couple transactions for an id and I want to count total number and multiply by 4 or what I was trying to do below was to add 4 for each instance of a record with the same id. The code below is only adding 4 once if there are two transactions for an id instead of multiple times if there is more than one transaction.

==
update tmp
set issuecount = tmp.issuecount + 4, lastpayment = t.transaction_date
from (name n inner join trans t on t.st_id = n.id) inner join #temp_memdir tmp on tmp.id = n.id
where t.product_code like 'jh%' and t.transaction_date between '6/16/2006' and '8/13/2006'
==
You can use a SELECT to generate the value you need, like this:
Expand|Select|Wrap|Line Numbers
  1. update tmp
  2. set issuecount = (SELECT 4*count(*) FROM myTransactionTable where tmp.id = myTransactionTable.id), ...
  3.  
Sep 5 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Bri | last post by:
Greetings, After making various edits and deletes on aproximately 40,000 records in one table (on the Design Master) syncronization fails with Error 3052 - File Sharing Lock Count Exceeded....
0
by: DataFreakFromUtah | last post by:
Hello! No question here, just a procedure for the archive. Search critera: count records imported count data imported count number of rows imported count number of records imported record import...
5
by: katiechoy | last post by:
Hello everyone Im new to this so im just going to explain my problem. I have created a query that counts the number of times a supervisors name appears in the client table. (Each client is...
4
by: Anuradha | last post by:
Dear Experts How can I count records in a data reader? Thx Anuradha
2
by: Rich | last post by:
Hello, Is there a way to capture the Records Affected count when performing a table Update on a Sql Server table using a DataAdapter? How is this done? Thanks, Rich
5
by: MN | last post by:
Hello, I have a customer table and another table that I need to prepopulate with special customer IDs, unique and not sequential. Is there a way to configure Access to assign the customer ID to...
1
by: jlrolin | last post by:
I'm trying to update a new field in a table from a COUNT(*) of Registration IDs grouped by Course IDs. COUNT: Course_ID 11 1234 12 2323 19 8932 ...
9
by: MLH | last post by:
A mailing list table in its virgin state contained sequential, consecutive integers in an autonumber field (A97). I've deleted records throughout the table. Now I would like to identify each...
6
markrawlingson
by: markrawlingson | last post by:
Hopefully someone can help me out with this, it's driving me nuts... I have two tables - We'll call them table1 and table2. Table1 holds event information, and table2 holds user registration data...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.