3

I need the results of the past 24 hour, 7 day period. My desired results need to be in this format:

2011-11-05   11 orders  $12354.94
2011-11-04   12 orders  $12555.72
2011-11-03   5 orders  $1000.24

While experimenting with some queries, I keep getting a different amount of rows returned. For example, this query:

SELECT CAST(Left(OrderDate,10) as smalldatetime) as PurchaseDate,
       COUNT(Orders.WebOrderNumber) as "Rows",
       SUM(cast(Orders.Total_Lines_Net AS money)) as Total
FROM Orders 
WHERE OrderDate  BETWEEN getdate() - 7 AND getdate() 
GROUP BY cast(Left(OrderDate,10) as smalldatetime)
ORDER BY cast(Left(OrderDate,10) as smalldatetime)

Returns this Data:

   PurchaseDate Rows    Total
2011-12-01 00:00:00 1539    404798.85
2011-12-02 00:00:00 1572    324963.604
2011-12-03 00:00:00 970 119244.93
2011-12-04 00:00:00 1092    120888.244
2011-12-05 00:00:00 1201    337646.952
2011-12-06 00:00:00 1309    287609.243
2011-12-07 00:00:00 1555    315223.22

I thought that the rows for December 1st seemed low, so I tried a different query:

SELECT   SUM(cast(Orders.Total_Lines_Net AS money)) as Total,
         COUNT(Orders.WebOrderNumber) as "Rows",
         CAST(Left(OrderDate,10) as smalldatetime) as PurchaseDate
FROM Orders 
WHERE OrderDate between '2011-12-01 00:00:00' AND  + '2011-12-01 23:59:59'
GROUP BY cast(Left(OrderDate,10) as smalldatetime)

Which returns 2597 rows. Is the first query not returning the full 24 hour period? The date field in this database is a varchar(255), which contains the date, hour, minute, and seconds. Whats the right way to do this?

2 Answers 2

9
;WITH [cteOrders] AS
(
    SELECT  CONVERT(DATE, [OrderDate]) AS [OrderDate],
            CONVERT(MONEY, [Total_Lines_Net]) AS [Total_Lines_Net]
    FROM [dbo].[Orders]
)
SELECT  [OrderDate],
        COUNT(*) AS [Rows],
        SUM([Total_Lines_Net]) AS [Total]
FROM [cteOrders]
WHERE DATEDIFF(DAY,[OrderDate],GETDATE()) <= 7
GROUP BY [OrderDate]
ORDER BY [OrderDate]
2
2

The date field in this database is a varchar(255), which contains the date, hour, minute, and seconds. Whats the right way to do this?

And here your problem is. THis is ridiculous ;)

Make the OrderDate field a DATE from the start.

Your approach triggers table scans and other bad things.

5
  • This database pre-dates any of the employees at my work. I know that it should have been a datetime field, but I'm stuck with it.
    – broke
    Dec 8, 2011 at 15:38
  • 2
    @broke Why not add a new column to the table and add triggers or whatever other code is appropriate to convert the varchar value to a real datetime value? At least then all your new code and queries can use the correct data type, which will solve this problem and probably others. At least in theory, you can then gradually migrate existing code to use the new column and eventually drop the old column completely.
    – Pondlife
    Dec 8, 2011 at 15:44
  • @Pondlife Making changes like that to the table is a no no. Would I be able to cast the date to a datetime in my query instead?
    – broke
    Dec 8, 2011 at 15:47
  • 1
    @broke, think "computed column". You will endlessly cast/convert your varchar dates every time every query is run, or you can do it once during insert/update time and query and index them properly (and quickly) forever thereafter.
    – KM.
    Dec 8, 2011 at 15:49
  • @Pondlife Could you explain in an answer? I'm still a noob with sql.
    – broke
    Dec 8, 2011 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.