如何仅返回varchar列值不等于某个值的值

时间:2015-06-23 02:38:24

标签: sql-server tsql

我正在尝试计算月度销售额,但不包括来自特定客户的某些销售额。这可能吗?

我已经尝试了下面的内容,它返回时出现GROUP BY错误,但我不想按PickupLocation_ContactName分组,我希望每月的总销售额减少飓风客户订单。

我只读了对数据库的访问权。

SELECT 
    SUM (Price), 
    CONVERT(CHAR(7), PickupDateTime, 120) as Year_Month, 
    PickupLocation_ContactName
FROM 
    dbo.DeliveryRequests
WHERE 
    dbo.DeliveryRequests.PickupLocation_ContactName NOT LIKE 'Hurricanes%'
GROUP BY 
    CONVERT(CHAR(7), PickupDateTime, 120)
ORDER BY 
    Year_Month

3 个答案:

答案 0 :(得分:3)

SELECT部分​​中的任何字段必须是group by中的聚合或字段,而PickupLocation_ContactName不在GROUP BY中。将其添加到GROUP BY,您的查询应该没问题。

SELECT SUM (Price), CONVERT(CHAR(7), PickupDateTime, 120) as Year_Month, PickupLocation_ContactName
    FROM dbo.DeliveryRequests
    WHERE dbo.DeliveryRequests.PickupLocation_ContactName NOT LIKE 'Hurricanes%'
    GROUP BY CONVERT(CHAR(7), PickupDateTime, 120), PickupLocation_ContactName
    ORDER BY Year_Month

答案 1 :(得分:1)

我创建了一个示例,尽管它位于SYBASE ASE 15

请参阅下文

self.edgesForExtendedLayout = UIRectEdgeAll;
self.extendedLayoutIncludesOpaqueBars = YES;

RESULT ::

create table DeliveryRequests 
(
Price int,
PickupLocation_ContactName varchar(10),
PickupDateTime date
)

insert into DeliveryRequests(Price,PickupLocation_ContactName,PickupDateTime)
values (20 , "xxx" , "2015-06-23" )
insert into DeliveryRequests(Price,PickupLocation_ContactName,PickupDateTime)
values (20 , "xxx" , "2015-06-24" )
insert into DeliveryRequests(Price,PickupLocation_ContactName,PickupDateTime)
values (20 , "xxx" , "2015-06-25" )
insert into DeliveryRequests(Price,PickupLocation_ContactName,PickupDateTime)
values (10 , "xxx" , "2015-05-23" )
insert into DeliveryRequests(Price,PickupLocation_ContactName,PickupDateTime)
values (10 , "xxx" , "2015-05-24" )
insert into DeliveryRequests(Price,PickupLocation_ContactName,PickupDateTime)
values (10 , "xxx" , "2015-05-25" )


insert into DeliveryRequests(Price,PickupLocation_ContactName,PickupDateTime)
values (30 , "yyy" , "2015-06-23" )
insert into DeliveryRequests(Price,PickupLocation_ContactName,PickupDateTime)
values (30 , "yyy" , "2015-06-24" )
insert into DeliveryRequests(Price,PickupLocation_ContactName,PickupDateTime)
values (30 , "yyy" , "2015-06-25" )
insert into DeliveryRequests(Price,PickupLocation_ContactName,PickupDateTime)
values (40 , "yyy" , "2015-05-23" )
insert into DeliveryRequests(Price,PickupLocation_ContactName,PickupDateTime)
values (40 , "yyy" , "2015-05-24" )
insert into DeliveryRequests(Price,PickupLocation_ContactName,PickupDateTime)
values (40 , "yyy" , "2015-05-25" )

insert into DeliveryRequests(Price,PickupLocation_ContactName,PickupDateTime)
values (50 , "zzz" , "2015-06-23" )
insert into DeliveryRequests(Price,PickupLocation_ContactName,PickupDateTime)
values (50 , "zzz" , "2015-06-24" )
insert into DeliveryRequests(Price,PickupLocation_ContactName,PickupDateTime)
values (50 , "zzz" , "2015-06-25" )
insert into DeliveryRequests(Price,PickupLocation_ContactName,PickupDateTime)
values (60 , "zzz" , "2015-05-23" )
insert into DeliveryRequests(Price,PickupLocation_ContactName,PickupDateTime)
values (60 , "zzz" , "2015-05-24" )
insert into DeliveryRequests(Price,PickupLocation_ContactName,PickupDateTime)
values (60 , "zzz" , "2015-05-25" )

select sum(Price) sales, PickupLocation_ContactName,month(PickupDateTime) month
from DeliveryRequests where
PickupLocation_ContactName <> "zzz"
group by PickupLocation_ContactName, month(PickupDateTime)

答案 2 :(得分:1)

您可以使用SUM的窗口版本来获取每年/每月的总和,即每月总销售额以及PickupLocation_ContactName

SELECT DISTINCT
    SUM (Price) OVER (PARTITION BY Year_Month) AS SumPerYearMonth, 
    Year_Month,
    PickupLocation_ContactName
FROM 
    dbo.DeliveryRequests
CROSS APPLY (
    SELECT CONVERT(CHAR(7), PickupDateTime, 120)) AS x(Year_Month)
WHERE 
    dbo.DeliveryRequests.PickupLocation_ContactName NOT LIKE 'Hurricanes%'
ORDER BY Year_Month

这会为每个SumPerYearMonth组中的每个PickupLocation_ContactName重复Year_Month个值。

Demo here