您好,我有一個SQL問題,我希望答案很簡單。Oracle數據庫子查詢導致多行
我有一個具有以下結構的數據庫。
State Gender birthyear birthname count
----- ------ --------- --------- ------
AK F 1923 Helen 15
AK F 1926 Helen 35
AK F 1927 Susan 25
AK F 1920 Helen 15
有成千上萬的記錄,我想輸出看起來像這樣:
birthname 1910 1911 1912 -->2012
----- ------ --------- --------- ------
Helen 5 6 12 800
使用的MS Access我能得到一些結果與此:
SELECT DISTINCT as1.birthname AS Expr1,
(select totalcount from AK as2 where as1.birthname=as2.birthname and as1.gender=as2.gender and as1.state=as2.state and as1.birthyear=as2.birthyear and birthyear=1910) as 1910,
(select totalcount from AK as2 where as1.birthname=as2.birthname and as1.gender=as2.gender and as1.state=as2.state and as1.birthyear=as2.birthyear and birthyear=1911) as 1911,
(select totalcount from AK as2 where as1.birthname=as2.birthname and as1.gender=as2.gender and as1.state=as2.state and as1.birthyear=as2.birthyear and birthyear=2012) as 2012
FROM AK AS as1
谷歌搜索「oracle交叉表查詢」將引導您獲取所需的信息。 –
也查找PIVOT – Randy