替换<聚合SUM T-SQL查询中的1个值

时间:2017-09-20 14:58:09

标签: sql-server-2008 tsql

我尽量避免使用#temp来获得表中的字段只包含少于1个值的平均值。转换为varchar,替换然后转换回来似乎很难看。建议非常欢迎!!

Select
    (select cmp_zip from company where cmp_id = ord_originpoint) as OriginZip,
    ord_originpoint as OriginPoint,
    replace((select cty_nmstct from company where cmp_id = ord_originpoint),'/','') as Origin_City_State,
    (select cmp_zip from company where cmp_id = ord_destpoint) as DestZip,
    ord_destpoint as DestPoint,
    replace((select cty_nmstct from company where cmp_id = ord_destpoint),'/','') as Dest_City_State,
    COUNT(ord_hdrnumber) as OrdCount,
    SUM(ord_rate)/COUNT(ord_hdrnumber) as AvgRate,
    SUM(ord_totalmiles)/COUNT(ord_hdrnumber) as AvgMiles,
    (SUM(ord_rate) / COUNT(ord_hdrnumber)) / (SUM(ord_totalmiles) / COUNT(ord_hdrnumber)) as AvgRevperMiles
from 
    orderheader (NOLOCK) 
where ord_billto <> 'CSXJAC01' 
  and ord_revtype1 = 'NE' 
  and ord_status = 'CMP' 
  and ord_bookdate > GETDATE() - 730 
  and ord_completiondate < GETDATE()
  and ord_totalmiles > 0 
group by 
    ord_originpoint,ord_destpoint 
order by 
    OrdCount desc

如果我在totalmiles列中排除小于零的列,我得到:

+-------+----------+---------------+-------+-------+----------+-----------------+-------+---+------+-----+
|  8850 | DSDMIL   | MILLTOWN      | NJMID |  7206 | NORELI   | ELIZABETH       | NJ    | 1 |  243 |  25 |
|  7047 | CSXNOR   | NORTH BERGEN  | NJ    | 11550 | NASHEM   | HEMPSTEAD       | NYNAS | 1 |  492 |  34 |
|  7047 | CSXNOR   | NORTH BERGEN  | NJ    |  7022 | PERFAI   | FAIRVIEW        | NJBER | 1 |  190 |   1 |
| 17013 | PEPCAR01 | CARLISLE      | PA    | 21224 | CSXBAL   | BALTIMORE       | MD    | 1 |  350 |  97 |
| 23944 | GARKEN   | KENBRIDGE     | VA    | 21224 | CSXBAL   | BALTIMORE       | MD    | 1 |  814 | 230 |
| 21224 | CSXBAL   | BALTIMORE     | MD    | 18202 | HAZHAZ04 | HAZLETON        | PA    | 1 |  621 | 161 |
| 17055 | WOOMEC   | MECHANICSBURG | PACUM | 21224 | CSXBAL   | BALTIMORE       | MD    | 1 |  355 |  85 |
| 23139 | MORPOW01 | POWHATAN      | VA    | 21224 | CSXBAL   | BALTIMORE       | MD    | 1 | 1376 | 186 |
| 17109 | PEPHAR   | HARRISBURG    | PA    | 21224 | CSXBAL   | BALTIMORE       | MD    | 1 |  350 |  78 |
| 21224 | CSXBAL   | BALTIMORE     | MD    | 20066 | WASWAS05 | WASHINGTON      | DC    | 1 |  675 |  54 |
| 21224 | CSXBAL   | BALTIMORE     | MD    | 20743 | GRACAP   | CAPITOL HEIGHTS | MD    | 1 |  300 |  45 |
|  7047 | CSXNOR   | NORTH BERGEN  | NJ    |  7866 | ROCROC05 | ROCKAWAY        | NJ    | 1 |  243 |  34 |
| 21224 | CSXBAL   | BALTIMORE     | MD    | 20772 | BOBUPP   | UPPER MARLBORO  | MD    | 1 |  283 |  37 |
+-------+----------+---------------+-------+-------+----------+-----------------+-------+---+------+-----+

1 个答案:

答案 0 :(得分:0)

我还不完全确定您提出的确切问题,但您在SELECT声明中执行子查询的方式总是很差性能,尝试使用正确的连接,像这样;

SELECT
    c1.cmp_zip AS OriginZip
    ,oh.ord_originpoint AS OriginPoint
    ,replace(c1.cty_nmstct),'/','') AS Origin_City_State
    ,c2.cmp_zip AS DestZip
    ,oh.ord_destpoint AS DestPoint
    ,replace(c2.cty_nmstct,'/','') AS Dest_City_State
    ,COUNT(oh.ord_hdrnumber) AS OrdCount
    ,SUM(oh.ord_rate)/COUNT(oh.ord_hdrnumber) AS AvgRate
    ,SUM(oh.ord_totalmiles)/COUNT(oh.ord_hdrnumber) AS AvgMiles
    ,(SUM(oh.rd_rate)/COUNT(oh.ord_hdrnumber))/( SUM(oh.ord_totalmiles)/COUNT(oh.ord_hdrnumber)) AS AvgRevperMiles
FROM orderheader oh
LEFT JOIN company c1
    ON oh.ord_originpoint = c1.cmp_id
LEFT JOIN company c2
    ON oh.ord_destpoint = c2.cmp_id 
WHERE oh.ord_billto <> 'CSXJAC01' 
    AND oh.ord_revtype1 = 'NE' 
    AND oh.ord_status = 'CMP' 
    AND oh.ord_bookdate > GETDATE() - 730 
    AND oh.ord_completiondate < GETDATE()
    AND oh.ord_totalmiles > 0 
GROUP BY 
    c1.cmp_zip
    ,oh.ord_originpoint
    ,replace(c1.cty_nmstct),'/','')
    ,c2.cmp_zip
    ,oh.ord_destpoint
    ,replace(c2.cty_nmstct,'/','')
ORDER BY OrdCount DESC

您可能需要调整奇数字段(我不确定包含字段rd_rate的表格,例如,这可能是猜测中ord_rate的拼写错误)< / p>