SQL Select Top and Random Fill - sql

I would like to select and insert the top 4650 fields from table a column g into table b column e. How can I randomly fill this column with data from table a column g? How do I replace the data that already exist in column e? Would this be easier to do in multiple parts?

if this is just for a single column then this should work for you.
insert into tableB (columnE)
select top(4650) columnG from tableA
if there is a relationship between the tables then you could do something like this
Update x
set columnE = y.columnG
from tableB as x
inner join (select top(4650) ID from tableA) as y
on x.ID = y.ID
you could also utilize CTE
;with as
(
select top (4650) id,ColumnG
from TableA
) Y
update X
set columnE = Y.ColumnG
from TableB as X
inner join Y on x.ID = y.ID
We will need the structure of the tables to fully answer your question

This answer assumes 2 things: A) your Products table has an unbroken auto-incrementing Id column (by unbroken I mean no deleted rows that would cause the int to jump) and b) your Orders has an int PK Id column
DECLARE #MaxProductId int
SELECT #MaxProductId = MAX(Id) FROM Product
SELECT p.Description, o.Id
FROM Product p
JOIN Order o
ON p.Id = o.Id % #MaxProductId
This will haphazardly join a product to an order. If you like what you see, you can change the p's column to whatever your g column is. You can then use UPDATE and this select to join on your Order's Id.

Well I actually went about this completely different. I made a temp table with 2 fields, Temp1, Temp2. I pulled all the Products and put them in Temp1 then used Two different updates. One to add random digits to Temp2 and one to change my TableToUpdate to those temp digits. This way I can do this to other tables and the data looks different but keeps the sales forecasting structure.
UPDATE TempTable
SET [Temp2] = abs(checksum(NewId())) % 5000 + 10000
UPDATE TableToUpdate
SET Product = ( SELECT Temp2
FROM TempTable
WHERE TableToUpdate.Product = TempTable.Temp1)
Thank you everyone for your input.

Related

ORACLE - Update a value from two different columns in differente tables with a filter

I'have a little question about a query.
I have to update a column from a table where there are only record of expense(integer).
I must increase the expense of 5% if the client is from a specific state, the column of the state is in a different table and the key in common is the address.
This is my query below :
UPDATE table 1 a
SET expense_vl = (
SELECT expense*1.05 FROM table 1
LEFT JOIN table2 b ON b.ADDRESS_ID=a.ADDRESS_ID
WHERE description_state IN 'lollyland'
)
I'd recommend using a semi-join:
update table_1 a
set expense_v1 = expense * 1.05
where exists (
select null
from table2 b
where
a.address_id = b.address_id and
b.description_state = 'lollyland'
)
Althought I must add that it would help if you include the DDL for your table. We're sort of guessing at which table "description" came from.
Also, when possible, include sample input for each table and desired output. We don't need a million records, just an example that illustrates your issue.
Try below
UPDATE table1 a SET expense_vl = (SELECT expense*1.05
FROM table2 b
WHERE b.ADDRESS_ID=a.ADDRESS_ID)
WHERE description_state IN 'lollyland'
Or try with subselect:
UPDATE table1
SET expense_vl = expense*1.05
WHERE ADDRESS_ID IN (SELECT ADDRESS_ID FROM table2 WHERE description_state IN 'lollyland')
I think you need to change your query like below :
UPDATE table 1 A
SET expense_vl=expense*1.05 FROM table 1
LEFT JOIN table2 B ON B.ADDRESS_ID=A.ADDRESS_ID
WHERE B.description_state IN 'lollyland'

Mapping an attribute in nvarchar to another column with int data type

I have this as legacy and it seems impossible to solve it in T-SQL ... (?).
I need to map one column of table A to another column of Table B, THIS mapping is done using a parametrization table. All 3 tables are in the same server, in the same application. The staging table is an intermediate table to receive external data, and then it will feed the final table clean, after the mapping.
Table_a (staging table)
ID NAME rack object
-----------------------------
1 x y zz:zz1
2 x y zz:zz2
table_b (clean table - BEFORE the mapping)
ID Name rack object
--------------------------
230 x y null
245 x y null
I want to achieve the next table_b after mapping.
Table_b (clean table - final table after mapping)
ID Name rack object
---------------------------
230 x y 10
245 x y 11
How to relate both tables if the Ids are different for the same Name/Rack. for example, table A has id 1 for name/Rack as x and y. The same case for the table B is under id 230.
table_parametrization
Id nameObject
---------------
10 zz:zz1
11 zz:zz2
The column object in table_a is of nvarchar type, and column object in table_b is of int type.
I do not even know how to achieve it in T-SQL.
I'm making some wild assumptions here, but I'll walk you through it.
If the connection between table_a and table_b is tenuous, and there are possible duplicate (name,rack) pairs, you might want to do the updates one at a time.
First, take the contents of your staging table and put them into a temp table. We're going to be deleting rows from the temp table during this process, so you probably don't want to do this directly with table_a. Once you have that, we're going to loop through the records with a WHILE loops, and update table_b one record at a time.
DECLARE #A_ID INT
WHILE EXISTS (SELECT TOP 1 1 FROM #table_a)
BEGIN
--Get one ID value at a time
SELECT TOP 1 #A_ID = id
FROM #table_a
--the CTE gets the first record from table_b that needs an [object] value set
;WITH Top1B AS
(
SELECT TOP 1 b.id, objectId = p.id
FROM #table_b b
INNER JOIN #table_a a ON a.[name] = b.[name] AND a.rack = b.rack
INNER JOIN #table_param p ON p.nameObject = a.object
WHERE a.id = #A_ID
AND b.object IS NULL
)
UPDATE b
SET b.[object] = tb.objectId
FROM Top1B tb
INNER JOIN #table_b b ON b.id = tb.id
--remove the record from the staging table
DELETE #table_a
WHERE id = #A_ID
END
Assuming that your table_b.ID attribute is an IDENTITY, and are automatically generated, you need to JOIN table_a to table_parameterization using the object. You'll get the values to insert into B using something like this:
SELECT
a.Name,
a.rack,
p.ID
FROM table_a_staging a
INNER JOIN table_parametrization p ON
a.object = p.nameObject

Comparing two columns in two different tables

I have two tables A and B where table A has a column X, table B has column Y. These columns contain account number information. I want to check whether the account number information in column A.X is present in B.Y.
Note: Both column X and Y can have duplicates because these are like composite primary keys.
How can I do solve this?
You can use an INNER JOIN, like this:
SELECT *
FROM table1 a
INNER JOIN table2 b
ON a.X = b.Y
OR
you can go for IF EXISTS,like this:
SELECT *
FROM table1 a
WHERE EXISTS(
SELECT 1
FROM table2 b
WHERE a.x=b.Y )
This will give you account information in table A that is also present in table B. The check is done by a correlated subquery that checks for each row in A whether the account information is present in B.
SELECT DISTINCT
X
FROM
A
WHERE
EXISTS (
SELECT
*
FROM
B
WHERE
B.Y=A.X
);
select distinct(X)
from A,B
WHERE A.X=B.Y
This will give you a list of account numbers in A that are not in B:
SELECT X FROM (
SELECT DISTINCT X FROM A
) A
LEFT JOIN B ON Y = X
WHERE Y IS NULL

Create writeable multi table select query in Access

I have two tables with below schema
Table 1 -
id, Serial Code, Hours
xxxx-aa, 1
xxxx-bb, 2
yyyy-aa, 1
Table 2 -
Stage , Description
aa, foo
bb, bar
Right now my joins are like this.
SELECT x.*, y.Description
FROM table1 x LEFT JOIN
table2 y
ON MID(serial, 6, 2) = y.stage
this gave me the desire result. however, it is readable only. I know I can create a writeable query if i separate out the foreign key in table 1 so the joining condition is
SELECT x.*, y.Description
FROM table1 x LEFT JOIN
table2 y
ON x.stage = y.stage
but is there a better way to create a writeable query without .. un-normalize.. table 1?
thanks.
One approach would be to create a calculated column called [stage] in table 1 and define it MID(serial,6,2). As you fill up the table, this column is automatically populated and allows you to query it simply as if you "un-normalized" table 1.

SQL MS Access Summing Results Based on Two Table Criteria

I've been asked to find if totals from one table exceed a certain value. However the identifier I need to group these is stored in another table. So I've figured how to isolate what I need and then copy into excel. However I do understand the principle of summing in SQL, as I've made my own queries that look like this:
SELECT * FROM
(SELECT ID, SUM(Table1.Amount) AS Subtotal FROM Table1 WHERE LineNumber = 1 GROUP BY ID) AS a, Table2 AS b
WHERE a.ID = b.ID
AND a.Subtotal > b.Threshold
In this case, the totals I need to sum all have the same LineNumber from the original table (Table1) so it's easy to compare to a value in a different table. What I want to do is be able to is join a subquery back to the original table, then GROUPBY the identifier and sum from the other table, adding a criteria that the original ID from table one must be a duplicate:
SELECT * FROM
((Table1 as t
INNER JOIN
(SELECT ID FROM Table1
GROUP BY ID HAVING COUNT(ID) >1) as b
ON t.ID = b.ID
Joining criteria back to the original table, I need to do this because the table I need to sum doesn't contain ID,
INNER JOIN Table2 as c
ON t.ID2 = c.ID2
WHERE c.LineNumber = 4
This is where I'm stuck. I want to sum all the LineNumber = 4 for each ID. Again, I had to join using ID2 because ID1 isn't in Table2
Perhaps I'm making this too complicated? Any suggestions would be welcome
Thanks!