是否可以将这两个查询合二为一?

时间:2013-08-05 01:43:48

标签: sql sql-server

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')

我正在寻找的是包含nextcheckdatelastcheckdate的单行。任何帮助都会很棒。

6 个答案:

答案 0 :(得分: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行,其中某些ID不具有deliveredscheduled值。这是猜测,因为问题中没有足够的信息来了解你真正想要的东西。

答案 1 :(得分:1)

您的WHERE子句是多余的。

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

对于搜索的任何行,如果status ='processed',则状态既不是'已传递',也不是'已安排'。它们是独家的。

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

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' )

答案 2 :(得分: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

答案 3 :(得分:1)

您向我们提供的信息太少,但假设您的查询都是有效的,并且每个查询只返回一行,而目标就是您放置 ... 1行nextcheckdatelastcheckdate ... 您可以使用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 演示

答案 4 :(得分: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')

答案 5 :(得分: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'))