Skip to content
Advertisement

SQL Server query to group records and insert in other tables

I am using SQL Server database which has tables x, y and mapping table xy.

I have a table X with x_id, date, text and receiver id and mapping table xy with x_id and y_id. I need a script to generate XY and Y tables in such a way that for each unique text there should be a record in y table and based on number of receivers there should be mapping in XY table. date column in Y table should be populated with shortest date for that specific text.

Table: x

So, final data in XY & Y for the above records should end up with

Table: Y

Table: XY

Advertisement

Answer

first get the data fro Y table and insert, I have used Row Number for Id of Y table, you can create identity for that if you want. second select data for XY table with join with X and Y table text property.

please do not use any keyword for field name like text or date.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement