在select语句中嵌入一个案例(SQL)

时间:2014-09-23 21:54:18

标签: sql sql-server

我已经尝试了多种方式,并且无法获得正确的语法,以便最大化结果。 (抱歉,这是在SQL 2000服务器上)

我升级除了应用程序可能会破坏它的运行。 所以,我试图根据(如果一个案例被关闭)拉出最大数量的结果。但是,我不想要"只有"在尝试

时,内心情况
SELECT DISTINCT CASE.CASENUM      AS Case#, 
                CASE.Lastname     AS [Client Name], 
                event.actiondate  AS [Last Action in Event], 
                event.description AS [Last Event], 
                CASE.statcode     AS [Case Status], 
                transmdf.amount, 
                transmdf.postdate [Last ActionDate], 
                transmdf.description AS [Last Fin. Action] 
FROM            Transmdf 
INNER JOIN 
                ( 
                         SELECT   CASENUM, 
                                  MAX(postdate) AS latest 
                         FROM     Transmdf 
                         GROUP BY CASENUM) YYY 
ON              transmdf.CASENUM = YYY.CASENUM 
AND             transmdf.postdate = YYY.latest 
INNER JOIN      event 
ON              transmdf.casenum = event.casenum 
INNER JOIN 
                ( 
                         SELECT   CASENUM, 
                                  MAX(actiondate) AS latest 
                         FROM     EVENT 
                         GROUP BY CASENUM) XXX 
ON              EVENT.CASENUM = XXX.CASENUM 
AND             EVENT.ACTIONDATE = XXX.latest 
INNER JOIN 
                CASE 
                                ON              transmdf.casenum = CASE.casenum 
                                WHERE           CASE.statcode = 'c' 
                                ORDER BY        CASE.lastname

然而,我遇到的情况是从我的返回结果中减少了5000例。

理想情况下,我尝试做的是将所有结果提取到已关闭"过了某个约会。

有一个空洞......这些案件已经归还

  1. 显示事件表中的最后(最近)操作(日期和说明)(针对查询的每个客户端)

  2. 以及他们上次(最近)的财务交易(金额/日期/说明)(针对查询的每个客户)

  3. 帮助??

2 个答案:

答案 0 :(得分:0)

CasedescriptionEvent都是关键词,因此当这些词被用作对象名称时,您需要使用方括号[]比一个关键词,sql server中对象名称的错误选择。

;With [event] AS
 (
   SELECT actiondate , [description] , casenum 
         , ROW_NUMBER() OVER (PARTITION BY casenum ORDER BY actiondate DESC) rn 
   FROM [event]
 ),
transmdf AS
 (
  SELECT amount, postdate , [description], CASENUM
       , ROW_NUMBER() OVER (PARTITION BY casenum ORDER BY postdate DESC) rn 
  FROM Transmdf
 )
SELECT [Case].CASENUM           AS [Case] 
      ,[Case].Lastname          AS [Client Name] 
      ,[event].actiondate       AS [Last Action in Event] 
      ,[event].[description]    AS [Last Event]
      ,[Case].statcode          AS [Case Status] 
      ,transmdf.amount          AS [Amount]
      ,transmdf.postdate        AS [Last ActionDate] 
      ,transmdf.[description]   AS [Last Fin. Action]
FROM [Case] 
INNER JOIN transmdf ON transmdf.casenum = [Case].casenum  AND transmdf.rn = 1
INNER JOIN [event]  ON transmdf.casenum = [event].casenum AND [event].rn  = 1
WHERE  [Case].statcode = 'c'
ORDER BY [Case].lastname

答案 1 :(得分:0)

case是一个保留字,因此对于表名来说它是一个糟糕的选择。我认为SQL Server可以更容易解析以下内容。

SELECT distinct  case.CASENUM AS Case#, c.Lastname AS [Client Name], 
       e.actiondate as [Last Action in Event], e.description as [Last Event], 
       c.statcode as [Case Status], t.amount, t.postdate as [Last ActionDate],
       t.description as [Last Fin. Action]
FROM Transmdf t INNER JOIN
     (SELECT CASENUM, MAX(postdate) AS latest
      FROM Transmdf
      GROUP BY CASENUM
     ) YYY
     ON t.CASENUM = YYY.CASENUM AND transmdf.postdate = YYY.latest Inner join
     event e
     on t.casenum = e.casenum INNER JOIN
     (SELECT CASENUM, MAX(actiondate) AS latest
      FROM EVENT
      GROUP BY CASENUM
     ) XXX
     ON e.CASENUM = XXX.CASENUM AND e.ACTIONDATE = XXX.latest inner join
     [case] c
     on t.casenum = c.casenum
where c.statcode = 'c'
ORDER BY c.lastname;