How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL?

RG20

I have two tables, Table 1 and Table 2. Table 1 have columns "start" and "end" . Table 2 has column "position" and "Sequence". I would like to extract the sequences from Table 2 from position = start to position = end and the create a new column with the concatenated string.

Table 1

Start End
100 104
105 109

Table 2

Position Seq
100 A
101 T
102 C
103 T
104 G
105 T
106 T
107 G
108 T
109 G

My final result needs to be

Start End Sequence
100 104 ATCTG
105 109 TTGTG

I tried concatenating the values in the Table 2 using the below statement

 SELECT Sequence = (Select '' + Seq 
 from Table2
 where Position >= 100 and Position <= 104
 order by Position FOR XML PATH(''))

But I am not sure how to perform this in the entire table. Thank you very much for all the helpp

SEarle1986

You don't state what DBMS you are using so here is a SQL Server solution using a CTE and FOR XML to perform the transpose:

; WITH SequenceCTE AS
(
    SELECT  [Start],
            [End],
            Seq
    FROM    Table1 a
            JOIN Table2 b
                ON b.Position >= a.[Start] AND
                  b.Position <= a.[End]
)
SELECT  DISTINCT
        a.[Start],
        a.[End],
        (
            SELECT  STUFF(',' + Seq,1,1,'')
            FROM    SequenceCTE b
            WHERE   a.[Start] = b.[Start] AND
                    a.[End] = b.[end]
            FOR XML PATH ('') 
        )
FROM    SequenceCTE a

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Removing duplicate rows (based on values from multiple columns) from SQL table

From Dev

How to update multiple columns based on values from an associated table?

From Dev

Concatenate A Row from columns of another table with multiple row

From Dev

How to add multiple rows of data from one sql table to another with selection based on checkboxes?

From Dev

SQL: How to select multiple values from one table as seperate columns

From Dev

Update multiple columns of a table using aggregate values from another table

From Dev

Update multiple columns of a table using aggregate values from another table

From Dev

Extract one column from sql table and insert into another table as multiple records coupled with different values

From Dev

Removal of Duplicate Rows from Data table Based on Multiple columns

From Dev

Jquery to hide table rows depending on multiple values from different columns

From Dev

Jquery to hide table rows depending on multiple values from different columns

From Dev

INSERT rows multiple times based on a column value from another table

From Dev

How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

From Dev

Update multiple rows in one table with differing values from another

From Dev

Update all rows on table based on value from other table that gets multiple values from third table

From Dev

Flatten a SQL Table from rows to columns allowing multiple results

From Dev

SQL insert multiple rows from single field in another table

From Dev

Sql server update multiple columns from another table

From Dev

Copying multiple columns from one SQL table to another

From Dev

Insert multiple rows from select into another table

From Dev

Field involving multiple rows from another table

From Dev

MYSQL - UPDATE multiple rows from another table

From Dev

insert multiple rows mysql from another table

From Dev

Select multiple rows from table based on ID

From Dev

Getting a value from another table for multiple columns

From Dev

Updating columns values from another table SQL

From Dev

How to get all the rows from MySQL table which have same values for multiple columns?

From Dev

JQuery: How to drag multiple rows from one table to another?

From Dev

JQuery: How to drag multiple rows from one table to another?

Related Related

  1. 1

    Removing duplicate rows (based on values from multiple columns) from SQL table

  2. 2

    How to update multiple columns based on values from an associated table?

  3. 3

    Concatenate A Row from columns of another table with multiple row

  4. 4

    How to add multiple rows of data from one sql table to another with selection based on checkboxes?

  5. 5

    SQL: How to select multiple values from one table as seperate columns

  6. 6

    Update multiple columns of a table using aggregate values from another table

  7. 7

    Update multiple columns of a table using aggregate values from another table

  8. 8

    Extract one column from sql table and insert into another table as multiple records coupled with different values

  9. 9

    Removal of Duplicate Rows from Data table Based on Multiple columns

  10. 10

    Jquery to hide table rows depending on multiple values from different columns

  11. 11

    Jquery to hide table rows depending on multiple values from different columns

  12. 12

    INSERT rows multiple times based on a column value from another table

  13. 13

    How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

  14. 14

    Update multiple rows in one table with differing values from another

  15. 15

    Update all rows on table based on value from other table that gets multiple values from third table

  16. 16

    Flatten a SQL Table from rows to columns allowing multiple results

  17. 17

    SQL insert multiple rows from single field in another table

  18. 18

    Sql server update multiple columns from another table

  19. 19

    Copying multiple columns from one SQL table to another

  20. 20

    Insert multiple rows from select into another table

  21. 21

    Field involving multiple rows from another table

  22. 22

    MYSQL - UPDATE multiple rows from another table

  23. 23

    insert multiple rows mysql from another table

  24. 24

    Select multiple rows from table based on ID

  25. 25

    Getting a value from another table for multiple columns

  26. 26

    Updating columns values from another table SQL

  27. 27

    How to get all the rows from MySQL table which have same values for multiple columns?

  28. 28

    JQuery: How to drag multiple rows from one table to another?

  29. 29

    JQuery: How to drag multiple rows from one table to another?

HotTag

Archive