偶尔明智的订单计数
Occasion wise Orders Count
Declare OccasionName Varchar(100)
set @OccasionName=(select distinct (Message) from OrderProducts
where message like '%b''day%' or message like '%bday%' or Message like '%Birth%')
select Count(Distinct od.OrderID) as TotalOrders from orderdetails od
inner join (select Orderid,Message from OrderProducts) op on od.Orderid=op.OrderiD
where od.Orderdate between '01/01/2015' and '01/05/2015'
and op.Message like '%' + @OccasionName + '%'
and (od.TransactionId is not null) AND (od.TransactionId<>'')
这是错误:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
您的查询对于您想要完成的任务来说似乎太复杂了。我认为这符合您的要求:
select Count(Distinct od.OrderID) as TotalOrders
from OrderDetails od inner join
OrderProducts op
on od.Orderid = op.OrderiD
where od.Orderdate between '2015-01-01' and '2015-05-01' and
od.TransactionId <> '' and
(op.message like '%b''day%' or
op.message like '%bday%' or
op.Message like '%Birth%'
);
备注:
- 不需要定义变量。逻辑可以直接进入查询。
- 您原来的问题是多条消息符合条件,因此出现错误。
OrderProducts
上的子查询是多余的。 SQL服务器足够聪明,可以忽略它,但它可能会混淆其他数据库。
- 当对该列进行任何其他比较时,
is not null
条件实际上是多余的。
- 您应该使用 ISO (YYYY-MM-DD) 或 SQL 服务器标准 (YYYYMMDD) 日期格式,而不是区域特定格式。
- 如果 "products" 和 "details" 是
Order
的独立维度,则由于缺乏有关基础数据的任何信息,您 运行 有获得笛卡尔积的风险。 =25=]
Declare OccasionName Varchar(100)
set @OccasionName=(select distinct (Message) from OrderProducts
where message like '%b''day%' or message like '%bday%' or Message like '%Birth%')
select Count(Distinct od.OrderID) as TotalOrders from orderdetails od
inner join (select Orderid,Message from OrderProducts) op on od.Orderid=op.OrderiD
where od.Orderdate between '01/01/2015' and '01/05/2015'
and op.Message like '%' + @OccasionName + '%'
and (od.TransactionId is not null) AND (od.TransactionId<>'')
这是错误:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
您的查询对于您想要完成的任务来说似乎太复杂了。我认为这符合您的要求:
select Count(Distinct od.OrderID) as TotalOrders
from OrderDetails od inner join
OrderProducts op
on od.Orderid = op.OrderiD
where od.Orderdate between '2015-01-01' and '2015-05-01' and
od.TransactionId <> '' and
(op.message like '%b''day%' or
op.message like '%bday%' or
op.Message like '%Birth%'
);
备注:
- 不需要定义变量。逻辑可以直接进入查询。
- 您原来的问题是多条消息符合条件,因此出现错误。
OrderProducts
上的子查询是多余的。 SQL服务器足够聪明,可以忽略它,但它可能会混淆其他数据库。- 当对该列进行任何其他比较时,
is not null
条件实际上是多余的。 - 您应该使用 ISO (YYYY-MM-DD) 或 SQL 服务器标准 (YYYYMMDD) 日期格式,而不是区域特定格式。
- 如果 "products" 和 "details" 是
Order
的独立维度,则由于缺乏有关基础数据的任何信息,您 运行 有获得笛卡尔积的风险。 =25=]