简体   繁体   English

SQL Server:查询多行,返回一个

[英]SQL Server : Querying Multiple Rows, Returning In One

Problem 问题
We are currently working with some data which is stored as in the screenshot below. 我们目前正在处理一些数据,这些数据存储在下面的屏幕截图中。 We are using this to display information in a C# application. 我们使用它来在C#应用程序中显示信息。 The only way we can get the application to work as needed is to have the information in one single row. 我们可以根据需要使应用程序工作的唯一方法是将信息放在一行中。

We have tried some methods on Display multiple rows and column values into a single row, multiple column values to no avail. 我们尝试了一些方法将多行和列值显示为单行,多列值无效。

Desired output 期望的输出
We require each row with the same date and vehicleID to be on the same row to be output from our stored procedure. 我们要求具有相同日期和vehicleID每一行都在同一行上,以便从我们的存储过程输出。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Cheers. 干杯。

当前数据集

if I understood correctly, you don't know how many unique date/vehicle_id combinations there are in the data. 如果我理解正确,你不知道数据中有多少个唯一的日期/ vehicle_id组合。 hence you can't just do a pivot over a set list of values. 因此,您不能只对一组值列表进行转动。

in this case, you would need to build up a list of the date/vehicle ids dynamically, and then pivot over that list. 在这种情况下,您需要动态建立日期/车辆ID列表,然后在该列表上进行转动。

please check out the following examples involving dynamic SQL and pivot tables: 请查看以下涉及动态SQL和数据透视表的示例:

PIVOT in sql 2005 在SQL 2005中的PIVOT

SQL Server : dynamic pivot over 5 columns SQL Server:超过5列的动态数据透视表

Pivot Dynamic Columns, no Aggregation 透视动态列,无聚合

https://www.simple-talk.com/blogs/2007/09/14/pivots-with-dynamic-columns-in-sql-server-2005/ https://www.simple-talk.com/blogs/2007/09/14/pivots-with-dynamic-columns-in-sql-server-2005/

SQL Server dynamic PIVOT query? SQL Server动态PIVOT查询?

Dynamic Pivot Columns in SQL Server SQL Server中的动态数据透视表列

SQL Pivot Query with Dynamic Columns 带有动态列的SQL数据透视查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM