问题描述
考虑下表:
我有一个表,将 DeviceGUID 与 DetailsGUID 链接起来,其想法是具有无限的详细信息选项(只需创建一个新的详细信息即可获取).但是,这意味着每个 deviceGUID 的记录数量是有限且未知的.
我想向我的用户展示的表格是这样的:
<预><代码>+--------+---------------------------------------------------------------------+|用户名 |DeviceGUID |Detail1 |Detail2 |Detail4 |Detail7 |DateStored ||条目 1 |设备 1 |SN112 |[NULL] |1241 |GUID12 |[最大日期存储] ||条目2 |设备 2 |SN111 |RND123 |2351 |[空] |[最大日期存储] ||条目3 |DEVICE3 |SN100 ||||||[...] |[...] ||||||+------------------------------------------------------------------------------+我一直在搜索并找到了 PIVOT
选项,但它似乎只对一个字段起作用,另一个选项是 CROSS APPLY
,但这(似乎)需要将所有内容转换为相同的数据类型;因为我希望在 ColumnNames 中可见,我将有 3 种类型的数据:String (VARCHAR
) 值、Integer
值、GUID (uniqueidentifier
) 值,并且它们不能互换(意味着具有 GUID Detail1 的 Detail 将始终具有 VARCHAR
,具有 DetailGUID Detail4 的 Detail 将始终是 Integer
到目前为止我能发现什么:
制作用于转置数据的动态 PIVOT.. 但如前所述,这仅限于一列
和
这将使我将所有字段转换为 VARCHAR..
我尝试过的另一个选项(为了理解 PIVOT)是这样的:
('IN
子句中的 GUIDS 是 DetailGUID)这几乎让我得到了我想要的,除了它不是动态的,而且它仍然仅限于一个数据列.(max(sValue)
) 在这种情况下.
====================
回应
应该就是这么简单:
Consider the following table:
I have a table which links a DeviceGUID with a DetailsGUID, with the idea of having unlimited options for Details (just create a new detail and it will be fetchable). however, this means I have a finite and unknown amount of records per deviceGUID.
What I want to show to my users is a table like this:
I have been searching a bit and found the PIVOT
option but that only seems to function for one field,
another option was CROSS APPLY
, but that (seems to) need to convert everything to the same datatype; as I hope is visible n the ColumnNames, I will have 3 types of data: String (VARCHAR
) value, Integer
value, GUID (uniqueidentifier
) value, and they will not be interchangeable (meaning Detail with GUID Detail1 will always have a VARCHAR
, Detail with DetailGUID Detail4 will always be an Integer
what I was able to find out until now:
to make a dynamic PIVOT for transposing the data.. but as mentioned this is limited to one column
and
This will have me converting all fields to VARCHAR..
One other option I tried (to understand PIVOT) was this:
(The GUIDS in the 'IN
clause are the DetailGUIDs)
this almost gets me what I want, except that it is not dynamic and the fact that it is still limited to one data column. (max(sValue)
) in this case.
===================
in response to
It should be as simple as that:
这篇关于TSQL - 将每条记录的行数转换为列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!