我试图创建一个查询,这样如果为一组订单订购了特定商品,我就可以让列显示Y / N.我要找的项目是OLI.id =' 538'。
所以我的结果是:
=IIF(DateInterval.Weekday = 1, 'Monday = Monday Last Week
DateAdd(DateInterval.Day, 2-WeekDay(Today), DateAdd(DateInterval.Day, -7, Today)),
IIF(DateInterval.Weekday = 2, 'Tuesday = Monday Last Week
DateAdd(DateInterval.Day, 2-WeekDay(Today), DateAdd(DateInterval.Day, -7, Today)),
IIF(DateInterval.Weekday = 3, 'Wednesday = Monday Last Week
DateAdd(DateInterval.Day, 2-WeekDay(Today), DateAdd(DateInterval.Day, -7, Today)),
IIF(DateInterval.Weekday = 4, 'Thursday = Monday Last Week
DateAdd(DateInterval.Day, 2-WeekDay(Today), DateAdd(DateInterval.Day, -7, Today)),
IIF(DateInterval.Weekday = 5, 'Friday = Monday This Week
DATEADD("d", 1 - DATEPART(DateInterval.WeekDay, Today,FirstDayOfWeek.Monday), Today),
IIF(DateInterval.Weekday = 6, 'Saturday = Monday This Week
DATEADD("d", 1 - DATEPART(DateInterval.WeekDay, Today,FirstDayOfWeek.Monday), Today),
IIF(DateInterval.Weekday = 7, 'Sunday = Monday This Week
DATEADD("d", 1 - DATEPART(DateInterval.WeekDay, Today,FirstDayOfWeek.Monday), Today),
Today
)
)
)
)
)
)
)
我无法弄清楚我是否需要使用子查询或where exists函数?
这是我当前的疑问:
Order#, Customer#, FreightPaid
12345, 00112233, Y
12346, 00112233, N
答案 0 :(得分:1)
我建议使用exists
子句而不是join
:
select ol.OrderID, ol.Accountuid as Customerno,
(case when exists (select 1
from Orderlog_item OLI join
Account A
on A.uid = OL.Accountuid
where OLI.orderlogkey = OL.[key] and A.GroupId = 'X9955'
)
then 1 else 0
end) as flag
from [SMILEWEB_live].[dbo].[OrderLog] OL
where OL.CreateDate >= GETDATE() - 60;
这可以防止出现一些问题。首先,当存在多个匹配行时会导致重复行(并且select distinct
会增加不必要的开销)。第二,缺少行,当您使用内部联接而不是外部联接时会发生这种情况。