久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

SQL - 聯合兩個表,每個表都有幾個唯一的列

SQL - union two tables, each having a few unique columns(SQL - 聯合兩個表,每個表都有幾個唯一的列)
本文介紹了SQL - 聯合兩個表,每個表都有幾個唯一的列的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

有兩組數據(兩張表)用于患者記錄,一組是 1999-2003,另一組是 2004-2009.每個都有 >100 列;Table_A 有 ~8 個獨特的列,Table_B ~ 25 個獨特的列(相互比較).我的目標是:

Have two sets of data (two tables) for patient records, one 1999-2003, the other 2004-2009. Each has >100 columns; Table_A has ~8 unique columns, Table_B ~ 25 unique columns (Compared to each other). My goal is:

  1. 包含 1999-2009 年所有數據的單個表格
  2. 對于一個表中不在另一個表中的行,只需將該列的值設為 NULL.例如如果表A有Diagnostic_Category_12而Table_B沒有,則該值將是表A中的原始值,而表B中為NULL

我見過一種手動執行此操作的方法:合并具有不同列數的兩個表

I've seen a method for doing this manually: Unioning Two Tables With Different Number Of Columns

但是,此數據集中的列太多,無法逐一輸入 - 我只想自動創建列并根據需要插入 NULL 值.

However there are far too many columns in this data set to type each one in - I'd like to just auto-create columns and insert NULL values as needed.

我使用的是 SQL Server 2008R2.

I am using SQL Server 2008R2.

推薦答案

更聰明地工作,而不是更努力.

Work smarter, not harder.

我建議您通過查詢您的架構來構建一些 SQL...這樣您就不會因為手工編寫而遺漏任何東西.您可以像這樣生成腳本(只需將 @tableName1@tableName2 值替換為適當的表名):

I'd recommend that you build up some SQL by querying your schema... this way you don't miss anything by writing things by hand. You can generate the script like so (just replace @tableName1 and @tableName2 values with the appropriate table names):

declare
 @tableName1 sysname = 'myfirsttablename'
,@tableName2 sysname = 'mysecondtablename'
,@select varchar(max) = 'select';

declare @columns table
(
     Id int identity(1,1)
    ,ColumName nvarchar(128)
    ,ExistsInTable1 bit
    ,ExistsInTable2 bit
);

-- Get a column listing with flags for their existence in each table
insert @columns
select distinct
 quotename(c.Column_Name)
,iif(c2.Table_Name is null, 0, 1)
,iif(c3.Table_Name is null, 0, 1)
from Information_Schema.Columns as c
    left join Information_Schema.Columns as c2
    on c2.Column_Name = c.Column_Name
    and c2.Table_Name = @tableName1
    left join Information_Schema.Columns as c3
    on c3.Column_Name = c.Column_Name
    and c3.Table_Name = @tableName2 
where c.Table_Name in (@tableName1, @tableName2);

-- Build the select statement for the 1sttable (using null where the column is absent)
select
 @select += char(10) + iif(c.Id = 1, ' ', ',') 
+ iif(c.ExistsInTable1 = 1, c.ColumName, 'null') + ' as ' + c.ColumName
from @columns as c
order by c.Id;

set @select += '
from ' + quotename(@tableName1) + '
union all
select';

-- Build the select statement for the 2ndtable (using null where the column is absent)
select
 @select += char(10) + iif(c.Id = 1, ' ', ',') 
+ iif(c.ExistsInTable2 = 1, c.ColumName, 'null') + ' as ' + c.ColumName
from @columns as c
order by c.Id;

set @select += '
from ' + quotename(@tableName2);

-- Print or execute your sql.
print(@select); -- or exec(@select);

生成 SQL 后,我建議您:

Once you've generated your SQL, I'd recommend that you:

  1. 驗證您的結果并根據需要調整您的查詢.
  2. 將最終的 SQL 放在存儲過程中,而不是為每個請求即時生成它.

這篇關于SQL - 聯合兩個表,每個表都有幾個唯一的列的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 免费在线看黄 | 久久久久国产 | 中文字幕欧美一区 | 国产高清久久久 | 中文字幕精品视频在线观看 | 国产成人精品一区二区三区网站观看 | 无码一区二区三区视频 | 美女在线视频一区二区三区 | 久久久久久艹 | 99久久精品免费看国产免费软件 | 国产精品精品视频一区二区三区 | 中文字幕免费视频 | 亚洲性在线 | 99re66在线观看精品热 | 在线视频日韩 | 国产1区2区 | 欧美一区两区 | 在线观看av不卡 | 欧美一区不卡 | 羞羞视频免费观看入口 | 国产中文字幕在线观看 | 亚洲日本成人 | 亚洲人成人一区二区在线观看 | 国产91视频免费 | 99精品网 | 中文字幕av一区二区三区 | 91视频入口 | 国产精品999 | 日韩中文字幕一区二区 | 国产精品亚洲第一区在线暖暖韩国 | 欧美456 | 狠狠草视频 | a在线免费观看视频 | 中文字幕日韩欧美一区二区三区 | 国产91丝袜在线播放 | 亚洲性视频网站 | 99久久影院 | 久久精品伊人 | 在线看片国产 | 99精品99久久久久久宅男 | 538在线精品 |