select distinct (Member_Mailid), OrderLog_City, Member_MailId,
OrderLog_MovieName, OrderLog_MovieLanguage
from tbl_OrderLog
where
OrderLog_Initiated between
'2013-02-01 00:00:01.010' and '2013-02-01 23:59:59.990'
and OrderLog_MovieLanguage in
('TAMIL','ENGLISH','HINDI','MALAYALAM')
group by Member_Mailid
having
count(Member_Mailid)>3
order by member_mailid asc
当我运行上述查询时,我遇到了一些错误。
我的结果中需要以下列,这就是我之前在select
语句中指定的原因:
(OrderLog_City,
Member_MailId,
OrderLog_MovieName,
OrderLog_MovieLanguage)
列出了观看次数超过3次和5次的客户。
但它会引发错误;
Msg 8120,Level 16,State 1,Line 1 Column 'tbl_OrderLog.OrderLog_City'在选择列表中无效,因为它 不包含在聚合函数或GROUP BY中 子句。
消息209,级别16,状态1,行9不明确的列名称 “member_mailid“
答案 0 :(得分:0)
您不能混用DISTINCT
和GROUP BY
。
由于您使用的是COUNT
,因此需要与GROUP BY
进行汇总。
您需要在GROUP BY
子句中包含所有非聚合列。
这样的事情应该成功运行:
select Member_Mailid
, OrderLog_City
, OrderLog_MovieName
, OrderLog_MovieLanguage
from tbl_OrderLog
where OrderLog_Initiated between '2013-02-01 00:00:01.010' and '2013-02-01 23:59:59.990'
and OrderLog_MovieLanguage in ('TAMIL','ENGLISH','HINDI','MALAYALAM')
group by Member_Mailid
, OrderLog_City
, OrderLog_MovieName
, OrderLog_MovieLanguage
having COUNT(Member_Mailid) > 3
order by member_mailid asc
在评论后修改
如果您想根据上述查询获得不同的Member_Mailid
值,只需转换为子查询并针对它运行DISTINCT
:
select distinct(Member_Mailid)
from
(
select Member_Mailid
from tbl_OrderLog
where OrderLog_Initiated between '2013-02-01 00:00:01.010' and '2013-02-01 23:59:59.990'
and OrderLog_MovieLanguage in ('TAMIL','ENGLISH','HINDI','MALAYALAM')
group by Member_Mailid
, OrderLog_City
, OrderLog_MovieName
, OrderLog_MovieLanguage
having COUNT(Member_Mailid) > 3
) m