简体   繁体   English

在 SELECT 查询 SQL 中生成动态日期列

[英]Generate dynamic date columns in a SELECT query SQL

First of I've got a table like this:首先我有一张这样的表:

vID视频ID bID出价 date日期 type类型 value价值
1 1 100 100 22.01.2021 22.01.2021 o 250.00 250.00
1 1 110 110 25.01.2021 25.01.2021 c C 100.00 100.00
2 2 120 120 13.02.2021 13.02.2021 o 400.00 400.00
3 3 130 130 20.02.2021 20.02.2021 o 475.00 475.00
3 3 140 140 11.03.2022 11.03.2022 c C 75.00 75.00
1 1 150 150 15.03.2022 15.03.2022 o 560.00 560.00

To show which values were ordered(o) and charged(c) per Month, I have to like 'generate' columns for each month both ordered and charged in a MSSQL SELECT query.为了显示每月订购(o)和收费(c)的值,我必须喜欢在 MSSQL SELECT 查询中为每个月订购和收费的“生成”列。 Here is an example table of what I want to get:这是我想要获得的示例表:

vID视频ID JAN2021O JAN2021O JAN2021C JAN2021C FEB2021O FEB2021O FEB2021C FEB2021C MAR2022O MAR2022O MAR2022C MAR2022C
1 1 250.00 250.00 100.00 100.00 560.00 560.00
2 2 400.00 400.00
3 3 475.00 475.00 75.00 75.00

I need a posibility to join it in a SQL SELECT in addition to some other columns I already have.除了我已经拥有的其他一些列之外,我还需要将其加入 SQL SELECT 中。

Does anyone has an idea and could help me please?有没有人有想法,可以帮助我吗?

The SQL language has a very strict requirement to know the number of columns in the results and the type of each column at query compile time, before looking at any data in the tables.在查看表中的任何数据之前,SQL 语言有一个非常严格的要求,即在查询编译时知道结果中的列数每列类型 This applies even to SELECT * and PIVOT queries, where the columns are still determined at query compile time via the table definition (not data) or SQL statement.这甚至适用于SELECT *PIVOT查询,其中列仍然在查询编译时通过表定义(而非数据)或 SQL 语句确定。

Therefore, what you want to do is only possible in a single query if you want to show a specific, known number of months from a base date.因此,如果您想显示从基准日期算起的特定的已知月数,那么您想要执行的操作只能在单个查询中进行。 In that case, you can accomplish this by specifying each column in the SQL and using date math with conditional aggregation to figure the value for each of the months from your starting point.在这种情况下,您可以通过指定 SQL 中的每一列并使用带有条件聚合的日期数学来计算从起点开始的每个月份的值来完成此操作。 The PIVOT keyword can help reduce the code, but you're still specifying every column by hand, and the query will still be far from trivial. PIVOT关键字可以帮助减少代码,但您仍然需要手动指定每一列,并且查询仍然远非微不足道。

If you do not have a specific, known number of months to evaluate, you must do this over several steps:如果您没有具体的、已知的要评估的月数,则必须通过以下几个步骤来完成:

  1. Run a query to find out how many months you have.运行查询以了解您有多少个月。
  2. Use the result from step 1 to dynamically construct a new statement使用步骤 1 的结果动态构造一条新语句
  3. Run the statement constructed in step 2.运行在步骤 2 中构造的语句。

There is no other way.没有其他办法。

Even then, this kind of pivot is usually better handled in the client code or reporting tool (at the presentation level) than via SQL itself.即便如此,与通过 SQL 本身相比,这种类型的数据透视通常在客户端代码或报告工具(在表示级别)中处理得更好。

It's not as likely to come up for this specific query, but you should also be aware there are certain security issues that can be raised from this kind of dynamic SQL, because some of the normal mechanisms to protect against injection issues aren't available (you can't parameterize the names of the source columns, which are dependent on data that might be user-generated) as you build the new query in step 2.这个特定查询不太可能出现,但您还应该意识到,这种动态 SQL 可能会引发某些安全问题,因为一些防止注入问题的正常机制不可用(在步骤 2 中构建新查询时,您无法参数化源列的名称,这些名称取决于可能是用户生成的数据。

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

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