1
Select convert(varchar(8), max(checkdate),1) lastcheckdate 
from table 
where status = 'processed' 
and not status in ('delivered', 'scheduled')

Select convert(varchar(8), max(checkdate),1) as nextcheckdate 
from table 
where status = 'scheduled'
and not status in ('delivered', 'processed')

我正在寻找的是 1 个具有nextcheckdate和的单行lastcheckdate。任何帮助都会很棒。

4

6 回答 6

3

形式上,我认为以下返回两个值。

select convert(varchar(8), max(case when status = 'processed' then checkdate end), 1
              ) as lastcheckdate,
       convert(varchar(8), max(case when status = 'scheduled' then checkdate end), 1
              ) as nextcheckdate
from table;

但是,您的查询是毫无意义的。每种情况下的where子句都检查状态是否具有值(例如'processed')。当这是真的时,这not部分总是正确的。

这使我怀疑涉及到一些聚合,您正在寻找processed具有一些没有deliveredorscheduled值的 id 的行。这是推测,因为问题中没有足够的信息来了解您真正在寻找什么。

于 2013-08-05T01:54:36.040 回答
1

您的 WHERE 子句是多余的。

Select convert(varchar(8), max(checkdate),1) lastcheckdate 
from table 
where status = 'processed' 
and not status in ('delivered', 'scheduled')

对于搜索到的任何行,如果 status = 'processed',则 status 既不是 'delivered' 也不是 'scheduled'。它们是排他性的。

因此,开始通过减少代码来寻找您的解决方案。

Select convert(varchar(8), max(checkdate),1) lastcheckdate 
from table 
where status = 'processed'

Select convert(varchar(8), max(checkdate),1) as nextcheckdate 
from table 
where status = 'scheduled'

现在,查看选择列。请注意,函数逻辑是相同的,只是您对列进行了不同的别名。

让他们保持一致。

Select convert(varchar(8), max(checkdate),1) as next_or_last_check_date 
from table 
where status = 'processed'

Select convert(varchar(8), max(checkdate),1) as next_or_last_check_date 
from table 
where status = 'scheduled'

现在唯一的区别是两个状态检查。我们可以将它们组合在一起以进行单个查询或使用 IN(相同的区别)。

Select convert(varchar(8), max(checkdate),1) as next_or_last_check_date 
from table 
where status = 'processed'
   or status = 'scheduled'

使用相同的东西:

Select convert(varchar(8), max(checkdate),1) as next_or_last_check_date 
from table 
where status IN ( 'processed', 'scheduled' )
于 2013-08-05T01:56:03.220 回答
1
select
  case when status = 'processed' then convert(varchar(8),checkdate) else null end as lastcheckddate
 ,case when status = 'scheduled' then convert(varchar(8),checkdate) else null end as nextcheckddate
from
  table
where (status = 'processed' and status not in ('delivered', 'scheduled'))
   or (status = 'scheduled' and status not in ('delivered', 'processed'))
于 2013-08-05T02:18:10.223 回答
1

尝试这个。

SELECT 
    CASE WHEN status = 'processed' 
            THEN convert(varchar(8), max(checkdate),1) 
    END as lastcheckdate, 

    CASE  WHEN status = 'scheduled' 
            THEN convert(varchar(8), max(checkdate),1) 
    END as nextcheckdate

    FROM table
     WHERE status in ('processed', 'scheduled') GROUP BY status

这将返回两行。

对 2 行不满意?然后这样做。

    DECLARE @table1 TABLE (lastcheckdate int,nextcheckdate int)
    DECLARE @table2 TABLE (lastcheckdate int,nextcheckdate int)

    DECLARE @lastcheckdate int
    DECLARE @nextcheckdate Int

    INSERT INTO @table1(lastcheckdate ,nextcheckdate)
            SELECT 
            CASE WHEN status = 'processed' 
                    THEN convert(varchar(8), max(checkdate),1) 
            END as lastcheckdate, 
            CASE  WHEN status = 'scheduled' 
                    THEN convert(varchar(8), max(checkdate),1) 
            END as nextcheckdate
            FROM table
            WHERE status in ('processed', 'processed') GROUP BY status

    DECLARE test_cur CURSOR
        FOR SELECT lastcheckdate,nextcheckdate FROM @table1

    OPEN test_cur
    FETCH NEXT FROM test_cur INTO @lastcheckdate,@nextcheckdate
    WHILE @@FETCH_STATUS = 0
    BEGIN
        IF @lastcheckdate IS NOT NULL
        BEGIN
            INSERT INTO @Table2 (lastcheckdate) VALUES(@lastcheckdate)
        END
        ELSE
        BEGIN
            UPDATE @Table2 SET nextcheckdate = @nextcheckdate
        END

        FETCH NEXT FROM test_cur INTO @lastcheckdate,@nextcheckdate
    END
    SELECT * FROM @Table2
于 2013-08-05T02:00:23.760 回答
1

您给我们提供的信息太少,但假设您的两个查询都可以正常工作,并且每个查询都只返回一行,而目标就是您所说的... 1 单行具有nextcheckdate并且lastcheckdate...您可以使用它CROSS JOIN来生成该行

SELECT a.lastcheckdate, b.nextcheckdate
  FROM
( 
  SELECT CONVERT(VARCHAR(8), MAX(checkdate), 1) lastcheckdate 
    FROM table 
   WHERE status = 'processed' 
     AND NOT STATUS IN ('delivered', 'scheduled')
) a CROSS JOIN 
(
  SELECT CONVERT(VARCHAR(8), MAX(checkdate), 1) nextcheckdate 
    FROM table 
   WHERE status = 'scheduled'
     AND NOT STATUS IN ('delivered', 'processed')
) b 

这是SQLFiddle演示

于 2013-08-05T02:00:28.937 回答
1

尝试这个

select x.customer_id,x.customer_name,
(Select  max(checkdate)
from table 
where status = 'processed' and customer_id = x.customer_id) Lastcheckdate,
(Select max(checkdate) from table 
where status = 'scheduled' and customer_id = x.customer_id) nextcheckdate 
from table x where and not x.status in ('delivered', 'scheduled')
于 2013-08-05T02:12:46.770 回答