0

Have a bit of an issue here and I for some reason can't find a simple way to do this even though I know there has to be. We have the ability to get total sales between dates, but my boss wants to be able to get the total sales per day of the week, so for example, total of all sales on Mondays between 7/1/13 and 8/1/13.

Here is the query for summing the sales between those dates.

SET NOCOUNT ON
SET CONCAT_NULL_YIELDS_NULL OFF

SELECT tblItem.dateOrder,
    tblMerchPack.strMerchPackCategory + tblPayTypeID.strPayTypeName AS strMerchPackCategory,
    tblMerchPack.strMerchPackID,
    tblMerchPack.strMerchPackName + tblPayTypeID.strPayTypeName AS strMerchPackDescription,
    tblMerchItem.strMerchItemID,
    tblMerchItem.strMerchItemName + tblPayTypeID.strPayTypeName AS strMerchItemDescription,
    tblItem.lngQuantity,
    tblItem.curPrice,
    tblItem.curPriceDiscount,
    tblItem.curTaxA,
    tblItem.curTaxB,
    tblPay.curTender - tblPay.curChange AS curPayment,
    tblStoredValue.curCash AS curStoredPayment,
    CASE 
        WHEN tblItem.lngMerchItemID IS NULL
            THEN - 1
        WHEN tblItem.lngMerchItemID > 0
            THEN 0
        WHEN tblPayTypeID.lngPayTypeID IS NULL
            THEN 1000
        WHEN tblPayTypeID.lngPayTypeID > 0
            THEN tblPayTypeID.lngPayTypeID
        ELSE - 1
        END AS 'lngPaymentSort'
FROM tblItem
LEFT JOIN tblPay ON tblItem.lngItemID = tblPay.lngItemID
LEFT JOIN tblStoredValue ON tblItem.lngItemID = tblStoredValue.lngItemID
LEFT JOIN tblPayTypeID ON tblPay.lngPayTypeID = tblPayTypeID.lngPayTypeID
LEFT JOIN tblMerchItem ON tblItem.lngMerchItemID = tblMerchItem.lngMerchItemID
LEFT JOIN tblMerchPack ON tblItem.lngMerchPackID = tblMerchPack.lngMerchPackID
WHERE (
        tblItem.dateOrder BETWEEN '7/1/2013 5:00:00 AM'
            AND '8/1/2013 4:59:59 AM'
        )
ORDER BY lngPaymentSort,
    strMerchPackCategory,
    strMerchItemDescription
4

1 回答 1

0

如果你想 /just/ 得到星期一的结果,那么只需将以下内容添加到你的 where 子句中:

and datepart(dw,tblItem.dateOrder) = 1

如果您希望查询包含每一天的列,则必须在选择部分中使用 case 语句,并为每一天使用 datepart。

这是假设您在美国.. 一周的第一天因国家/地区而异

日期部分信息 - http://msdn.microsoft.com/en-us/library/ms174420.aspx

于 2013-09-23T20:09:21.133 回答