简体   繁体   中英

Populating a column with rows from another table in SQL Server 2008

I'm trying to populate under a column in a table with the rows sequentially from another table one by one. Which means each data in a particular row must be added back to back into that column in the other table and then the next row should be added like this etc.

I tried every single way, searched everywhere but apparently one one has ever needed to do something like this before.

First, I tried to convert these rows into columns with pivot and then tried to import but failed again. Is there any proper way to do this?

All I know is that this won't be done with a simple

insert into tablename (columnname) 
    select *....

way I guess?

This is the example of the data table that I need to take values from. Each cell in each row must be taken back to back;

Defter No   Mezarlık    Sayfa No    Satır No    
--------------------------------------------
20-8        ÇAMLICA            1    1       
20-8        ÇAMLICA            1    2   

and then get inserted under "Value" column in this table;

TipId   EvrakId IndexId Value   KayitTarihi
--------------------------------------------
1         23      66     JohnMc   NULL
2         45      32     NewYork  NULL

Each column is nvarchar , so is Value column. So I want this table to be like this;

TipId   EvrakId IndexId Value   KayitTarihi
--------------------------------------------
1         23      66     JohnMc   NULL
2         45      32     NewYork  NULL
..        ..     ..      20-8     ..
..        ..     ..      ÇAMLICA  ..    
..        ..     ..      1        .. 
..        ..     ..      1        .. 
..        ..     ..      20-8     ..
..        ..     ..      ÇAMLICA  ..    
..        ..     ..      1        .. 
..        ..     ..      2        .. 

This is my first table's full structure, the one that includes data in it.

CREATE TABLE [dbo].[EvrakUstBilgileri](
[Evrak Id] [float] NULL,
[Defter No] [nvarchar](255) NULL,
[Mezarlık] [nvarchar](255) NULL,
[Sayfa No] [nvarchar](255) NULL,
[Satır No] [nvarchar](255) NULL,
[Ada No] [nvarchar](255) NULL,
[Mezar No] [nvarchar](255) NULL,
[Adı Soyadı] [nvarchar](255) NULL,
[Doğduğu Gün] [nvarchar](255) NULL,
[Yaş] [nvarchar](255) NULL,
[Cinsiyet] [nvarchar](255) NULL,
[Öldüğü Gün] [nvarchar](255) NULL,
[Gömüldüğü Gün] [nvarchar](255) NULL,
[Defin Ruhsatiye No] [nvarchar](255) NULL,
[Defin Ruhsatiye Tarihi] [nvarchar](255) NULL,
[Defin Ruhsatiye Verildiği Yer] [nvarchar](255) NULL,
[Eşleştirme Yapıldı] [nvarchar](255) NULL,
[Evrak Barkodu] [nvarchar](255) NULL,
[Okunamadı] [nvarchar](255) NULL,
[Not] [nvarchar](255) NULL,
[Belge Tarihi] [nvarchar](255) NULL
) ON [PRIMARY]

This is the second table that I need to populate.

CREATE TABLE [dbo].[TblEvrakTipValue](
[TipId] [int] NOT NULL,
[EvrakId] [int] NOT NULL,
[IndexId] [int] NOT NULL,
[Value] [nvarchar](250) NULL,
[KayitTarihi] [datetime] NULL
) ON [PRIMARY]

Both of these tables have the same "EvrakId" column which should act as if its a primary key.

This is the table 1 that already has data in it;

EvrakId DefterNo    Mezarlik    SayfaNo SatirNo
5018352 20-8        ÇAMLICA     1       1
5018353 20-8        ÇAMLICA     1       2
5018354 20-8        ÇAMLICA     1       3
5018355 20-8        ÇAMLICA     1       4
5018356 20-8        ÇAMLICA     1       5

IndexId structure should be like this;

TipID   EvrakID IndexID Value       KayitTarihi
1       23      66      JohnMc      NULL
NULL    23      67      20-8        NULL
NULL    23      68      ÇAMLICA     NULL
NULL    23      69      1           NULL
NULL    23      70      1           NULL
2       45      66      NewYork     NULL
NULL    45      67      20-8        NULL
NULL    45      68      ÇAMLICA     NULL
NULL    45      69      1           NULL
NULL    45      70      2           NULL
3       46      66      NewYork     NULL
NULL    46      67      20-8        NULL
NULL    46      68      NULL TEST   NULL
NULL    46      69      NULL        NULL
NULL    46      70      NULL        NULL

First real 20 data of IndexId...

TipId   EvrakId IndexId Value   KayitTarihi
14         1    388      NULL   NULL
14         2    336      NULL   NULL
14         3    408      NULL   NULL
14         4    83       NULL   NULL
14         5    402      NULL   NULL
14         6    403      NULL   NULL
14         7    404      NULL   NULL
14         8    228      NULL   NULL
14         9    390      NULL   NULL
14         10   419      NULL   NULL
14         11   406      NULL   NULL
14         12   392      NULL   NULL
14         13   389      NULL   NULL
14         14   401      NULL   NULL
14         15   362      NULL   NULL
14         16   407      NULL   NULL
14         17   391      NULL   NULL
14         18   400      NULL   NULL
14         19   242      NULL   NULL
14         20   92       NULL   NULL

Thanks for your help.

Here an example featuring the infamous unpivot :-P . As suggested by you, I nulled all other columns for the time being...

DECLARE @t1 TABLE(
  EvrakID NVARCHAR(100),
  DefterNo NVARCHAR(100),
  Mezarlik NVARCHAR(100),
  SayfaNo NVARCHAR(100),
  SatirNo NVARCHAR(100)
)

INSERT INTO @t1 VALUES
('23', '20-8'        ,'ÇAMLICA',            '1'    ,'1')       
,('45', '20-8'        ,'ÇAMLICA',            '1'    ,'2')  
,('46', '20-8'        ,'NULL TEST',            NULL    ,NULL)  


DECLARE @t2 TABLE(
  TipID NVARCHAR(100),
  EvrakID NVARCHAR(100),
  IndexID NVARCHAR(100),
  Value NVARCHAR(100),
  KayitTarihi NVARCHAR(100)
)

INSERT INTO @t2 VALUES
('1'         ,'23'      ,'66'    ,'JohnMc',   NULL)
,('2'         ,'45'      ,'32'    ,'NewYork',  NULL)
,('3'         ,'46'      ,'32'    ,'NewYork',  NULL)

DECLARE @MaxIndexID INT = (SELECT MAX(IndexId) FROM @t2);

WITH cte AS(
  SELECT  t1.EvrakID,
          ISNULL(t1.DefterNo,'*NULL#') DefterNo,
          ISNULL(t1.Mezarlik,'*NULL#') Mezarlik,
          ISNULL(t1.SayfaNo,'*NULL#') SayfaNo,
          ISNULL(t1.SatirNo, '*NULL#') SatirNo
    FROM @t1 t1
),
cteColsToRows AS(
  SELECT EvrakID, ROW_NUMBER() OVER (PARTITION BY EvrakID ORDER BY (SELECT 0)) rn, NewRow
    FROM
    (SELECT   EvrakID,
              DefterNo,
              Mezarlik,
              SayfaNo,
              SatirNo
       FROM cte) AS nr
       UNPIVOT
       (NewRow FOR OldCols IN (DefterNo, Mezarlik, SayfaNo, SatirNo)
       ) AS NewRows
)
INSERT INTO @t2
SELECT  NULL TipID,
        EvrakID,
        @MaxIndexID + rn AS IndexId,
        CASE WHEN NewRow = '*NULL#' THEN NULL ELSE NewRow END AS Value,
        NULL KayitTarihi
  FROM cteColsToRows

SELECT *
  FROM @t2
  ORDER BY EvrakID, ISNULL(TipID, 999999)

Result:

TipID   EvrakID IndexID Value       KayitTarihi
1       23      66      JohnMc      NULL
NULL    23      67      20-8        NULL
NULL    23      68      ÇAMLICA     NULL
NULL    23      69      1           NULL
NULL    23      70      1           NULL
2       45      32      NewYork     NULL
NULL    45      67      20-8        NULL
NULL    45      68      ÇAMLICA     NULL
NULL    45      69      1           NULL
NULL    45      70      2           NULL
3       46      32      NewYork     NULL
NULL    46      67      20-8        NULL
NULL    46      68      NULL TEST   NULL
NULL    46      69      NULL        NULL
NULL    46      70      NULL        NULL
TipId   EvrakId IndexId Value   KayitTarihi
NULL    1811417 388     41-2    NULL
NULL    1811417 336     NULL    NULL
NULL    1811417 408     9       NULL
NULL    1811417 83      11      NULL
NULL    1811425 388     41-2    NULL
NULL    1811425 336     NULL    NULL
NULL    1811425 408     9       NULL
NULL    1811425 83      12      NULL
NULL    1811446 388     41-2    NULL
NULL    1811446 336     NULL    NULL
NULL    1811446 408     11      NULL
NULL    1811446 83      1       NULL

This is supposed to be the final state of table 2. As you can see some predefined, already known IndexId values are just repeating themselves for each new EvrakId. This example is for 4 columns only of course. There are 20 columns in my first table and therefore 20 different IndexId's but once you give me the correct algorithm(query) I will be able to edit again for all 20 columns like I did before. Also those 2 example columns are not present here since they are not real data.

In this solution I added the table variable @tColIndexID, which holds the given IndexIDs for the (currently) four new values. These IndexIDs are than added to each new row:

DECLARE @t1 TABLE(
  EvrakID NVARCHAR(100),
  DefterNo NVARCHAR(100),
  Mezarlik NVARCHAR(100),
  SayfaNo NVARCHAR(100),
  SatirNo NVARCHAR(100)
)

INSERT INTO @t1 VALUES
('23', '20-8'        ,'ÇAMLICA',            '1'    ,'1')       
,('45', '20-8'        ,'ÇAMLICA',            '1'    ,'2')  
,('46', '20-8'        ,'NULL TEST',            NULL    ,NULL)  


DECLARE @t2 TABLE(
  TipID NVARCHAR(100),
  EvrakID NVARCHAR(100),
  IndexID NVARCHAR(100),
  Value NVARCHAR(100),
  KayitTarihi NVARCHAR(100)
)

INSERT INTO @t2 VALUES
('1'         ,'23'      ,'66'    ,'JohnMc',   NULL)
,('2'         ,'45'      ,'32'    ,'NewYork',  NULL)
,('3'         ,'46'      ,'32'    ,'NewYork',  NULL)

DECLARE @tColIndexID TABLE(
  ColID INT
 ,IndexID INT
)

INSERT INTO @tColIndexID
  VALUES (1, 388)
        ,(2, 336)
        ,(3, 408)
        ,(4, 83);

WITH cte AS(
  SELECT  t1.EvrakID,
          ISNULL(t1.DefterNo,'*NULL#') DefterNo,
          ISNULL(t1.Mezarlik,'*NULL#') Mezarlik,
          ISNULL(t1.SayfaNo,'*NULL#') SayfaNo,
          ISNULL(t1.SatirNo, '*NULL#') SatirNo
    FROM @t1 t1
),
cteColsToRows AS(
  SELECT EvrakID, ROW_NUMBER() OVER (PARTITION BY EvrakID ORDER BY (SELECT 0)) rn, NewRow
    FROM
    (SELECT   EvrakID,
              DefterNo,
              Mezarlik,
              SayfaNo,
              SatirNo
       FROM cte) AS nr
       UNPIVOT
       (NewRow FOR OldCols IN (DefterNo, Mezarlik, SayfaNo, SatirNo)
       ) AS NewRows
)
INSERT INTO @t2
SELECT  NULL TipID,
        EvrakID,
        t3.IndexID AS IndexId,
        CASE WHEN NewRow = '*NULL#' THEN NULL ELSE NewRow END AS Value,
        NULL KayitTarihi
  FROM cteColsToRows
  JOIN @tColIndexID t3 ON t3.ColID = rn

SELECT *
  FROM @t2
  ORDER BY EvrakID, ISNULL(TipID, 999999)

Result:

TipID   EvrakID IndexID Value       KayitTarihi
1       23      66      JohnMc      NULL
NULL    23      388     20-8        NULL
NULL    23      336     ÇAMLICA     NULL
NULL    23      408     1           NULL
NULL    23      83      1           NULL
2       45      32      NewYork     NULL
NULL    45      388     20-8        NULL
NULL    45      336     ÇAMLICA     NULL
NULL    45      408     1           NULL
NULL    45      83      2           NULL
3       46      32      NewYork     NULL
NULL    46      388     20-8        NULL
NULL    46      336     NULL TEST   NULL
NULL    46      408     NULL        NULL
NULL    46      83      NULL        NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM