sql - 如何制作我们想要在 PostgreSQL 中查看的 txt/doc/pdf/html 文件

标签 sql postgresql postgresql-9.2

我有表格和数据。

我想要的是,生成所有学生的报告姓名排序..

--student table

create table student(
sid int,
sname text
);


--student details

insert into student(sid,sname)
values(101,'John'),
(102,'barbie'),
(103,'britney'),
(104,'jackson'),
(105,'abraham')
;

--questions table all the questions for the test

create table questions(
questionid serial,
question text
);

--i have the questions in my table 

insert into questions(question)
values('How much is 1+1'),('What is the value of PI'),('Whose dimensions are all equal');

--the test table it contains the details of the test attebdee by every student..

create table test(
sno serial,
sid int,
questionid int,
answer text,
marks int  
);

--insert into test table the answers and the marks ..should be updated here..

insert into test(sid,questionid,answer,marks)
values(101,1,'2',10),
(102,2,' 3 ',0),
(103,3,' ring ',0),
(104,1,' 1 ',0),
(105,1,' 1 ',0),
(101,2,'3.7',0),
(101,3,' square',10);

我的要求:

生成的 txt/doc/pdf/html 文件应位于如下 View 中

enter image description here

live sql fiddle demo i tried

最佳答案

可能是这样的:

copy(
    with cte as (
        select
            s.sid, s.sname,
            q.question, t.answer, t.marks,
            row_number() over(partition by s.sid order by t.sno) as row_num
        from student as s
            left outer join test as t on t.sid = s.sid
            left outer join questions as q on q.questionid = t.questionid
    )
    select
        case when c.row_num = 1 then c.sid else null end as sid,
        case when c.row_num = 1 then c.sname else null end as sname,
        c.question, c.answer, c.marks
        from cte as c
        order by c.sname asc, c.row_num asc
) to 'e:\sample.csv' delimiter ',' csv header;

sql fiddle demo

关于sql - 如何制作我们想要在 PostgreSQL 中查看的 txt/doc/pdf/html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18507970/

相关文章:

postgresql - 在大型数据集上删除 Postgres 中的列

sql - 如何查找给定列是否存在唯一键约束

sql - 在嵌套循环连接中哪个表被认为是 'inner'

php - 我的 Sql 查询是正确的,但它没有影响任何行。?

postgresql - slick 3 auto-generated - default value (timestamp) 列,如何定义 Rep[Date] 函数

Postgresql 是在本地运行并在 Unix 域上接受连接的服务器

sql - Postgres慢查询(慢索引扫描)

sql - PostgreSQL 中的 "null value in column violates not-null constraint"错误

sql - 根据长度在oracle sql中拆分字符串

javascript - sequelize v4 复制的密码验证失败