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.
Related
I have an MS Access Database with a lot of tables (including data for certain units) and a lot of queries. I have another table of units that I want to investigate.
For now the queries apply to all units in the tables.
My goal is depending on the filter that I select in the unit table, execute all my queries taking into account in the where clause that only these units can be shown.
So my table is for example (the actual table is 5000 records)
Unit Detail
AA 1
BB 1
CC 3
If I filter in this table for Detail 1, I want all my queries to add in the where clause:
Where Unit="AA" or Unit="BB"
Is there a way to achieve this?
Thanks.
I have two tables (with same structure). One with 80 million records and other with 60 million records.
I want to delete records in 80m table that match in 60m table.
I use a sql query as below:
DELETE FROM tbl_80M
FROM tbl_80M INNER JOIN
tbl_60M ON tbl_80M.MobileNumber = tbl_60M.MobileNumber
In two tables, we have index on mobilenumber fields.
I run above query and it takes a long time .
Is there a better way to reach result in shorter time?
Note: tbl_80M has all of records that is in tbl_60M . I want to find and delete all records that are common in tbl_80M and tbl_60M.
Have you tried writing a query to insert those N million records into a new table and then drop the old tables.
Then at last you can rename new table to tbl_80M.
SELECT
* INTO tbl_NM
FROM tbl_80m a,
tbl_60m b
WHERE tbl_80M.MobileNumber = tbl_60M.MobileNumber
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
when i click SELECT TOP 1000 row from table then it only shows some records like 3 records
but when i manually run query on same table then it shows all records like many 1000s records which i always want.
Select * from dbo.HrEmployee
why ? Help please, i'm using SQL SERVER 2012
It look like you have created two copies of the same database, the one is in the “intended” database and the second has been created in the master database. 3 records were then inserted into the intended table and the rest were inserted into the master.dbo.HrEmployee.
When you use the select top 1000 you are running the query against the correct database even though it only has 3 records and when you run the second query you are running it against the same table in Master
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