相关子查询结构化数据格式

时间:2014-05-29 23:11:41

标签: sql sql-server correlated-subquery

我从SQL开始,所以要善良,我创建了7个查询脚本,我可以运行来提取数据。我被告知,他们不是要获得7个独立文件,而是将所有数据放在一个报告中以分隔格式组合在一起,这样MAIN EOC打印一行,所有其他查询打印出该EOC记录,然后打印到下一个EOC记录如下:

TYPE RECORD ACCOUNT NUM  
EOC       1| 000001     | 1stuff | 1stuff |More 1stuff |More 1stuff | even more 1stuff
DIAG      2| 000001     | 2stuff | More 2stuff | even more 2stuff
DIAG      2| 000001     | 2stuff | More 2stuff | even more 2stuff
ADMIN     3| 000001     | 3stuff |More 3stuff | even more 3stuff
ADMIN     3| 000001     | 3stuff |More 3stuff | even more 3stuff
PROC      4| 000001     | 4stuff | 4stuff |More 4stuff | even more 4stuff
EVENTS    5| 000001     | 5stuff | 5stuff |More 5stuff | even more 5stuff
EVENTS    5| 000001     | 5stuff | 5stuff |More 5stuff | even more 5stuff
EOC       1| 000002     | 1stuff | 1stuff |More 1stuff |More 1stuff | even more 1stuff
DIAG      2| 000002     | 2stuff | More 2stuff | even more 2stuff
DIAG      2| 000002     | 2stuff | More 2stuff | even more 2stuff
ADMIN     3| 000002     | 3stuff |More 3stuff | even more 3stuff
ADMIN     3| 000002     | 3stuff |More 3stuff | even more 3stuff
PROC      4| 000002     | 4stuff | 4stuff |More 4stuff | even more 4stuff
EVENTS    5| 000002     | 5stuff | 5stuff |More 5stuff | even more 5stuff
EVENTS    5| 000002     | 5stuff | 5stuff |More 5stuff | even more 5stuff

等...........

你明白了。

那么我该如何进行主要的EOC查询,例如:

SELECT
    1Stuff
    ,More1Stuff
    ,EvenMore1Stuff
FROM Somewhere

并在每个EOC行下注入新查询?我想到了它,并意识到我不能使用子查询,因为这只会在EOC行的末尾添加更多数据。任何人对此事都有任何想法。

1 个答案:

答案 0 :(得分:1)

这是你正在寻找的吗?

select * from (
<your query 1>
union all
<your query 2>
union all
<your query 3>
...
union all
<your query 7>
)
order by account num, record -- change the column names here