Solved

What is this SQL Code doing?

Posted on 2012-03-30
6
218 Views
Last Modified: 2012-03-30
Can someone tell me what "umc" is referencing, or how it is being populated in this query.  
I have a basic understanding of the #temp for temp tables but I am a bit puzzled with what is going on with "umc".
umc.txt
0
Comment
Question by:morinia
  • 3
  • 2
6 Comments
 
LVL 9

Accepted Solution

by:
OCDan earned 400 total points
ID: 37788446
Basically they have just called the larger query below UMC. It is just an alias for the query it is the equivalent of OH in the below query:
select * from orderheader as OH

Your query:
(
--QUERY1
SELECT
  mc.member_id, mc.enrollment_date , mc.exit_date
, mc.primary_secondary , mc.active_code , mc.benefit_plan
, mc.benefit_product, mc.benefit_group, 'CURR' as ONFILE
from llm.llmdba.member_coverage mc (nolock)
   , #mbrLkup m (nolock)
where
   mc.enrollment_date < '01/01/2012'
  and (mc.exit_date >= '01/01/2011' or mc.exit_date is null)
  and (convert(varchar(10),mc.enrollment_date,120) <> convert(varchar(10),mc.exit_date,120) or mc.exit_date is null)
  and mc.member_id = m.member_id
union
--QUERY2
SELECT
  mc.member_id, mc.enrollment_date, mc.exit_date, mc.primary_secondary
, mc.active_code, mc.benefit_plan, mc.benefit_product, mc.benefit_group, 'HIST' as ONFILE
from llm.llmdba.member_coverage_history mc (nolock), #mbrLkup m (nolock)
where
      mc.enrollment_date < '01/01/2012'  and (mc.exit_date > '01/01/2011' or mc.exit_date is null)
  and (convert(varchar(10),mc.enrollment_date,120) <> convert(varchar(10),mc.exit_date,120) or mc.exit_date is null)
  and mc.member_id = m.member_id
) umc

Hope that helps.
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 100 total points
ID: 37788464
umc is referencing the results of the sub-select that starts on line 40 and finishes on line 86:

into #mbr_covg0
from
(     <------    starts here
SELECT
  mc.member_id
...
...
) umc    <-- finishes here
0
 

Author Comment

by:morinia
ID: 37788508
SELECT umc.*
, dense_rank()
       over (partition by umc.member_id
             order by umc.enrollment_date desc
                    , umc.exit_date)
       as nRank

What is the first six lines of the code doing?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 9

Assisted Solution

by:OCDan
OCDan earned 400 total points
ID: 37788520
select umc.*
selects all the fields from UMC subquery as said above

Dense_rank():
Ranks the results per each umc.member_id with the most recent enrollment date and the earliest exit date being the highest ranked.

Have a read of this link on
Ranking Functions it should help you understand the dense_rank function a bit better.
0
 

Author Comment

by:morinia
ID: 37788594
The use of temporary tables and partitions is this regular SQL or Transact-Sql?  Since you are able to access temproary tables as well as the tables on the server what differentiates this code from regular  MSSql code?
0
 
LVL 9

Expert Comment

by:OCDan
ID: 37788603
Yes they get used quite a lot in TSQL soryr mate not sure what you mean by regular SQL.

This is valid MSSQL(TSQL) code, copying it into Sql Server Management Studio and performing a syntax check runs fine, so it is likely valid.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now