【问题标题】:How I can create a query like that without pivot?我如何在没有数据透视的情况下创建这样的查询?
【发布时间】:2016-01-11 23:26:11
【问题描述】:

真的,我希望有人可以帮助我解决这个问题。我想得到这个例子。我不想使用“枢轴”。我有一些名为“Serie”的项目,这显然需要分组,因为这重复了太多次,这就是我对它进行分组的原因。因此,“系列”字段的每个项目都有一个从 001 到 100 的代码列表,然后我只想获取该列表的最小编号,因此“001”和最大编号,因此每个“100”个月。

请帮帮我, 提前致谢。

例子:

SERIE               JAN     FEB       MAR    APRI   MAY     JUN     JUL
                    D | H  D | H     D | H  D | H   D | H   D | H   D | H

Recibo CA           01  10 02  50   01  10 02  50
Recibo VA-03        04  20 05  80
Recibo UV           08  40 03  10
Recibo VA-02
Recibo VA-04
Recibo VA-01
Recibo WH
Factura 0003

我的数据库中的数据就像这里显示的那样。

 Id     Serie        Month   Serie number
    1   Factura 0003    Jan     4771           
    2   Factura 0003    Jan     4779           
    3   Factura 0003    Jan     4792           
    4   Factura 0003    Febr    4864           
    5   Factura 0003    Febr    4892      

【问题讨论】:

  • 如有疑问请上图。
  • 使用 CASE 表达式。例如SELECT SERIE, CASE WHEN x THEN y END [col1]...
  • 你为什么不想使用pivot呢?
  • 你可以访问 ssrs 吗?
  • 如果您不想要枢轴,您可以使用CASE WHEN 条件来做到这一点。搜索“条件聚合”,例如:stackoverflow.com/questions/16517298/…

标签: sql sql-server sql-server-2008


【解决方案1】:

您可以通过对角矩阵乘法来模拟 Pivot:

With test ([Id], [Serie], [Month], [Serie number])
As (
    Select 1, 'Factura 0003', 'Jan', 4771 Union
    Select 2, 'Factura 0003', 'Jan', 4779 Union
    Select 3, 'Factura 0003', 'Jan', 4792 Union
    Select 4, 'Factura 0003', 'Feb', 4864 Union
    Select 5, 'Factura 0003', 'Feb', 4892
), diagonal_matrics ([Month], [Jan], [Feb], [Mar], [Apr], [May], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec])
As (
    Select 'Jan',    1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL Union
    Select 'Feb', NULL,    1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL Union
    Select 'Mar', NULL, NULL,    1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL Union
    Select 'Apr', NULL, NULL, NULL,    1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL Union
    Select 'May', NULL, NULL, NULL, NULL,    1, NULL, NULL, NULL, NULL, NULL, NULL, NULL Union
    Select 'Jun', NULL, NULL, NULL, NULL, NULL,    1, NULL, NULL, NULL, NULL, NULL, NULL Union
    Select 'Jul', NULL, NULL, NULL, NULL, NULL, NULL,    1, NULL, NULL, NULL, NULL, NULL Union
    Select 'Aug', NULL, NULL, NULL, NULL, NULL, NULL, NULL,    1, NULL, NULL, NULL, NULL Union
    Select 'Sep', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,    1, NULL, NULL, NULL Union
    Select 'Oct', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,    1, NULL, NULL Union
    Select 'Nov', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,    1, NULL Union
    Select 'Dec', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,    1
)
Select [Serie],
    Min([Serie number] * [Jan]) [Jan Min], Max([Serie number] * [Jan]) [Jan Max],
    Min([Serie number] * [Feb]) [Feb Min], Max([Serie number] * [Feb]) [Feb Max],
    Min([Serie number] * [Mar]) [Mar Min], Max([Serie number] * [Mar]) [Mar Max],
    Min([Serie number] * [Apr]) [Apr Min], Max([Serie number] * [Apr]) [Apr Max],
    Min([Serie number] * [May]) [May Min], Max([Serie number] * [May]) [May Max],
    Min([Serie number] * [Jun]) [Jun Min], Max([Serie number] * [Jun]) [Jun Max],
    Min([Serie number] * [Jul]) [Jul Min], Max([Serie number] * [Jul]) [Jul Max],
    Min([Serie number] * [Aug]) [Aug Min], Max([Serie number] * [Aug]) [Aug Max],
    Min([Serie number] * [Sep]) [Sep Min], Max([Serie number] * [Sep]) [Sep Max],
    Min([Serie number] * [Oct]) [Oct Min], Max([Serie number] * [Oct]) [Oct Max],
    Min([Serie number] * [Nov]) [Nov Min], Max([Serie number] * [Nov]) [Nov Max],
    Min([Serie number] * [Dec]) [Dec Min], Max([Serie number] * [Dec]) [Dec Max]
From test
Inner Join diagonal_matrics On test.[Month] = diagonal_matrics.[Month]
Group By [Serie]

【讨论】:

  • 嗨,Y.B.感谢您的回答,但或多或​​少地了解您的逻辑。按照第一行的顺序。这意味着我将在我的数据库中为 emalute "Pivot" 写下所有内容,因为我的表中有数千条记录。当然,直到现在它们都是“Recibos”不同的种类 (8),每个都有一年中的所有月份......
  • @JosephMarchena 这只是将测试数据包含到示例中的一种方式。在您的数据库中使用实际表而不是 test 公用表表达式。
猜你喜欢
  • 1970-01-01
  • 2022-12-03
  • 2021-10-06
  • 2019-08-20
  • 1970-01-01
  • 1970-01-01
  • 2020-08-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多