亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 5024 | 回復(fù): 2
打印 上一主題 下一主題

Postgres SQL查詢求助 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2010-04-07 22:53 |只看該作者 |倒序瀏覽
本帖最后由 zhuyongxin 于 2010-04-07 22:59 編輯

有一表結(jié)構(gòu)如下:
day                equipment     output
2010/4/1        DAT501           100
2010/4/1        DAT502           120
2010/4/2        DAT501           110
2010/4/2        DAT502           105

查詢結(jié)果:
day                 DAT501     DAT502
2010/4/1        100           120
2010/4/2        110           105

這個查詢怎么實現(xiàn)?

論壇徽章:
3
數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2015-06-18 22:20:00數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2015-06-21 22:20:00數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2015-08-27 06:20:00
2 [報告]
發(fā)表于 2010-04-08 00:05 |只看該作者

使用交叉表函數(shù)解決

本帖最后由 osdba 于 2010-04-08 00:17 編輯

SELECT *
FROM crosstab(
  'select day, equipment, output
   from t
   where equipment = ''DAT501'' or equipment = ''DAT502''
   order by 1,2')
  AS t(day date, DAT501 integer, DAT502 integer);

注意需要安裝contrib下的tablefunc模塊后才會有crosstab函數(shù)。

見我的演示:
root@postgres1 /usr/src/postgresql-8.4.3/contrib/tablefunc]#su - postgres
[postgres@postgres1 ~]$ cd /usr/src/postgresql-8.4.3/contrib/tablefunc
[postgres@postgres1 tablefunc]$ ls
Makefile  data  expected  sql  tablefunc.c  tablefunc.h  tablefunc.so  tablefunc.sql  tablefunc.sql.in  uninstall_tablefunc.sql
[postgres@postgres1 tablefunc]$ psql -f tablefunc.sql
SET
CREATE FUNCTION
CREATE FUNCTION
CREATE TYPE
CREATE TYPE
CREATE TYPE
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
[postgres@postgres1 tablefunc]$ psql
psql (8.4.3)
Type "help" for help.

create table t(day date,equipment varchar(20),output integer);
insert into t values('2010-04-01','DAT501',100);
insert into t values('2010-04-01','DAT502',120);
insert into t values('2010-04-02','DAT501',110);
insert into t values('2010-04-02','DAT502',105);


postgres=# SELECT *
postgres-# FROM crosstab(
postgres(#   'select day, equipment, output
postgres'#    from t
postgres'#    where equipment = ''DAT501'' or equipment = ''DAT502''
postgres'#    order by 1,2')
postgres-#   AS t(day date, DAT501 integer, DAT502 integer);
    day     | dat501 | dat502
------------+--------+--------
2010-04-01 |    100 |    120
2010-04-02 |    110 |    105
(2 rows)

論壇徽章:
0
3 [報告]
發(fā)表于 2010-04-11 20:58 |只看該作者
謝謝了,你太熱心了!
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP