I am trying to create a number of rows in a table based on the value of record in a different table.
i.e Table A has two columns NumberID, Number and TableB has MonthID, Month, Amount
When TableA.Number gets a value of say 5 I want TableB to be updated with 5 new rows. The values of TableB.Month and TableB.Amount will then be entered manually. Can this be done?
Depending upon what database you are using, this can be done using triggers.
Alternately, you could do this in the application layer.
Related
I get the feeling this is easy for SQL, but I'm new to it. I want to query table CUST_TRAN and find out the ID for value $1 which is in column INVOICE.
I then want to use the newly discovered ID to update table TRANSACTS to change the DATE and DATE_TIME (two values in two columns) to value $2 using the column IDFROMTRAN to match the ID against. There are very often two rows with the same IDFROMTRAN that need to be updated, because the customer signs for the goods and that creates its own row.
Does any of this make sense?
Basically, to explain what I'm doing I'm changing the date on transactions to put them into last month, so that they'll be invoiced correctly and reflect when the customer was actually in store.
TLDR: How do I find out an alternative ID for a transaction then use that to change the date in two rows in another table?
I want to add a new row for each existing rows. The new row will contain a new value for one of the column and same values for the remaining columns.
From the below table, I want to add a new 'ADUser' -> 'manju#apac.corpdir.net' for each 'Tag' value. Lets say, the current number of rows are 4. After the operation, the final number of rows should be 8.
I have been experimenting with some INSERT queries, but I am unsuccessful. Any help is highly appreciated.
Database: Azure SQL Database
Current Table Rows :
Expected Table Rows:
You can use insert:
insert into t (id, tag, aduser)
select id, t.tag, 'manju#apac.corpdir.net'
from t;
I am guessing that created is assigned automatically. If not, you could use:
insert into t (id, tag, aduser, created)
select t.tag, 'manju#apac.corpdir.net', current_timestamp
from t;
It seems odd that you want duplicate values for id, but that is what the question is asking for. This should really identify each row uniquely and be assigned automatically.
I want to update numerical columns of one table based on matching string columns from another table.i.e.,
I have a table (let's say table1) with 100 records containing 5 string (or text) columns and 10 numerical columns. Now I have another table that has the same structure (columns) and 20 records. In this, few records contain updated data of table1 i.e., numerical columns values are updated for these records and rest are new (both text and numerical columns).
I want to update numerical columns for records with the same text columns (in table1) and insert new data from table2 into table1 where text columns are also new.
I thought of taking an intersect of these two tables and then update but couldn't figure out the logic as how can I update the numerical columns.
Note: I don't have any primary or unique key columns.
Please help here.
Thanks in advance.
The simplest solution would be to use two separate queries, such as:
UPDATE b
SET b.[NumericColumn] = a.[NumericColumn],
etc...
FROM [dbo].[SourceTable] a
JOIN [dbo].[DestinationTable] b
ON a.[StringColumn1] = b.[StringColumn1]
AND a.[StringColumn2] = b.[StringColumn2] etc...
INSERT INTO [dbo].[DestinationTable] (
[NumericColumn],
[StringColumn1],
[StringColumn2],
etc...
)
SELECT a.[NumericColumn],
a.[StringColumn1],
a.[StringColumn2],
etc...
FROM [dbo].[SourceTable] a
LEFT JOIN [dbo].[DestinationTable] b
ON a.[StringColumn1] = b.[StringColumn1]
AND a.[StringColumn2] = b.[StringColumn2] etc...
WHERE b.[NumericColumn] IS NULL
--assumes that [NumericColumn] is non-nullable.
--If there are no non-nullable columns then you
--will have to structure your query differently
This will be effective if you are working with a small dataset that does not change very frequently and you are not worried about high contention.
There are still a number of issues with this approach - most notably what happens if either the source or destination table is accessed and/or modified while the update statement is running. Some of these issues can be worked around other ways but so much depends on the context of how the tables are used that it is difficult to provide a more effective generically-applicable solution.
I have a database table with 8 fields say Table(a,b,c,d,e,f,g,h).For some rows one of the field is different(say 4 different a values) while all other field values(b-h) in the schema are same.I have to make a table selecting just one rows from such rows with different a's but same b-h.That is I can select any one of the different a's and keep b-h same which they are and display it in table as 1 single row instead of 4.
SELECT MIN(a) a,b,c,d,e,f,g,h
FROM mytable
GROUP BY b,c,d,e,f,g,h
i'm trying to keep a record of which customers have received which newsletters. I have a query called
TempQuery
I want to take the field
TempQuery.CustID
That consists of several numerical id's and combine it with two static values specified on a form
Forms![frmAddCorrespondence]![txtNID]
Forms![frmAddCorrespondence]![txtDate]
such that I end up with something like
CustID NID Date
1 5 28/03/2011
3 5 28/03/2011
14 5 28/03/2011
56 5 28/03/2011
Again the fields NID and Date will be the same values for each individual insert specified on frmAddCorrespondence and the CustID's are pulled from TempQuery.
I'd like this to be a query from which the data can be inserted into a log table of all our past correspondences.
Any help is greatly appreciated
Thanks, Rob
you want a full outer join between your temp query and the date and ID entered at runtime?
If so, you can pass the two new column values (ID and date) as parameters to your query. Just create a new query based on your existing one and add the two control references as new columns.
Is that what you wanted?
In response to your comment, yes, you can create a second append query in Access that takes all the columns from your query and adds the two parameters from the controls on your form.