SQL Descending命令LEFT JOIN子查询问题

时间:2015-12-16 09:33:23

标签: sql sql-server join

我有以下查询。

    void MouseWheel(object sender, MouseEventArgs e)
    {
        if (_panel.VerticalScroll.Value > _panel.VerticalScroll.Maximum - _panel.VerticalScroll.LargeChange)
            MessageBox.Show("Bottom");
    }

问题是表C可以包含基于de PID join的多个记录。由于表C上有多条记录,因此会生成相同的记录。 表C需要作为最新记录加入,因此只有C中的一个。这样,表A记录将不会重复。

我尝试过TOP 1并按顺序排序,但不能在子查询中使用。

SELECT r1.*, 
       r2.vlag54, 
       r2.vlag55 
FROM   [rxmon].[dbo].[a] AS r1 
       LEFT JOIN [rxmon].[dbo].[b] AS r2 
              ON r2.artikelnummer = r1.drug_id 
       LEFT JOIN (SELECT * 
                  FROM   [rxmon].[dbo].[c]) AS r3 
              ON r3.pid = r1.patient_id 
WHERE  r3.obx_id = 20937 
       AND Cast(r3.obx_datetime AS DATE) = Cast(Getdate() - 1 AS DATE) 
       AND r1.patient_id = 7092425 
       AND obx_value < CASE 
                         WHEN r2.vlag54 = 1 THEN 30 
                         WHEN r2.vlag55 = 1 THEN 50 
                       END 
       AND r2.vlag54 = CASE 
                         WHEN r3.obx_value < 30 THEN 1 
                         ELSE 0 
                       END 
       AND r2.vlag55 = CASE 
                         WHEN r3.obx_value BETWEEN 30 AND 50 THEN 1 
                         ELSE 0 
                       END 
ORDER  BY obx_datetime DESC;  

2 个答案:

答案 0 :(得分:1)

这将按OBX_DATETIME排序,只取第一个:

...
LEFT JOIN (
    SELECT pid, obx_id, obx_datetime, obx_value
        , n = ROW_NUMBER() over(PARTITION BY pid ORDER BY obx_datetime desc)
    FROM   [rxmon].[dbo].[c]
) AS r3 
    ON r3.pid = r1.patient_id and r3.n = 1
...

如果以增量方式插入OBX_DATETIME(仅限较新的日期),则可以按ID排序。

您在r3上使用LEFT JOIN,但在WHERE子句中也使用等号运算符的r3:

WHERE  r3.obx_id = 20937 
    AND Cast(r3.obx_datetime AS DATE) = Cast(Getdate() - 1 AS DATE) 

它将从r3上的左连接中删除NULL值。也许您还应该将其移动到子查询或使用INNER JOIN。

您还应该避免在查询中使用数据库名称,除非此查询是从同一服务器上的另一个数据库运行的。这没关系:

SELECT ... FROM [dbo].[a] AS r1 ...

使用SELECT *也是一个坏习惯。您应该只列出代码将使用的列。

答案 1 :(得分:1)

试试这个.... @Shift

SELECT r1.*, 
       r2.vlag54, 
       r2.vlag55 
FROM   [dbo].[a] AS r1 
       LEFT JOIN [dbo].[b] AS r2 
              ON r2.artikelnummer = r1.drug_id 
       LEFT JOIN (
           SELECT
               ROW_NUMBER() OVER (PARTITION BY pid ORDER BY id DESC) RN,
               c.*
           FROM C
       ) r3 
              ON r3.pid = r1.patient_id  AND r3.RN = 1
WHERE  r3.obx_id = 20937 
       AND Cast(r3.obx_datetime AS DATE) = Cast(Getdate() - 1 AS DATE) 
       AND r1.patient_id = 7092425 
       AND obx_value < CASE 
                         WHEN r2.vlag54 = 1 THEN 30 
                         WHEN r2.vlag55 = 1 THEN 50 
                       END 
       AND r2.vlag54 = CASE 
                         WHEN r3.obx_value < 30 THEN 1 
                         ELSE 0 
                       END 
       AND r2.vlag55 = CASE 
                         WHEN r3.obx_value BETWEEN 30 AND 50 THEN 1 
                         ELSE 0 
                       END 
ORDER  BY obx_datetime DESC;