【问题标题】:Group Header in SQL QuerySQL 查询中的组头
【发布时间】:2013-12-18 03:55:12
【问题描述】:

我有一张这样的桌子,

ID    Description     City
----------------------------------
001   free_text       MUL
002   also_free_text  ERL
003   another_text    MUL
004   whatever_text   BER  
005   text            ERL
006   another         BER
007   another_again   MUL

我想知道如何编写查询来获得这样的结果,

City ID     Description
----------------------------------
BER 
     004    whatever_text
     006    another
ERL
     002    also_free_text
     005    text  
MUL
     001    free_text
     003    another_text
     007    another_again

我正在使用带有 SharePoint 列表的 Access 2007。 我需要在 Access 2007 中设计查询,以便我可以在其他地方使用查询(不是直接在 Access 中,换句话说,我不能在 Access 2007 中使用报表功能。

有什么建议吗? 谢谢!

【问题讨论】:

    标签: sql ms-access ms-access-2007


    【解决方案1】:

    想象一个表序列存在,只有一列 SeqNo 存在,有两行 { {1}, {2} }。

    现在我们可以创建查询 QueryInner 为:

    Select
         City   
        ,SeqNo  
        ,ID 
        ,Description 
    
        ,iif(SeqNo=1, City, " ")  as zCity
        ,iif(SeqNo=2,ID, " ") as zID
        ,iif(SeqNo=2,Description, " ") as zDescription
    
    from data, Sequence
    
    order by 
         City
        ,SeqNo
        ,ID
        ,Description
    

    查询Query为:

    SELECT 
        City
        ,SeqNo,
        max(QueryInner.zCity)          as zCity, 
        max(QueryInner.zID)    as zID, 
        max(QueryInner.zDescription) as zDescription
    FROM QueryInner
    group by
        City
        ,SeqNo
        ,zID
    ;
    

    它产生以下结构,可以从中投影所需的列:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      相关资源
      最近更新 更多