[英]Formatting results in SQL Server 2012 or Excel 2013
I am having trouble with getting my output from SQL queries to be in the format I want it in. Right now I have a few queries where I do a self join to a table to compare records for accounts that fall within my criteria. 我无法将SQL查询的输出设置为所需的格式。现在我有几个查询,在这些查询中我可以自我连接到表以比较属于我的条件的帐户的记录。 A simplified example would look like this: 一个简化的示例如下所示:
SELECT a.Field1
,a.Field2
,a.Field3
,b.Field1
,b.Field2
,b.Field3
FROM Table1 a
JOIN Table1 b
ON a.Field = b.Field
The output for a and b are in the same rows and I want to try and get them stacked on each other. a和b的输出在同一行中,我想尝试使它们彼此堆叠。 Sometimes there are 1000's of records returned. 有时返回1000条记录。 Is there an easy way to have the b fields stacked right under the corresponding a fields? 有没有一种简单的方法可以将b字段堆叠在对应的a字段下? After some research I am unable to figure out an efficient way of doing this that is not completely manual and time consuming. 经过一番研究,我无法找到一种有效的方法,这种方法并不完全是手动且费时的。
EDIT: 编辑:
My apologies for the lack of information in my first post. 对于我在第一篇文章中缺少信息而深表歉意。 This is a better representation of what one of my queries looks like. 这可以更好地表示我的查询之一。
SELECT DISTINCT a.ID
,a.Field1
,a.Field2
,a.Field3
,a.Field4
,a.Field5
,a.Field6
,b.ID
,b.Field1
,b.Field2
,b.Field3
,b.Field4
,b.Field5
,b.Field6
FROM Table1 a
JOIN Table1 b
ON a.somefield = b.somefield
AND a.anotherfield = b.anotherfield
WHERE a.ID > b.ID
AND a.thisfield <> b.thisfield
AND a.field1 = 'something'
AND b.field1 = 'something'
Given the criteria where some of the fields are not necessarily equal is a union of sorts still the most logical way? 给定某些字段不一定相等的标准,是否仍然是最合乎逻辑的方式?
Is there an easy way to have the b fields stacked right under the corresponding a fields? 有没有一种简单的方法可以将b字段堆叠在对应的a字段下?
SELECT
Field1
,Field2
,Field3
FROM (
SELECT
a.Field1
,a.Field2
,a.Field3
,a.Field
FROM Table1 a
UNION ALL
SELECT
b.Field1
,b.Field2
,b.Field3
,b.Field
FROM Table1 b
) t
ORDER BY Field ASC
EDIT following edit in original question: 编辑以下原始问题后进行编辑:
You do need to use a UNION to get the format you want, but I would start with your current query as a CTE and use a UNION that draws from it, like this: 您确实需要使用UNION来获取所需的格式,但我将从当前查询作为CTE开始,并使用从中查询的UNION,如下所示:
WITH cte AS (
SELECT DISTINCT a.ID AS A_ID
,a.Field1 AS A_Field1
,a.Field2 AS A_Field2
,a.Field3 AS A_Field3
,a.Field4 AS A_Field4
,a.Field5 AS A_Field5
,a.Field6 AS A_Field6
,b.ID AS B_ID
,b.Field1 AS B_Field1
,b.Field2 AS B_Field2
,b.Field3 AS B_Field3
,b.Field4 AS B_Field4
,b.Field5 AS B_Field5
,b.Field6 AS B_Field6
FROM Table1 a
JOIN Table1 b
ON a.somefield = b.somefield
AND a.anotherfield = b.anotherfield
WHERE a.ID > b.ID
AND a.thisfield <> b.thisfield
AND a.field1 = 'something'
AND b.field1 = 'something'
)
SELECT
A_ID
,B_ID
,A_Field1
,A_Field2
,A_Field3
,A_Field4
,A_Field5
,A_Field6
FROM cte
UNION ALL
SELECT
A_ID
,B_ID
,B_Field1
,B_Field2
,B_Field3
,B_Field4
,B_Field5
,B_Field6
FROM cte
ORDER BY A_ID, B_ID
If you absolutely have to stack the ID columns on top of each other in the results, then you'll need to put the UNION above in a derived table like I did in my first example. 如果绝对必须在结果中将ID列彼此堆叠,则需要像在第一个示例中一样将UNION放在派生表中。 But you need both ID's in the base UNION query to get the row-order right. 但是在基本UNION查询中需要两个ID才能获得正确的行顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.