toksoladuti
IS-IT--Management
I have the following statement, which returns what I need:
SELECT B1.Name AS Branch,
(DATENAME(dw, OH.DateTimeCreated)) as [Day Created],
(DATEPART(hour, OH.DateTimeCreated)) as [Hour Created],
OPT.Name AS HowPlaced,
NVL1.Name AS SaleType,
(OH.TotalSellPrice - OH.TotalCostPrice) AS TotalProfit,
OH.TotalSellPrice, OH.OrderID AS ID1
FROM OrderHeader AS OH WITH(NOLOCK)
LEFT JOIN Branch AS B1 WITH(NOLOCK) ON B1.BranchID = OH.BranchID
LEFT JOIN OrderPlacementType AS OPT WITH(NOLOCK) ON OPT.OrderPlacementTypeID = OH.OrderPlacementTypeID
LEFT JOIN NamedValue AS NVL1 WITH(NOLOCK) ON (NVL1.ValueListName = 'disOrderSaleTypes' AND NVL1.Value = OH.SaleType)
However, I need to GROUP BY the hour created, which is pulled by:
(DATEPART(hour, OH.DateTimeCreated)) as [Hour Created]
I've tried just grouping by this statement but it doesn't work. I'm not too hot on SQL so any pointers would be greatly appreciated.
Many thanks.
SELECT B1.Name AS Branch,
(DATENAME(dw, OH.DateTimeCreated)) as [Day Created],
(DATEPART(hour, OH.DateTimeCreated)) as [Hour Created],
OPT.Name AS HowPlaced,
NVL1.Name AS SaleType,
(OH.TotalSellPrice - OH.TotalCostPrice) AS TotalProfit,
OH.TotalSellPrice, OH.OrderID AS ID1
FROM OrderHeader AS OH WITH(NOLOCK)
LEFT JOIN Branch AS B1 WITH(NOLOCK) ON B1.BranchID = OH.BranchID
LEFT JOIN OrderPlacementType AS OPT WITH(NOLOCK) ON OPT.OrderPlacementTypeID = OH.OrderPlacementTypeID
LEFT JOIN NamedValue AS NVL1 WITH(NOLOCK) ON (NVL1.ValueListName = 'disOrderSaleTypes' AND NVL1.Value = OH.SaleType)
However, I need to GROUP BY the hour created, which is pulled by:
(DATEPART(hour, OH.DateTimeCreated)) as [Hour Created]
I've tried just grouping by this statement but it doesn't work. I'm not too hot on SQL so any pointers would be greatly appreciated.
Many thanks.