SQL Server 2008 查询结果格式(更改 x 和 y 轴字段)

发布于 2025-01-08 11:53:14 字数 618 浏览 0 评论 0原文

格式化查询结果的最有效方法是什么,是在实际的 SQL Server SQL 代码中还是使用不同的程序(例如 access 或 excel),以便可以将 X(第一行列标题)和 Y 轴(第一列字段值)已更改,但仍以不同的方式表示相同的查询结果数据。

它们将数据存储在我的数据库中的方式以及在 SQL Server 2008 中返回原始查询结果的方式如下:

原始结果格式

下面是我需要查看数据的方式:

我需要如何显示结果

本质上,我需要将邮政编码字段沿着 Y轴(第一列)和覆盖代码字段穿过顶部第一行(X 轴),曝光填充其余数据。

我能完成此任务的唯一方法是将数据导入 Excel 并进行一系列 V-LookUps。我尝试使用一些数据透视表,但没有走得太远。我将继续尝试使用 V-LookUps 格式化数据,但希望有人能想出更好的方法来做到这一点。

What is the most efficient way to format query results, wether in the actual SQL Server SQL code or using a different program such as access or excel so that the X (first row column headers) and Y Axis (first column field values) can be changed, but with the same query result data still being represented, just in a different way.

they way the data is stored in my database and they way my original query results are returned in SQL Server 2008 are as follows:

Original Results Format

And Below is the way I need to have the data look:

How I need the Results to Look

In essence, I need to have the zipcode field go down the Y Axis (first column) and the Coveragecode field to go across the top first Row (X Axis) with the exposures filling in the rest of the data.

The only way I can thing of getting this done is by bringing the data into excel and doing a bunch of V-LookUps. I tried using some pivot tables but didn't get to far. I'm going to continue trying to format the data using the V-LookUps but hopefully someone can come up with a better way of doing this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油饼 2025-01-15 11:53:14

您正在寻找的是一个名为 PIVOT 的表运算符。文档: http://msdn.microsoft.com/en-us/library/ms177410 .aspx

WITH Src AS(
    SELECT Coveragecode, Zipcode, EarnedExposers
    FROM yourTable
)
SELECT Zipcode, [BI], [PD], [PIP], [UMBI], [COMP], [COLL]
FROM Src
PIVOT(MAX(EarnedExposers) FOR CoverageCode 
    IN(
           [BI], 
           [PD], 
           [PIP], 
           [UMBI], 
           [COMP], 
           [COLL]
      )
) AS P;

What you are looking for is a table operator called PIVOT. Docs: http://msdn.microsoft.com/en-us/library/ms177410.aspx

WITH Src AS(
    SELECT Coveragecode, Zipcode, EarnedExposers
    FROM yourTable
)
SELECT Zipcode, [BI], [PD], [PIP], [UMBI], [COMP], [COLL]
FROM Src
PIVOT(MAX(EarnedExposers) FOR CoverageCode 
    IN(
           [BI], 
           [PD], 
           [PIP], 
           [UMBI], 
           [COMP], 
           [COLL]
      )
) AS P;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文