SQL - 每列的公司

时间:2016-07-19 07:24:37

标签: sql sql-server

我在SQL中运行以下代码。我想要做的是为每一列(最小,最大和最新的价格)带来公司现在现在的代码读取公司名称(我不知道它如何决定)

感谢您的帮助

SELECT CLC.DEFINITION_
    ,ITM.CODE
    ,itm.NAME
    ,min(isnull(STL.price, 0)) [min price]
    ,max(isnull(STL.price, 0)) [max]
    ,SUM(total) / SUM(amount) AS [avr]
    --SON SATINALMA FIYATI
    ,ISNULL((
            SELECT TOP 1 PRC.PRICE
            FROM LG_092_01_STLINE AS PRC
            WHERE PRC.STOCKREF = ITM.LOGICALREF
                AND PRC.PRICE > 0
                AND PRC.TRCODE = 1
                AND PRC.CANCELLED = 0
                AND PRC.INVOICEREF > 0
            ORDER BY PRC.DATE_ DESC
            ), 0) AS latestprice
FROM LG_092_ITEMS AS ITM
INNER JOIN LG_092_01_STLINE AS STL ON ITM.LOGICALREF = STL.STOCKREF
INNER JOIN LG_092_CLCARD AS CLC ON CLC.LOGICALREF = STL.CLIENTREF
WHERE STL.PRICE > 0
    AND TRCODE = 1
    AND CANCELLED = 0
    AND INVOICEREF > 0
GROUP BY ITM.LOGICALREF
    ,ITM.CODE
    ,ITM.NAME
    ,STL.STOCKREF
    ,CLC.DEFINITION_
ORDER BY ITM.CODE

我想要的示例数据

+-------------------+----------------------------+-------------+-------------------+-------------+-------------------+-------------+-------------+---------------------------+
|   Material Code   |       Material Name        |  min price  | cheapest company  | max. price  | expensive company |  avr price  | latestprice | latest purchased company  |
+-------------------+----------------------------+-------------+-------------------+-------------+-------------------+-------------+-------------+---------------------------+
| 150.01.001.01.001 | DEMIR NERVURLU A500C 8 MM  |  26,750.00  | METALLOKOMPLEKT-M |  26,750.00  | METALLOKOMPLEKT-M |  26,750.00  |  28,900.00  | METALLOKOMPLEKT-M         |
| 150.01.001.01.001 | DEMIR NERVURLU A500C 8 MM  |  29,600.00  | PERIGEY           |  29,600.00  | PERIGEY           |  29,600.00  |  28,900.00  | PERIGEY                   |
| 150.01.001.01.001 | DEMIR NERVURLU A500C 8 MM  |  28,900.00  | PERIGEY           |  28,900.00  | PERIGEY           |  28,900.00  |  28,900.00  | PERIGEY                   |
| 150.01.001.01.005 | DEMIR NERVURLU A500C 10 MM |  25,900.00  | METALLOKOMPLEKT-M |  27,500.00  | METALLOKOMPLEKT-M |  26,108.89  |  25,900.00  | METALLOKOMPLEKT-M         |
| 150.01.001.01.005 | DEMIR NERVURLU A500C 10 MM |  27,100.00  | METALLOKOMPLEKT-M |  27,100.00  | METALLOKOMPLEKT-M |  27,100.00  |  25,900.00  | METALLOKOMPLEKT-M         |
| 150.01.001.01.010 | DEMIR NERVURLU A500C 12 MM |  25,700.00  | METALLTORG        |  25,700.00  | METALLTORG        |  25,700.00  |  31,900.00  | METALLTORG                |
| 150.01.001.01.010 | DEMIR NERVURLU A500C 12 MM |  23,850.00  | PERIGEY           |  31,900.00  | PERIGEY           |  26,087.20  |  31,900.00  | PERIGEY                   |
| 150.01.001.01.010 | DEMIR NERVURLU A500C 12 MM |  24,100.00  | METALLTORG        |  25,000.00  | METALLTORG        |  24,698.69  |  31,900.00  | METALLTORG                |
| 150.01.001.01.017 | DEMIR NERVURLU A500C 14 MM |  25,400.00  | METALLTORG        |  25,400.00  | METALLTORG        |  25,400.00  |  25,400.00  | METALLTORG                |
+-------------------+----------------------------+-------------+-------------------+-------------+-------------------+-------------+-------------+---------------------------+

0 个答案:

没有答案