Updating one SQL table based on data in another table - sql

I am running Microsoft SQL Server 2008 R2, and pulling information from two tables to create one new table.
Table A has leads with a unique lead number and other information.
Table B has sales with a unique sales number, and the lead number associated with it.
Data from both tables are pulled into temp tables in SQL Server so I can change and update whatever I need, and the output of this will go into a new table.
One lead from Table A can have multiple sales associated with it in table B.
I want to update the Number of Sales column in Table A (Leads) based on how many times that lead number appears in Table B (sales). So if Table B (sales) has a lead number tied to seven (7) sales, the Number of Sales column in Table A (leads) will be updated to 7.
I have tried a few variations using the COUNT function but with no success. Any help would be appreciated.

This should work for you assuming the field name is leadNo:
update tablea
set sales = (select count(*)
from tableb
where tableb.leadNo = tablea.leadNo)
SQL Fiddle Demo

Related

one-to-one tables relationship, linked by the autonumber in the main table

I have a main table that contain the customers details, I built another table that contain for example a yes/no fields about if the customer paid his taxes, the two tables is linked with autonumber from the main table.
I want always to keep them both with the same amount of records (that means every customer has a record in the second table even if the second table has empty record with data only in the primary key field)
I need that cause with missing records I cannot run update query to auto fill the second table and i got an error of validation rule violation.
I use this sql:
update clients LEFT JOIN MonthlyTbl ON clients.SerialNo = MonthlyTbl.serialno
set sReport04='ready';
I have almost 700 records in the main table and only 80 records in the second, and when I run the sql it updates only 80!!!!
Thanks for Help
Please use below query,
update clients set sReport04='ready' where SerialNo in
(select serialno from MonthlyTbl);
here is the right answer
first run the sql:
INSERT INTO monthlytbl ( serialno )
SELECT clients.serialno FROM clients
WHERE (((clients.[serialno]) Not In (select serialno from monthlytbl)));
and then:
select sreport04 from monthlytbl
set sReport04='ready';

SQL Linked Server Join

I have 2 different SQL databases "IM" and "TR". These have different schemas.
IM has the table "BAL" which has 2 columns "Account" and "Balance".
TR has the table "POS" which has 2 columns "AccountId" and "Position".
Here the common link is BAL.Account=POS.AccountId.
The POS table has > 100k records. BAL has few only records as it shows only accounts which are new.
I want to run a select query on IM Databases' BAL table as follows:
Database: IM
Select Account, Balance from BAL
However, here the "Balance" should return the results from TR Database POS.Position based on BAL.Account=POS.AccountId
How can I achieve this in the fastest manner by not slowing the databases and considering that this query will be executed by lot of users every now and then. Should I use OPENQUERY? I will use where clause to shorten the return time.

MS Access - create JOIN table to store value for every combination

I am working on an MS Access 2013 database. I have two tables:
Customers (28 records)
Chemicals (34 records)
I need to create a table for usage rates for each customer for each chemical.
The rates will be entered manually (at user's request). I am trying to determine how to create a new table where the customer-chemical fields will combine to be primary key.
The resulting table should have 28x34=952 unique records.
The goal is to then have a form wherein the user can select the customer, then the chemical, and edit the rate.
For any table/query creation I am comfortable using either the Access interface or SQL.
I will advise to create a new table containing 4 columns. The first column will be an 'id' it is going to be your primary key (auto-increment if you want), second column is the customer, then the chemical, and finally the rating. Then if you format your query to select 'rating' where customer='customer name' and chemical='chemical name', you should get the desired result you want.
Thank you for the reply. Did a little more wrestling with it and used the following SQL to create the table:
SELECT customers.customer, chemicals.chemical
INTO UsageRates
FROM Chemicals, Customers
Then adding a blank 'rate' field to the table.

SQL Query to count multiple values from one table into specific view

I like to request your help. I can get the results seperated but now i want to create a query which has it perfect for a external person. my explanation:
I have a statistics database with in this database a table when some records comes in and each records has several columns with values etc...
Now one of these columns is called "MT"
MT Column can have only one of the following values per records: A,B,C,D,E
The records also have a columne called TotalAmount which indicate a size of a value outside the database. This TotalAmount column is numeric without decimals and can have a value between 1 and 10.000.
And the last part is the records it self, the table has X amount of records.
So Basicly i need to create a query which seperates each MT value and calculates the amount of records per MT and the sum of TotalAmount.
This is on SQL Server 2005.
Many thanks for your assistance!
Very hard to guess without a full db schema. But I think you need.
SELECT MT, Count(*), SUM (TotalAmout)
FROM YourTable
GROUP BY MT

How to "prefer" data from one table over another in SQL, help reformulating.

Using Sql management Studio on Sql-Server 2005.
I guess my title is not very clear, so although I'd appreciate a direct answer, even better would be help in reformulating the question that I can search for an answer by myself and learn in the process.
Table A contains yearly total sales for various products. This table is updated once a year in Feb/March.There are two consequences to that: A product that started being sold in, say, July, will obviously only have a six month sale total for the year. Also, any new product will not appear in it before the following year.
Table B is an exception table that gives an estimate for all products not yet in table A or that have a less than a twelve months sale history.
Table C joins the yearly sales with various info from other tables and also contains calculated columns .
In pseudo code what I need is:
Use sales of product x from Table A in table C unless sales for this product exist in table B.
Basically, data from table B should take precedence over table A wether the record exists in A or not.
If you do a LEFT JOIN and no records exist in B, you will get a null for the column values in B. So to "prefer" valued that exist in B, use COALESCE:
SELECT COALESCE(B.Sales, C.Sales) AS Sales
FROM ...
Which loosely translates to "If B has a Sales value, use it, otherwise use the value from C."
Technically it means "take the first non-null value from B.Sales and C.Sales (in that order)"