如何按名称对查询进行分组? P_Name

时间:2016-11-11 10:10:29

标签: sql asp.net

select  ID_Sale,SaleDate,Branch_Name,P_Name,P_UnitPrice,QuantitySold,
    sum (QuantitySold*P_UnitPrice) over (order by ID_Sale asc) AS RunningTotal
from tblP_Sales
INNER JOIN tblProduct_With_Img
    ON tblP_Sales.P_ID_Sales = tblProduct_With_Img.P_ID
INNER JOIN tblBranches
    ON tblP_Sales.BranchID = tblBranches.BranchID
--this group is not working ? how to work this ?
  group by P_Name

这是没有GROUP BY子句的结果:

Sale_ID Sale_Date   Branch   Item Name                 Pric/unit   qty  RUNTotal

1056    2016-11-10  Ajman   Afghani Pulaw With Meat       26        1   26
1057    2016-11-10  Ajman   Sada Rice With Chicken Boti   24        2   74
1058    2016-11-11  Ajman   Afghani Pulaw With Meat       26        1   100

1 个答案:

答案 0 :(得分:0)

我认为你想要添加“PARTITION BY”

select  ID_Sale,SaleDate,Branch_Name,P_Name,P_UnitPrice,QuantitySold,
    sum (QuantitySold*P_UnitPrice) over (PARTITION BY P_Name order by ID_Sale asc) AS RunningTotal
from tblP_Sales
INNER JOIN tblProduct_With_Img
    ON tblP_Sales.P_ID_Sales = tblProduct_With_Img.P_ID
INNER JOIN tblBranches
    ON tblP_Sales.BranchID = tblBranches.BranchID