gpt4 book ai didi

mysql - 如何进行正确的 SQL 递归?

转载 作者:行者123 更新时间:2023-11-29 23:17:53 26 4
gpt4 key购买 nike

我目前正在解决以下查询中的一个小错误处理问题:

declare @DayCounter int
declare @rows int
set @DayCounter = -2
set @rows = 0

while @rows = 0
begin
select
coalesce(p.name, case when len(ol.action)>1 then substring(ol.action,1,40)+'<br />'+substring(ol.action,41,80) else ca.code end) as description
, convert(varchar,DateAdd(minute,
15 * ((60 * Datepart(hour, latest_payment_status_change) +
Datepart(Minute, latest_payment_status_change)+
Case When DatePart(second, latest_payment_status_change) < 30
Then 7 Else 8 End) / 15),
DateAdd(day, DateDiff(day, 0, latest_payment_status_change), 0)),126) as date_time
, sum(num_of_persons) as num_tickets
from tick_order o
join tick_orderline ol on ol.order_id=o.id
join (
select olt.id
from tick_orderline_type olt
join tick_case ca on ca.id=olt.case_id
join tick_client tcl on tcl.id = ca.client_id
where tcl.id = 'CLIENT_TEST'
union
select orderline_type_id
from rpt_client_orderline_type rcot
join tick_client tcl on tcl.id = rcot.client_id
where tcl.id = 'CLIENT_TEST'
union
select olt.id
from tick_orderline_type olt
join rpt_client_case rcc on rcc.case_id=olt.case_id
join tick_client cl on cl.id=rcc.client_id
where cl.id = 'CLIENT_TEST'
) olt2 on olt2.id=ol.orderline_type_id
join tick_orderline_type olt on olt.id=ol.orderline_type_id
join tick_case ca on ca.id=olt.case_id
join tick_client cl on cl.id=ca.client_id
join tick_user_case uc on uc.case_id=ca.id
join tick_user u on u.id=uc.user_id
left join tick_promotion_code pc on pc.id=o.promotion_code_id
left join tick_promotion p on p.id=pc.promotion_id
where o.latest_payment_status_change>=dateadd(day,@DayCounter,current_timestamp)
and ((o.latest_payment_status_code = 'ORDER_PAYMENT_OK')
or (o.latest_payment_status_change > dateadd(minute,30,current_timestamp)
and o.latest_payment_status_code = 'ORDER_PAYMENT_WAITING' ))
group by
coalesce(p.name, case when len(ol.action)>1 then substring(ol.action,1,40)+'<br />'+substring(ol.action,41,80) else ca.code end)
, convert(varchar,DateAdd(minute,
15 * ((60 * Datepart(hour, latest_payment_status_change) +
Datepart(Minute, latest_payment_status_change)+
Case When DatePart(second, latest_payment_status_change) < 30
Then 7 Else 8 End) / 15),
DateAdd(day, DateDiff(day, 0, latest_payment_status_change), 0)),126)
order by 1,2

set @DayCounter = @DayCounter - 1;

if @@rowcount <> 0
begin
set @rows = @@rowcount;
print @DayCounter;
end;
end;

检索到的数据用于显示过去两天每种销售类型的销售额的图表。我现在想要实现的是:当过去两天没有销售时(@@rowcount = 0),每次再检查一天,直到找到数据。现在的查询返回类似这样的内容(我尝试让图像正常工作,但不知何故无法;有一个链接):

https://www.dropbox.com/s/0hculuegrc88c69/SQL_Result.png?dl=0

而且它不会停止,因为由于某种原因 @rows 变量保持为 0,尽管查询明确返回了行。即使使用 print @@rowcount 也会返回行。

那么我该如何解决这个问题呢?我应该使用完全不同的方法吗?

-祖巴贾

最佳答案

看来我不太明白雇主的意思。他不想在 SQL 中进行错误处理,而是在网站上进行:当过去两天没有找到数据时,只需显示一个空图表。

关于mysql - 如何进行正确的 SQL 递归?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27600079/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com