在擴充套件設計中,使用窄表可以很方便的增加新的項。如果用寬表,就會需要修改表結構,很不方便。
而使用寬表在查詢過濾資料的時候會比窄表方便很多,資料的記錄量也會少很多。
這裡介紹一種將窄表轉置成寬表的語法:
select
userkey,
max(case tagid
when 1 then valueid
else null
end) as tag1,
max(case tagid
when 2 then valueid
else null
end) as tag2,
max(case tagid
when 3 then valueid
else null
end) as tag3,
max(case tagid
when 4 then valueid
else null
end) as tag4,
max(case tagid
when 5 then valueid
else null
end) as tag5,
max(case tagid
when 6 then valueid
else null
end) as tag6,
max(case tagid
when 7 then ta**alue
else null
end) as tag7,
max(case tagid
when 8 then ta**alue
else null
end) as tag8,
max(case tagid
when 9 then ta**alue
else null
end) as tag9,
max(case tagid
when 10 then ta**alue
else null
end) as tag10,
max(case tagid
when 11 then ta**alue
else null
end) as tag11,
max(case tagid
when 12 then ta**alue
else null
end) as tag12,
max(case tagid
when 13 then ta**alue
else null
end) as tag13,
max(case tagid
when 14 then ta**alue
else null
end) as tag14,
max(case tagid
when 15 then ta**alue
else null
end) as tag15,
max(case tagid
when 16 then ta**alue
else null
end) as tag16,
max(case tagid
when 17 then ta**alue
else null
end) as tag17,
max(case tagid
when 18 then ta**alue
else null
end) as tag18,
max(case tagid
when 19 then ta**alue
else null
end) as tag19,
max(case tagid
when 20 then ta**alue
else null
end) as tag20,
max(case tagid
when 21 then ta**alue
else null
end) as tag21,
max(case tagid
when 22 then ta**alue
else null
end) as tag22,
max(case tagid
when 23 then ta**alue
else null
end) as tag23,
max(case tagid
when 24 then valueid
else null
end) as tag24,
max(case tagid
when 25 then valueid
else null
end) as tag25,
max(case tagid
when 26 then valueid
else null
end) as tag26,
max(case tagid
when 27 then valueid
else null
end) as tag27,
max(case tagid
when 28 then valueid
else null
end) as tag28,
max(case tagid
when 29 then valueid
else null
end) as tag29,
max(case tagid
when 30 then valueid
else null
end) as tag30,
max(case tagid
when 31 then valueid
else null
end) as tag31,
max(case tagid
when 32 then valueid
else null
end) as tag32
from
userdata
where
tagid in (1 , 2,
3,4,
5,6,
7,8,
9,10,
11,12,
13,14,
15,16,
17,18,
19,20,
21,22,
23,24,
25,26,
27,28,
29,30,
31,32)
group by userkey
寬表和窄表的區別
寬表和窄表的建設該如何選擇?這個問題相信糾結了很多從是資料庫開發 資料倉儲開發和後台開發人員 單單考慮這個問題,難給出乙個絕對的答案 本人從事資料倉儲開發工作到現在已經有一年半時間了,對於這個問題,我也曾經糾結過,但是是否有絕對的答案呢?事實上任何東西都沒有絕對的說法。考慮這樣的乙個問題,乙個公司有...
寬表和窄表的區別
一 寬表 1 寬表 從字面意義上講就是字段比較多的資料庫表。通常是指業務主題相關的指標 維度 屬性關聯在一起的一張資料庫表。由於把不同的內容都放在同一張表儲存,寬表已經不符合三正規化的模型設計規範,隨之帶來的主要壞處就是資料的大量冗餘,與之相對應的好處就是查詢效能的提高與便捷。這種寬表的設計廣泛應用...
將表中資料轉換成Insert語句
object storedprocedure dbo spgeninsertsql script date 02 01 2012 10 48 38 set ansi nulls on goset quoted identifier on goalter procedure dbo spgeninse...