SQL Server 2014 - 如何查找2个表之间的最小日期

时间:2016-11-07 12:06:00

标签: sql sql-server-2014

好的人。我只是写这些问题的新手,所以对我来说很容易...... 我想找到两个字段之间的最小日期,我找出了如何为SUM而不是MIN(显然+不适用于min ...) 正如您将从下面看到的那样,第一行(**中的行)不正确(我知道应该没有,但我放弃了)。下一行是完美的,正是我想用MIN代码实现的...... 基本上我有2个表(ScanLine和Inventory)我试图从这两个表中获得Min ProductionDate,在同一行...

**我不想为联接使用别名,我想尽可能避免使用UNION **

SELECT (SELECT min(inventory.productiondate)),
       (SELECT min(scanline.productiondate)) AS OldDate,
       (SELECT sum(Inventory.NetWeight)) + (SELECT sum(scanline.weight)) AS Wgt,
       sum(inventory.units) AS Qty,
       count(inventory.palletbarcode) AS Pallets
FROM   scowms.inventory
       LEFT OUTER JOIN
       scowms.scanline
       ON scowms.inventory.barcode = scowms.scanline.barcode
WHERE  inventory.locationbarcode = '2D03D';

1 个答案:

答案 0 :(得分:0)

您可以使用export PS1="\[\033[38;5;0m\]\[\033[48;5;253m\]\u@\h\[$(tput sgr0)\]\[\033[38;5;15m\]\[\033[48;5;-1m\]:\w\#\[$(tput sgr0)\]" 从两列中获取最小值:

case

表别名是一件好事。它们使查询更容易编写和阅读。