Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
Chanty4u
MVP
MVP

SQL Explain

Hi all

can anyone explain what below code does?

1.

WITH Quarters AS (
SELECT Q = 'Q1', MonthBegin = 11, MonthEnd = 12 UNION
SELECT Q = 'Q1', MonthBegin = 1, MonthEnd = 1 UNION
SELECT Q = 'Q2', MonthBegin = 2, MonthEnd = 4 UNION
SELECT Q = 'Q3', MonthBegin = 5, MonthEnd = 7 UNION
SELECT Q = 'Q4', MonthBegin = 8, MonthEnd = 10
)

2.

left(Datename(month,cast(S.ReportedMonth+'01' as date)),3) AS 'ReportedMonth',

3.

(q.Q+'FY'+right(S.reportedYear,2)) AS 'Fiscal_Quarter',

4 .

 

INNER JOIN Quarters q ON ( right(S.ReportedMonth,2) >= q.MonthBegin AND      right(S.ReportedMonth,2) <= q.MonthEnd )
where S.businessunit = 'SW' and S.ReportedYear = '2018'
1 Solution

Accepted Solutions
PrashantSangle

1: Your 1st sql query create temp table in memory. which store data like

Q, monthBegin, monthEnd

1, 11,12

2,1,1

3,2,4

4,5,7

5,8.10

Regards,

Great dreamer's dreams never fulfilled, they are always transcended.
Please appreciate our Qlik community members by giving Kudos for sharing their time for your query. If your query is answered, please mark the topic as resolved 🙂

View solution in original post

4 Replies
PrashantSangle

1: Your 1st sql query create temp table in memory. which store data like

Q, monthBegin, monthEnd

1, 11,12

2,1,1

3,2,4

4,5,7

5,8.10

Regards,

Great dreamer's dreams never fulfilled, they are always transcended.
Please appreciate our Qlik community members by giving Kudos for sharing their time for your query. If your query is answered, please mark the topic as resolved 🙂
dapostolopoylos
Creator III
Creator III

This code is kind of weird, can you share with us the full script?

The CTE in bullet 1 seems to be wrong, Q1 isn't from month 1 to month 3?

Father/Husband/BI Developer
jyothish8807
Master II
Master II

Hi Chanty,

1. "WITH" is used to set a name to the sub query block. You can refer it in many places in main sql query.

The Union statement is combining all the results of the statements.

Br,

KC

Best Regards,
KC
Chanty4u
MVP
MVP
Author

thanks all.   im pretty new to Sql queries.  Let me check some of the codes.