27,579
社区成员
发帖
与我相关
我的任务
分享
SELECT O_Guid
,MIN(createOn) AS createOn
FROM Record
WHERE createOn > '2016-1-1'
选择列表中的列 'Record.O_GUID' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中
--DATEDIFF(Second,u_b_dateTime,u_b_payTime)>3600*24,u_b_dateTime/u_b_payTime是那个表?
UPDATE d
SET [money]=CASE WHEN d.[level]=1 THEN CASE WHEN d.[money]+1000>=2000 THEN 2000 ELSE d.[money]+1000 END
ELSE CASE WHEN d.[money]+2000>=3000 THEN 3000 ELSE d.[money]+2000 END END
FROM ( SELECT O_Guid
,MIN(createOn) AS createOn
FROM Record
WHERE createOn > '2016-1-1' ) AS a
INNER JOIN Bill AS b ON b.O_Guid = a.O_Guid
AND Date1 BETWEEN a.createOn AND a.createOn
+ 65
AND b.Date2 < b.Date1 + 1
INNER JOIN [ORDER] AS c ON c.O_Guid = a.O_Guid
INNER JOIN [USER] AS d ON d.U_Guid = c.U_Guid
WHERE d.[level]=1 AND d.[money]<2000 OR d.[level]=2 AND d.[money]<3000
;
CREATE TABLE [dbo].[User](
[id] [int] NULL,
[U_GUID] [varchar](36) NULL,
[money] [money] NULL,
[level] [int] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Order](
[id] [int] NULL,
[O_GUID] [varchar](36) NULL,
[U_GUID] [varchar](36) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Bill](
[id] [int] NULL,
[O_GUID] [varchar](36) NULL,
[date1] [datetime] NULL,
[date2] [datetime] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Record](
[id] [int] NULL,
[O_GUID] [varchar](36) NULL,
[U_GUID] [varchar](36) NULL,--既然有这字段,就写上,可以少连一步
[createOn] [datetime] NULL
) ON [PRIMARY]
GO
with t as (
select u.*,MIN(r.createOn) MinCreateOn
from [User] u join Record r on u.U_GUID=r.U_GUID
where r.createOn>cast('2016-01-01' as datetime)
)
,tt as (
select
distinct t.*,b.date1,b.date2
from t
join Record r on t.U_GUID=r.U_GUID
and r.createOn-t.MinCreateOn<=65
join [Order] o on r.O_GUID=o.O_GUID
join Bill b on b.O_GUID=o.O_GUID
), u as (
select t.id
from t
--(你是不是要求,所有订单都是1天内付款的这种的用户?如果有一单,隔天付款,就不要这个用户了?如果是这么理解,那么用下句where。不存在>3600*24的用户)
where not exists(select 1 from tt where tt.id=t.id and DATEDIFF(Second,date1,date2)>3600*24)
)
update [User] set [money]=
case [level]
when 1
then case when [money]+1000>2000 then 2000 else [money]+1000 end
when 2
then case when [money]+2000>3000 then 3000 else [money]+2000 end
end
from [User] u1 join u on u1.id=u.id