SQL查询字符串列表

时间:2014-04-28 20:25:43

标签: sql oracle database-design

我正在尝试查询上次从SQL表导入文件" import",给定一个月字符串整数(Jan是' 01',Feb是' ; 02',March是' 03' ..)。我已经粘贴了我的解决方案,但我想知道是否有更优雅的方式。

SELECT DISTINCT 
months.month_string, MAX(import.process_date)
FROM import import,
(
    select '01' month_string from dual union
    select '02' month_string from dual union
    select '03' month_string from dual union
    select '04' month_string from dual union
    select '05' month_string from dual union
    select '06' month_string from dual union
    select '07' month_string from dual union
    select '08' month_string from dual union
    select '09' month_string from dual union
    select '10' month_string from dual union
    select '11' month_string from dual union
    select '12' month_string from dual 
) months
WHERE import.process_month (+) = months.month_string
GROUP BY months.month_string
ORDER BY months.month_string;

4 个答案:

答案 0 :(得分:1)

我不知道你是否会发现这更优雅",但这是一个更好的方式来编写查询:

SELECT months.month_string, MAX(import.process_date)
FROM (select '01' as month_string from dual union all
      select '02' as month_string from dual union all
      select '03' as month_string from dual union all
      select '04' as month_string from dual union all
      select '05' as month_string from dual union all
      select '06' as month_string from dual union all
      select '07' as month_string from dual union all
      select '08' as month_string from dual union all
      select '09' as month_string from dual union all
      select '10' as month_string from dual union all
      select '11' as month_string from dual union all
      select '12' as month_string from dual 
     ) months LEFT OUTER JOIN
     import
     on import.process_month = months.month_string
GROUP BY months.month_string
ORDER BY months.month_string;

以下是更改:

  • 使用显式的ANSI标准外连接替换外连接的(无法解释的)Oracle语法。
  • 撤消表格的顺序,使用left outer join而不是right outer join
  • select distinct更改为selectselect distinct几乎不需要group by
  • union更改为union allunion花费精力去除重复项,这是不需要的。
  • 为列别名添加了as。这使得名称更明显地被分配给列,并有助于防止漫游逗号搞乱查询。

您还可以使用connect by或递归CTE来实际生成月份数字,但我不确定该版本是否与此版本一样清晰。

编辑:

我假设您需要获取NULL值,因为并非所有月份都会出现在import中。这就是你使用months表的原因。如果没有,请执行:

SELECT i.process_month, MAX(i.process_date)
FROM import i
GROUP BY i.process_month
ORDER BY i.process_month;

如果您担心范围,

SELECT i.process_month, MAX(i.process_date)
FROM import i
WHERE i.process_month in ('01', '02', '03', '04', '05', '06', '07', '08', '09', '10',
                          '11', '12'
                         )
GROUP BY i.process_month
ORDER BY i.process_month;

答案 1 :(得分:1)

怎么样

WITH SUMMARY_DATA AS
  (SELECT CASE
            WHEN PROCESS_MONTH IN ('01', '02', '03', '04', '05', '06',
                                   '07', '08', '09', '10', '11', '12')
              THEN PROCESS_MONTH
            ELSE
               NULL
          END AS SUMMARY_MONTH,
          PROCESS_DATE
     FROM IMPORT)
SELECT SUMMARY_MONTH, MAX(PROCESS_DATE)
  FROM SUMMARY_DATA
  GROUP BY SUMMARY_MONTH
  ORDER BY SUMMARY_MONTH

SQLFiddle here

分享并享受。

答案 2 :(得分:1)

您可以将all_object用于计数,此查询中不需要distinct

select months.month_string, MAX(import.process_date)
from import import, 
     (select lpad(to_char(rownum), 2, 0) month_string from all_objects where rownum <= 12) months
where  import.process_month (+) = months.month_string
group by months.month_string
order by months.month_string

答案 3 :(得分:0)

由于process_month没有任何其他值,您可以在没有隐含Join到月份表的情况下完成此操作。

也许以下内容可行?

SELECT  process_month, MAX(process_date)
FROM    import
GROUP BY process_month
ORDER BY process_month;