具有来自同一表的多个选择的MYSQL查询

时间:2013-11-29 22:14:04

标签: mysql

收到错误

操作数应包含1列

PK是ID

该表只是将数据转储到表

需要获取最早的日期数量和最新日期数量并显示在同一列

任何帮助表示赞赏

SELECT ebx_r_history.ItemNumber,
       (SELECT r.QuantitySold as newqty, r.lastupdate as lu
          FROM ebx_r_history r
         WHERE ebx_r_history.ItemNumber = r.ItemNumber AND ebx_r_history.SKU = r.SKU
         ORDER BY r.LastUpdate ASC
         LIMIT 1),
       (SELECT r.QuantitySold as newqty, r.lastupdate as lu
          FROM ebx_r_history r
         WHERE ebx_r_history.ItemNumber = r.ItemNumber AND ebx_r_history.SKU = r.SKU
         ORDER BY r.LastUpdate DESC
         LIMIT 1)
FROM
ebx_r_history
GROUP BY ebx_r_history.ItemNumber,
         ebx_r_history.SKU
ORDER BY ebx_r_history.LastUpdate 

3 个答案:

答案 0 :(得分:1)

尝试这样的事情:

SELECT r1.ItemNumber,
  (
    SELECT r.QuantitySold
    FROM ebx_r_history r
    WHERE r1.ItemNumber = r.ItemNumber
      AND r1.SKU = r.SKU
    ORDER BY r.LastUpdate ASC LIMIT 1
    ) AS earliestDateQty,
  (
    SELECT r.QuantitySold
    FROM ebx_r_history r
    WHERE r1.ItemNumber = r.ItemNumber
      AND r1.SKU = r.SKU
    ORDER BY r.LastUpdate DESC LIMIT 1
    ) AS latestDateQty
FROM ebx_r_history r1
GROUP BY r1.ItemNumber,r1.SKU
ORDER BY 3

你有几个错误。你在内部选择中得到两列,并且你有几个地方可能会因为模糊的列名而得到错误。

答案 1 :(得分:1)

sqlFiddle here

SELECT T1.ItemNumber,
       T1.SKU,
       T1.Old_QuantitySold,
       T1.Old_LastUpdate,
       T2.New_QuantitySold,
       T2.New_LastUpdate
FROM
   (SELECT itemNumber,SKU,QuantitySold as Old_QuantitySold,LastUpdate as Old_LastUpdate
      FROM ebx_r_history r
     WHERE NOT EXISTS (SELECT 1 FROM ebx_r_history e
                        WHERE e.itemNumber = r.itemNumber AND e.SKU = r.SKU
                          AND e.LastUpdate < r.LastUpdate)
    )T1 
  LEFT JOIN        
   (SELECT itemNumber,SKU,QuantitySold as New_QuantitySold,LastUpdate as New_LastUpdate
      FROM ebx_r_history r
     WHERE NOT EXISTS (SELECT 1 FROM ebx_r_history e
                        WHERE e.itemNumber = r.itemNumber AND e.SKU = r.SKU
                          AND e.LastUpdate > r.LastUpdate)
    )T2 ON (T2.itemNumber = T1.itemNumber AND T2.SKU = T1.SKU)
WHERE T1.Old_LastUpdate >= CURDATE() - INTERVAL 14 DAY
  AND T2.New_LastUpdate >= CURDATE() - INTERVAL 14 DAY
ORDER BY T2.New_LastUpdate;

您可以left joininner join取决于您,因为T1始终会获得最早的记录,T2将始终获得ItemNumber,SKU分组的最新记录。

超过14天的无聊数据

SELECT T1.ItemNumber,
       T1.SKU,
       T1.Old_QuantitySold,
       T1.Old_LastUpdate,
       T2.New_QuantitySold,
       T2.New_LastUpdate
FROM
   (SELECT itemNumber,SKU,QuantitySold as Old_QuantitySold,LastUpdate as Old_LastUpdate
      FROM ebx_r_history r
     WHERE LastUpdate >= CURDATE() - INTERVAL 14 DAY
       AND NOT EXISTS (SELECT 1 FROM ebx_r_history e
                        WHERE e.itemNumber = r.itemNumber AND e.SKU = r.SKU
                          AND e.LastUpdate >= CURDATE() - INTERVAL 14 DAY
                          AND e.LastUpdate < r.LastUpdate)
    )T1 
  LEFT JOIN        
   (SELECT itemNumber,SKU,QuantitySold as New_QuantitySold,LastUpdate as New_LastUpdate
      FROM ebx_r_history r
     WHERE LastUpdate >= CURDATE() - INTERVAL 14 DAY
       AND NOT EXISTS (SELECT 1 FROM ebx_r_history e
                        WHERE e.itemNumber = r.itemNumber AND e.SKU = r.SKU
                          AND e.LastUpdate >= CURDATE() - INTERVAL 14 DAY
                          AND e.LastUpdate > r.LastUpdate)
    )T2 ON (T2.itemNumber = T1.itemNumber AND T2.SKU = T1.SKU)
ORDER BY T2.New_LastUpdate;

忽略超过14天的数据sqlFiddle here

如果您想使用确切时间(14天前),可以将CURDATE()的出现替换为NOW()

答案 2 :(得分:1)

此版本可能为您提供简化且更快捷的替代方案。 “AllItems”的内部查询在每个项目编号/ sku的基础上执行最后一次更新的最小值和最大值,但我相信它们将是同一记录。

现在,按结果/ sku将结果返回到历史数据,并将结果与​​最小或最大日期匹配。如果是一个真实的日期/时间,那么无论如何只会是一个,而不仅仅是一个日期。因此,由于将有2个可能的记录(一个用于最小值,一个用于最大值),我为每个分别应用最小和最大日期应用MAX(IIF())并且必须保留group by子句。< / p>

注意,如果您正在处理仅限日期的条目,或者完全相同的项目/ sku和lastupdate的可能性与第二项相同,那么您将需要一种更接近每个上升/下降的限制1的方法。 / p>

SELECT
      AllItems.ItemNumber,
      AllItems.SKU,
      AllItems.MinUpdate,
      MAX( IIF( rh.lastupdate = AllItems.MinUpdate, rh.Quantity.Sold, 0 )) as QtyAtMinDate,
      AllItems.MaxUpdate,
      MAX( IIF( rh.lastupdate = AllItems.MaxUpdate, rh.Quantity.Sold, 0 )) as QtyAtMaxDate
   from 
      ( SELECT 
              r.ItemNumber,
              r.SKU,
              MIN( r.lastupdate ) as MinUpdate,
              MAX( r.lastupdate ) as MaxUpdate
          FROM 
             ebx_r_history r
          group by 
             r.ItemNumber,
             r.SKU ) AllItems
      JOIN ebx_r_history rh
         ON AllItems.ItemNumber = rh.ItemNumber
         AND AllItems.SKU = rh.SKU
         AND ( rh.lastUpdate = AllItems.MinUpdate
            OR rh.lastUpdate = AllItems.MaxUpdate )

分组       AllItems.ItemNumber,       AllItems.SKU

根据您在最近14天内只查看IGNORE项目的另一个答案,您可以通过

向内部查询添加WHERE子句
 WHERE r.LastUpdate >= CURDATE() - INTERVAL 14 DAY

如果您的历史记录表有一个自动递增ID列,并且各个事务的lastUpdate按顺序标记,例如当它们被添加而未被任何其他操作修改时,那么您可以只应用类似但MIN / MAX的ID列,然后在ID上加入TWICE,并且每行ONCE,例如......

SELECT
      AllItems.ItemNumber,
      AllItems.SKU,
      rhMin.LastUpdate as MinUpdate,
      rhMin.QuantitySold as MinSold,
      rhMax.LastUpdate as MaxUpdate,
      rhMax.QuantitySold as MaxSold
   from 
      ( SELECT 
              r.ItemNumber,
              r.SKU,
              MIN( r.AutoIncrementColumn ) as MinAutoID,
              MAX( r.AutoIncrementColumn ) as MaxAutoID
          FROM 
             ebx_r_history r
          group by 
             r.ItemNumber,
             r.SKU ) AllItems
      JOIN ebx_r_history rhMin
         ON AllItems.MinAutoID = rhMin.AutoIncrementColumn
      JOIN ebx_r_history rhMax
         ON AllItems.MaxAutoID = rhMax.AutoIncrementColumn
   order by
      rhMax.LastUpdated