目前在SQL Server中使用多个左连接 - 是否有更简单的方法?

时间:2015-10-01 13:20:25

标签: sql-server left-join

我有一个产生正确结果的SQL语句,但该语句很长并且可能形成不良。有没有人有任何关于缩短声明的建议?

SELECT 
    COUNT(b.priceRRP) AS tomi, 
    COUNT(c.priceRRP) AS mac, 
    COUNT(d.priceRRP) AS well, 
    COUNT(bt.priceTrade) AS tomiTrade, 
    COUNT(ct.priceTrade) AS macTrade, 
    COUNT(dt.priceTrade) AS wellTrade
FROM 
    Items AS a
RIGHT JOIN 
    PriceT AS pt ON pt.internalid = a.internal_id
LEFT JOIN 
    Pricewell AS d ON d.idn = (SELECT TOP(1) idn FROM Pricewell 
                               WHERE pt.idn = item 
                                 AND CASE WHEN a.taxcode = 1 THEN a.retailPrice 
                                          ELSE a.retailPrice * 1.2 END > priceRRP 
                                 AND priceRRP <> 0 
                                 AND syncDate = '2015-10-01' 
                               ORDER BY syncDate DESC, syncTime DESC)
LEFT JOIN 
    Pricetomi AS b ON b.idn = (SELECT TOP(1) idn FROM Pricetomi 
                               WHERE pt.idn = item 
                                 AND CASE WHEN a.taxcode = 1 THEN a.retailPrice 
                                          ELSE a.retailPrice * 1.2 END > priceRRP 
                                 AND priceRRP <> 0 
                                 AND syncDate = '2015-10-01' 
                               ORDER BY syncDate DESC, syncTime DESC)
LEFT JOIN 
    Pricemac AS c ON c.idn = (SELECT TOP(1) idn FROM Pricemac 
                              WHERE pt.idn = item 
                                AND CASE WHEN a.taxcode = 1 THEN a.retailPrice 
                                         ELSE a.retailPrice * 1.2 END > priceRRP 
                                AND priceRRP <> 0 
                                AND syncDate = '2015-10-01' 
                              ORDER BY syncDate DESC, syncTime DESC)
LEFT JOIN 
    Pricewell AS dt ON dt.idn = (SELECT TOP(1) idn FROM Pricewell 
                                 WHERE pt.idn = item 
                                   AND CASE WHEN a.taxcode = 1 THEN a.tradePrice 
                                            ELSE a.tradePrice * 1.2 END > priceTrade 
                                   AND priceTrade <> 0 
                                   AND syncDate = '2015-10-01' 
                                 ORDER BY syncDate DESC, syncTime DESC)
LEFT JOIN 
    Pricetomi AS bt ON bt.idn = (SELECT TOP(1) idn FROM Pricetomi 
                                 WHERE pt.idn = item 
                                   AND CASE WHEN a.taxcode = 1 THEN a.tradePrice 
                                            ELSE a.tradePrice * 1.2 END > priceTrade 
                                   AND priceTrade <> 0 
                                   AND syncDate = '2015-10-01' 
                                 ORDER BY syncDate DESC, syncTime DESC)
LEFT JOIN 
    Pricemac AS ct ON ct.idn = (SELECT TOP(1) idn FROM Pricemac 
                                WHERE pt.idn = item 
                                  AND CASE WHEN a.taxcode = 1 THEN a.tradePrice 
                                           ELSE a.tradePrice * 1.2 END > priceTrade 
                                  AND priceTrade <> 0 
                                  AND syncDate = '2015-10-01' 
                                ORDER BY syncDate DESC, syncTime DESC)
WHERE 
    a.inactive = 0
    AND a.discontinued = 0

由于 克里斯

1 个答案:

答案 0 :(得分:0)

我认为你不能缩短这么多。请注意,您可以使用<select name="country" ng-options="item.country for item in countries"> <option value="">Pick a country</option> </select> <input name="countryCode" type="text" placeholder="Country Code"> <input name="currency" type="text" placeholder="Currency">

.controller('exampleCtrl', function {
  $scope.countries = [
    { country: "Sweden" },
    { country: "USA" },
    { country: "Canada" }
  ];

  $scope.currencies = [
    { currency: "SEK" },
    { currency: "USD" },
    { currency: "CAD" }
  ];

  $scope.countryCodes = [
    { countryCode: "SE" },
    { countryCode: "US" },
    { countryCode: "CA" }
  ];

});

许多人发现在使用outer apply代替SELECT ... FROM PriceT AS pt LEFT JOIN Items AS a ON pt.internalid = a.internal_id OUTER APPLY (SELECT TOP(1) priceRRP FROM Pricewell ...) d OUTER APPLY (SELECT TOP(1) priceRRP FROM Pricetomi ...) b ... WHERE ... 时更好理解。