选择一个值WHERE是最小值

时间:2016-05-25 12:52:12

标签: sql-server tsql

我正在尝试使用两列的最小值选择LOC_LOC_ID

有什么想法吗?

SELECT DISTINCT 
    LOC_LOC_ID as S, 
    MIN(LWWKS_TOTAL_SALES_VAL) as V, 
    MIN(LWWKS_TOTAL_SALES_QTY) as Q
FROM 
    Apollo.dbo.TBL_FACT_SALES_LWWKS 
GROUP BY  
    LOC_LOC_ID

1 个答案:

答案 0 :(得分:1)

AdminController查询包装在派生表中。然后使用group by表达式返回最小值:

case

或者,取决于OP的要求:

select s, case when v < q then v else q end
from
(
    SELECT LOC_LOC_ID as S, 
           MIN(LWWKS_TOTAL_SALES_VAL) as V, 
           MIN(LWWKS_TOTAL_SALES_QTY) as Q
    FROM 
    Apollo.dbo.TBL_FACT_SALES_LWWKS 
    GROUP BY LOC_LOC_ID
) dt