SQL生成2个已知整数之间的有效值范围

发布于 2024-10-17 15:29:03 字数 356 浏览 3 评论 0原文

我需要编写一个 SQL 查询,该查询将在给定范围的开始和结束的情况下生成有效整数列表。

即给出下表:-

CMPY------MIN_YEAR------MAX_YEAR
PS--------2007----------2014

我想编写一个查询,它将返回所有有效值(针对 CMPY)即:-

CMPY  YEAR
PS    2007
PS    2008
PS    2009
PS    2010
PS    2011
PS    2012
PS    2013
PS    2014

这需要在 Oracle 和 SQL Server 上工作。

I need to write a SQL query that will generate a list of valid integers given the start and end to the range.

ie given the below table :-

CMPY------MIN_YEAR------MAX_YEAR
PS--------2007----------2014

I'd like to write a query which would return all valid values (against CMPY) ie :-

CMPY  YEAR
PS    2007
PS    2008
PS    2009
PS    2010
PS    2011
PS    2012
PS    2013
PS    2014

This needs to work on both Oracle and SQL Server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若言繁花未落 2024-10-24 15:29:03

对于便携式解决方案,您可能需要创建一个简单的数字表,如下所示:

create table integers (val integer);

然后用您可能需要的尽可能多的行填充它。那么你的查询是:

select t.cmpy, i.val
from mytable t
join integers i on i.val between t.min_year and t.max_year;

For a portable solution you may want to create a simple table of numbers like this:

create table integers (val integer);

then populate it with as many rows as you may ever need. Then your query is:

select t.cmpy, i.val
from mytable t
join integers i on i.val between t.min_year and t.max_year;
相思碎 2024-10-24 15:29:03

Oracle无表的解决方案(SQL Server可以使用表方式):

SELECT a.cmpy, (a.min_year - 1) + LEVEL MIN_YEAR 
    FROM  YOUR_TABLE  a
  CONNECT BY (LEVEL-1) <= (MAX_YEAR - MIN_YEAR)                

eg:

SELECT a.cmpy, (a.min_year - 1) + LEVEL MIN_YEAR 
    FROM (
                  SELECT 'PS' CMPY, 2007 MIN_YEAR, 2014 MAX_YEAR
                  FROM DUAL
                ) a
  CONNECT BY (LEVEL-1) <= (MAX_YEAR - MIN_YEAR)    

Solution for Oracle without any table (You can use the table way for SQL Server):

SELECT a.cmpy, (a.min_year - 1) + LEVEL MIN_YEAR 
    FROM  YOUR_TABLE  a
  CONNECT BY (LEVEL-1) <= (MAX_YEAR - MIN_YEAR)                

e.g:

SELECT a.cmpy, (a.min_year - 1) + LEVEL MIN_YEAR 
    FROM (
                  SELECT 'PS' CMPY, 2007 MIN_YEAR, 2014 MAX_YEAR
                  FROM DUAL
                ) a
  CONNECT BY (LEVEL-1) <= (MAX_YEAR - MIN_YEAR)    
涙—继续流 2024-10-24 15:29:03

最有效的方法是拥有一个 Numbers(int num) 表,您可以查询该表来获取一系列数字,例如:

SELECT C.CMPY, N.num
FROM CMPY AS C
JOIN Numbers AS N
  ON N.num BETWEEN C.MIN_YEAR AND C.MAX_YEAR

The most efficient way to do this is to have a Numbers(int num) table that you can query to get a range of numbers, e.g.:

SELECT C.CMPY, N.num
FROM CMPY AS C
JOIN Numbers AS N
  ON N.num BETWEEN C.MIN_YEAR AND C.MAX_YEAR
¢好甜 2024-10-24 15:29:03

查看您的数据,数字是年。您可以在两台服务器中创建一个包含所有年份的表,然后对其进行联接:

select  year.YearNr
from    YourTable yt
join    YearTable year
on      year.YearNr between yt.StartDate and yt.EndDate

这可以在 Oracle 和 SQL Server 上为中间的每一年有效地创建一行。

Looking at your data, the numbers are years. You could create a table in both servers that contains all years, and then join on it:

select  year.YearNr
from    YourTable yt
join    YearTable year
on      year.YearNr between yt.StartDate and yt.EndDate

This effectively creates a row for each year in between, on both Oracle and SQL Server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文