Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

SQL erhält den höchsten Wert und verkettet

select  ProductID
        ,StockOnSite
        ,StockOffsite
        ,StockOnSite - StockOffsite
        ,cast(Stockonsite as varchar(10))+'/' +cast(StockOffsite as varchar(10)) 
from    Products pr1
where   StockOnSite - StockOffsite = (Select max(StockOnSite - StockOffsite)
                                        from Products pr2
                                        where   pr1.ProductID = pr2.ProductID
                                        )
group by ProductID, StockOnSite, StockOffsite