sql查询——需要一些建议

发布于 2024-08-30 21:45:02 字数 1947 浏览 4 评论 0原文

我有一个包含循环代码列表的表。CYCLE_DEFINITION。 每个cycle_code 在另一个表(PM1_CYCLE_STATE) 中都有12 个月的条目。 每个月都有一个cycle_start_date 和一个cycle_close_date。 我将检查特定日期(比如说 sysdate)并检查每个周期的当前月份。此外,我还将获得该特定周期的未来 3 个多月的列表。

我写的查询如下:

    SELECT cycd,cm,sd,ed,ld FROM (SELECT pcs.cycle_code CYCD,LTRIM(pcs.cycle_month,'0')+0 CM, pcs.cycle_start_date SD,pcs.cycle_close_date ED,ld.logical_date LD FROM pm1_cycle_state pcs,logical_date ld WHERE 
ld.logical_date BETWEEN pcs.cycle_start_date AND pcs.cycle_close_date and ld.logical_date_type='B')
UNION
SELECT cycd,cm,sd,ed,ld FROM (SELECT pcs.cycle_code CYCD,DECODE(LTRIM(pcs.cycle_month,'0')+1,13,1,14,2,15,3,LTRIM(pcs.cycle_month,'0')+1) CM ,pcs.cycle_start_date SD,pcs.cycle_close_date ED,ld.logical_date LD FROM pm1_cycle_state pcs,logical_date ld WHERE
ld.logical_date BETWEEN pcs.cycle_start_date AND pcs.cycle_close_date and ld.logical_date_type='B')
UNION
SELECT cycd,cm,sd,ed,ld FROM (SELECT pcs.cycle_code CYCD,DECODE(LTRIM(pcs.cycle_month,'0')+2,13,1,14,2,15,3,LTRIM(pcs.cycle_month,'0')+2) CM ,pcs.cycle_start_date SD,pcs.cycle_close_date ED,ld.logical_date LD FROM pm1_cycle_state pcs,logical_date ld WHERE
ld.logical_date BETWEEN pcs.cycle_start_date AND pcs.cycle_close_date and ld.logical_date_type='B')
UNION
SELECT cycd,cm,sd,ed,ld FROM (SELECT pcs.cycle_code CYCD,DECODE(LTRIM(pcs.cycle_month,'0')+3,13,1,14,2,15,3,LTRIM(pcs.cycle_month,'0')+3) CM ,pcs.cycle_start_date SD,pcs.cycle_close_date ED,ld.logical_date LD FROM pm1_cycle_state pcs,logical_date ld WHERE 
ld.logical_date BETWEEN pcs.cycle_start_date AND pcs.cycle_close_date and ld.logical_date_type='B')

该查询运行得很好。 这将导致当前月份和未来 3 个月的所有 Cycle_code 恰好有 4 行。

现在的要求是如果缺少月份中的任何一个。我该如何显示它? 例如:上述查询的输出是

cycd  cm
102 1
102 10
102 11
102 12
103 1
103 10
103 11
103 12
104 1
104 10
104 11
104 12

现在假设表中不存在 cycd=104 和 cm=11 的行,那么上述查询将不会获取行 104 11。 我只想显示那些行。 我怎样才能做到呢?

I have a table with list of cycle codes.CYCLE_DEFINITION.
each and every cycle_code has 12 months entries in another table(PM1_CYCLE_STATE).
Each and every month has a cycle_start_date and a cycle_close_date.
i will check with a particular date(lets say sysdate) and check what is the current month of every cycle.additionally i will also get the list of future 3 more months of that particular cycle.

the query i have written is as below:

    SELECT cycd,cm,sd,ed,ld FROM (SELECT pcs.cycle_code CYCD,LTRIM(pcs.cycle_month,'0')+0 CM, pcs.cycle_start_date SD,pcs.cycle_close_date ED,ld.logical_date LD FROM pm1_cycle_state pcs,logical_date ld WHERE 
ld.logical_date BETWEEN pcs.cycle_start_date AND pcs.cycle_close_date and ld.logical_date_type='B')
UNION
SELECT cycd,cm,sd,ed,ld FROM (SELECT pcs.cycle_code CYCD,DECODE(LTRIM(pcs.cycle_month,'0')+1,13,1,14,2,15,3,LTRIM(pcs.cycle_month,'0')+1) CM ,pcs.cycle_start_date SD,pcs.cycle_close_date ED,ld.logical_date LD FROM pm1_cycle_state pcs,logical_date ld WHERE
ld.logical_date BETWEEN pcs.cycle_start_date AND pcs.cycle_close_date and ld.logical_date_type='B')
UNION
SELECT cycd,cm,sd,ed,ld FROM (SELECT pcs.cycle_code CYCD,DECODE(LTRIM(pcs.cycle_month,'0')+2,13,1,14,2,15,3,LTRIM(pcs.cycle_month,'0')+2) CM ,pcs.cycle_start_date SD,pcs.cycle_close_date ED,ld.logical_date LD FROM pm1_cycle_state pcs,logical_date ld WHERE
ld.logical_date BETWEEN pcs.cycle_start_date AND pcs.cycle_close_date and ld.logical_date_type='B')
UNION
SELECT cycd,cm,sd,ed,ld FROM (SELECT pcs.cycle_code CYCD,DECODE(LTRIM(pcs.cycle_month,'0')+3,13,1,14,2,15,3,LTRIM(pcs.cycle_month,'0')+3) CM ,pcs.cycle_start_date SD,pcs.cycle_close_date ED,ld.logical_date LD FROM pm1_cycle_state pcs,logical_date ld WHERE 
ld.logical_date BETWEEN pcs.cycle_start_date AND pcs.cycle_close_date and ld.logical_date_type='B')

This query is running perfectly fine.
This will result in all the cycle_codes with exactly 4 rows for current month and future 3 months.

Now the requirement is if any of the month is missing.how could i show it?
for eg: the output of the above query is

cycd  cm
102 1
102 10
102 11
102 12
103 1
103 10
103 11
103 12
104 1
104 10
104 11
104 12

Now lets say the row with cycd=104 and cm=11 is not present in the table,then the above query will not get the row 104 11.
I want to display only those rows.
how could i do it?

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

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

发布评论

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

评论(2

美人迟暮 2024-09-06 21:45:02

创建一个包含月份 1 到 12 的表,使用 pm1_cycle_state 交叉连接月份,并使用 select 语句进行左连接(可能作为视图):

SELECT pm1_cycle_state.cycle_code, months.month
FROM pm1_cycle_state
CROSS JOIN months
LEFT OUTER JOIN V_Cycle_Months 
    ON V_Cycle_Months.cycd = pm1_cycle_state.cycle_code
    AND V_Cycle_Months.cm = months.month
WHERE V_Cycle_Months.cycd IS NULL

Create a table containing months 1 to 12, cross join months with pm1_cycle_state, and left join with your select statement (probably as a view):

SELECT pm1_cycle_state.cycle_code, months.month
FROM pm1_cycle_state
CROSS JOIN months
LEFT OUTER JOIN V_Cycle_Months 
    ON V_Cycle_Months.cycd = pm1_cycle_state.cycle_code
    AND V_Cycle_Months.cm = months.month
WHERE V_Cycle_Months.cycd IS NULL
夢归不見 2024-09-06 21:45:02

经过一番思考,我得到了另一种更好的方法。
如果我有一个这样的列表:

102 1
102 10
102 11
102 12
103 1
103 10
103 11
103 12

那么我还可以获取表中现有条目的列表,如下所示(假设 103 11 不存在于表中)。现在我的查询输出如下,

102 1
102 10
102 11
102 12
103 1
103 10
103 12

因为我有在上面的两个列表中,我可以简单地对两个查询进行减法,并根据我提出的问题获得所需的输出。我也做了同样的事情。并且我得到了预期的输出。如果您想查看查询,请发表一些评论!

I got another better approach by lot of thinking.
if i have a list like this :

102 1
102 10
102 11
102 12
103 1
103 10
103 11
103 12

then i can also get the list of existing entries in the table like below(lets say 103 11 is not present in the table ).Now my output of the query would be as below

102 1
102 10
102 11
102 12
103 1
103 10
103 12

since i have the above two lists i can simply do a minus of the two queries and get the required output as per the question i asked.And i did the same.and i got the expected output.Please put some comments if you wanna see the query!

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