If else exist statement in select query

huangapple go评论56阅读模式
英文:

If else exist statement in select query

问题

以下是翻译好的部分:

这里有三个表格,我想要一个查询来选择在接下来的7天内将会过期的筛选器/过滤器。

我在Filter表中有2列,分别是filterExp,以及在FilterChange表中的filterExpCustom。如果filterExpCustom为空,则第一个是默认值。

所有的过滤器都有一个以天为单位的过期值,但在某些情况下,一些用户希望为其自己更改过期值。因此,他们可以为特定的客户和过滤器设置filterExpCustom

以下是一些实际值,以便更好理解:

Filter

INSERT [Filter] ([filterId], [filterName], [filterExp]) 
VALUES (1, N'pp', 6)
INSERT [Filter] ([filterId], [filterName], [filterExp]) 
VALUES (2, N'Carbonate', 5)
INSERT [Filter] ([filterId], [filterName], [filterExp]) 
VALUES (3, N'Carbon Block', 5)
INSERT [Filter] ([filterId], [filterName], [filterExp]) 
VALUES (4, N'Carbon Post', 12)
INSERT [Filter] ([filterId], [filterName], [filterExp]) 
VALUES (5, N'Mineral', 12)

FilterChange

INSERT [FilterChange] ([filterChnageId], [customerId], [filterId], [customerDeviceId], [filterChangeDate], [filterExpCustom]) 
VALUES (186, 3, 2, 65, CAST(N'2023-01-31' AS Date), 7)
INSERT [FilterChange] ([filterChnageId], [customerId], [filterId], [customerDeviceId], [filterChangeDate], [filterExpCustom]) 
VALUES (187, 3, 5, 65, CAST(N'2023-01-31' AS Date), NULL)
INSERT [FilterChange] ([filterChnageId], [customerId], [filterId], [customerDeviceId], [filterChangeDate], [filterExpCustom]) 
VALUES (188, 2, 3, 66, CAST(N'2023-02-01' AS Date), 10)
INSERT [FilterChange] ([filterChnageId], [customerId], [filterId], [customerDeviceId], [filterChangeDate], [filterExpCustom]) 
VALUES (189, 2, 3, 66, CAST(N'2023-02-01' AS Date), NULL)

这是我可以从现在到接下来的7天获取过期的过滤器的查询:

CREATE PROCEDURE [dbo].[FilterExpirationGetList]
  @userName BIGINT
AS
  DECLARE @InNextDays int
  BEGIN
    SET @InNextDays = 7

    SELECT f.filterId,f.filterName, f.filterExp,
      fc.customerId, fc.customerDeviceId, fc.filterChangeDate, fc.filterExpCustom,
      c.fName, c.lName, c.cMobile, c.userName
     FROM FilterChange fc INNER JOIN
       Filter f  ON fc.filterId = f.filterId  INNER JOIN
       Customer c ON c.CustomerId = fc.customerId
     WHERE c.userName = @userName AND
      DATEADD(DAY, DATEPART(DAY, GETDATE()) 
      - DATEPART(DAY, DATEADD(DAY,f.filterExp,fc.filterChangeDate)), 
      DATEADD(DAY,f.filterExp,fc.filterChangeDate))
      BETWEEN CONVERT(DATE, GETDATE()) 
      AND CONVERT(DATE, GETDATE() + @InNextDays); 
  END

我的查询的问题是我不知道如何选择过期的过滤器,如果filterExpCustom有一些值,但如果它为空,查询必须计算filterExp

英文:

Here are three tables where I want a query to select which filter/s will be expired in 7 next few days.<br/>
If else exist statement in select query
<br>
I have 2 columns called filterExp in the Filter table and filterExpCustom in the FilterChange table, the first is the default if the second is not empty.
All filters have an expiration value in the day terms but in some cases, some users want to change the expiration value for their own. So they can set filterExpCustom for specific customers and filters.

<br>
Here are some actual values to better understand:<br>

Filter

INSERT [Filter] ([filterId], [filterName], [filterExp]) 
VALUES (1, N&#39;pp&#39;, 6)
INSERT [Filter] ([filterId], [filterName], [filterExp]) 
VALUES (2, N&#39;Carbonate&#39;, 5)
INSERT [Filter] ([filterId], [filterName], [filterExp]) 
VALUES (3, N&#39;Carbon Block&#39;, 5)
INSERT [Filter] ([filterId], [filterName], [filterExp]) 
VALUES (4, N&#39;Carbon Post&#39;, 12)
INSERT [Filter] ([filterId], [filterName], [filterExp]) 
VALUES (5, N&#39;Mineral&#39;, 12)

FilterChange

INSERT [FilterChange] ([filterChnageId], [customerId], [filterId], [customerDeviceId], [filterChangeDate], [filterExpCustom]) 
VALUES (186, 3, 2, 65, CAST(N&#39;2023-01-31&#39; AS Date), 7)
INSERT [FilterChange] ([filterChnageId], [customerId], [filterId], [customerDeviceId], [filterChangeDate], [filterExpCustom]) 
VALUES (187, 3, 5, 65, CAST(N&#39;2023-01-31&#39; AS Date), NULL)
INSERT [FilterChange] ([filterChnageId], [customerId], [filterId], [customerDeviceId], [filterChangeDate], [filterExpCustom]) 
VALUES (188, 2, 3, 66, CAST(N&#39;2023-02-01&#39; AS Date), 10)
INSERT [FilterChange] ([filterChnageId], [customerId], [filterId], [customerDeviceId], [filterChangeDate], [filterExpCustom]) 
VALUES (189, 2, 3, 66, CAST(N&#39;2023-02-01&#39; AS Date), NULL)

Here is the query that I can get the expired filter/s from now until 7 days next:

CREATE PROCEDURE [dbo].[FilterExpirationGetList]
  @userName BIGINT
AS
  DECLARE @InNextDays int
  BEGIN
    SET @InNextDays = 7

    SELECT f.filterId,f.filterName, f.filterExp,
      fc.customerId, fc.customerDeviceId, fc.filterChangeDate, fc.filterExpCustom,
      c.fName, c.lName, c.cMobile, c.userName
     FROM FilterChange fc INNER JOIN
       Filter f  ON fc.filterId = f.filterId  INNER JOIN
       Customer c ON c.CustomerId = fc.customerId
     WHERE c.userName = @userName AND
      DATEADD(DAY, DATEPART(DAY, GETDATE()) 
      - DATEPART(DAY, DATEADD(DAY,f.filterExp,fc.filterChangeDate)), 
      DATEADD(DAY,f.filterExp,fc.filterChangeDate))
      BETWEEN CONVERT(DATE, GETDATE()) 
      AND CONVERT(DATE, GETDATE() + @InNextDays); 
  END

The problem with my query is I don't know how to select expired filters if filterExpCustom has some value but if it doesn't, the filterExp must be calculated in the query.

答案1

得分: 2

以下是翻译好的部分:

"Something like this perhaps, you use ISNULL to get the correct day extension:" 可能是这样,您使用ISNULL来获取正确的天数延长:

"I'm not sure what you do with customers where filters expired before today though. Your design seem lacking a sort of flag that says that filterchange is 'superceeded' with another change." 我不确定对于那些在今天之前过期的过滤器,您要做什么。您的设计似乎缺少一种指示过滤器更改已被另一更改“替代”的标志。

"Alternatively you can fetch only the latest filterchange per filter type where expiration dates are less than 7 days forward. I leave it as an exercise to the reader." 或者,您可以仅检索每种过滤器类型的最新过滤器更改,其中过期日期在7天内。我将其留给读者作为练习。

英文:

Something like this perhaps, you use ISNULL to get the correct day extension:

select *
from customer c
inner join FilterChange fc
 ON fc.customerID = fc.CustomerID
INNER JOIN Filter f
 ON f.filterID = fc.filterID
WHERE DATEADD(DAY, ISNULL(fc.filterExpCustom,filterExp), filterChangeDate) BETWEEN CAST(GETDATE() AS DATE) AND DATEADD(DAY, 7, GETDATE())

I'm not sure what you do with customers where filters expired before today though. Your design seem lacking a sort of flag that says that filterchange is "superceeded" with another change.

Alternatively you can fetch only the latest filterchange per filter type where expiration dates is less than 7 days forward. I leave it as exercise to the reader

huangapple
  • 本文由 发表于 2023年2月6日 04:04:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定