× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



You cannot have an ORDER BY in the CTE.


Op 17-4-2020 om 20:13 schreef Tom Hambel:
One idea would be to wrap your statement in a CTE and compare that way.

With temp(id,usage,useyear,usemonth) as
(
SELECT id, sum(metereduse), year(getdatesql(readdate)),
month(getdatesql(readdate)) FROM dailyuse
GROUP BY id, year(getdatesql(readdate)), month(getdatesql(readdate)) ORDER BY id, year(getdatesql(readdate)), month(getdatesql(readdate))
)
Select id,usage,useyear,usemonth
from temp a
Join temp b on a.id=b.id and
(a.useyear=b.useyear and b.usemonth=a.usemonth+1 and a.usemonth<12
Or a.usemonth=12 and b.usemonth=1 and b.useyear=a.useyear+1)
where b.usage>=a.usage*1.5

the join will depend on which you want to compare too.

What we did to make these scenarios much easier is create a calendar table with all attributes of a date; numeric, cymd,mdy,month,date,period, date fields etc.
Then we just join to that and do our thing.
With the date data type in the join file it makes date manipulation super easy.





CENTRAL SEMICONDUCTOR CORP. CONFIDENTIALITY NOTICE: This electronic mail transmission may be privileged, contain trade secrets, or otherwise confidential information and should be read or retained only by the intended recipient. If you have received this transmission in error, you are hereby notified that any review, copying or distribution of it is strictly prohibited. Please inform us immediately and destroy the original transmittal. Thank you for your cooperation.


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.