简体   繁体   English

同时选择字段的最小日期和最大日期

[英]Select both min Date and max date for the field at the same time

I have this query which returns the min date value respective to the field. 我有此查询它返回分钟日期值相应的字段。

SELECT sa.ContentID, SA.MSISDN, sa.DateRegistered AS minDate, sa.DateRegistered AS maxDate 
FROM sms.dbo.SubscriptionsArchive AS sa 
INNER JOIN (SELECT MSISDN, MIN(DateRegistered) AS mindate 
            FROM sms.dbo.SubscriptionsArchive 
            GROUP BY MSISDN) mysa 
ON sa.MSISDN = mysa.MSISDN AND sa.DateRegistered = mysa.mindate 

It's output is 它的输出是

在此处输入图片说明

But I want to display the max date value for the same date as well in addition to min date. 但是除了最小日期外,我还要显示同一日期的最大日期值。 How do i achieve that? 我该如何实现?

Although it may be tempting to use a second subquery join to get the MAX() , and again join it by MSISDN an the max date, you should be able to achieve it by adding the MAX(DateRegistered) into the existing subquery and adding an OR condition in the join's ON clause to join either on the MSISDN,mindate or on MSISDN,maxdate . 尽管可能很想使用第二MAX(DateRegistered)查询MAX(DateRegistered)来获取MAX() ,然后通过MSISDN和最大日期再次MAX(DateRegistered)它,但是您应该能够通过将MAX(DateRegistered)添加到现有子查询中并添加一个来实现它。 MSISDN,mindateON子句中的OR条件在MSISDN,mindateMSISDN,maxdateMSISDN,maxdate

SELECT
  sa.ContentID,
  SA.MSISDN,
  -- Use the join's dates by alias in the SELECT
  mysa.mindate,
  mysa.maxdate
FROM
  sms.dbo.SubscriptionsArchive AS sa 
  INNER JOIN (
    SELECT
      MSISDN,
      MIN(DateRegistered) AS mindate,
      -- Add the max date
      MAX(DateRegistered) AS maxdate,
    FROM sms.dbo.SubscriptionsArchive 
    GROUP BY MSISDN
  ) mysa 
    -- Join on either a mindate or maxdate match
    ON (sa.MSISDN = mysa.MSISDN AND sa.DateRegistered = mysa.mindate)
      OR (sa.MSISDN = mysa.MSISDN AND sa.DateRegistered = mysa.maxdate)

I notice that you tagged this as but the dbo really suggests you are using MS SQL Server. 我注意到您将其标记为dbo确实建议您使用的是MS SQL Server。 If that is true, then you can use a common table expression rather than the subquery join. 如果是这样,则可以使用公用表表达式而不是子查询联接。

-- Define the subquery as a CTE instead
WITH mysa AS (
 SELECT
    MSISDN,
    MIN(DateRegistered) AS mindate,
    -- Add the max date
    MAX(DateRegistered) AS maxdate,
  FROM sms.dbo.SubscriptionsArchive 
  GROUP BY MSISDN
);

SELECT
  sa.ContentID,
  SA.MSISDN,
  -- Use the join's dates by alias in the SELECT
  mysa.mindate,
  mysa.maxdate
FROM sms.dbo.SubscriptionsArchive AS sa
-- Join on the CTE
INNER JOIN mysa
  ON (sa.MSISDN = mysa.MSISDN AND sa.DateRegistered = mysa.mindate)
  OR (sa.MSISDN = mysa.MSISDN AND sa.DateRegistered = mysa.maxdate)

Your syntax suggests SQL Server. 您的语法建议使用SQL Server。 If so, I would suggest window functions: 如果是这样,我建议使用窗口函数:

SELECT sa.ContentID, SA.MSISDN, sa.DateRegistered AS minDate, sa.DateRegistered AS maxDate 
FROM (SELECT sa.*,
             MIN(DateRegistered) OVER (PARITION BY sa.MSISDN) as mindr,
             MAX(DateRegistered) OVER (PARITION BY sa.MSISDN) as maxdr
      FROM sms.dbo.SubscriptionsArchive sa 
     ) sa
WHERE sa.DateRegistered IN (mindr, maxdr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM