Solved

how to do multiple partitioning

Posted on 2011-09-08
6
185 Views
Last Modified: 2012-05-12
Hi everyone

I was wondering if its possible to partition a set of data then get this data and partition it again?

My scenarios are
1) Need to firstly check that a register which has the same date, time, staff_code, week_no, and course I want to only pick the modules beginning with m (b.aos_code sorted desc)
2) Having carried out the first check I then need check the data for registers that occur same date, time, staff_code, week_no which have two courses attached in this case I want to select the highest nvq_lvl and if the nvq_lvls are the same then check the hrs_total

I have seperated both scenarios into partitions but im not sure if you can do what I have attached below ie do you need to partition and then store in a table and then partition again?
(I have removed the tables to shorten the code)

Help appreciated
select acad_period, register_id, register_group, staff_code, week_no, day_of_week, date, [month_no],
start_time, end_time,
convert(varchar, convert(int,floor([Total Hours])))+':'+
replicate('0',(2 - len(convert (varchar, convert(int,([Total Hours] - floor([Total Hours])) * 60.0)))))
+convert (varchar, convert(int,([Total Hours] - floor([Total Hours])) * 60.0)) as 'Total Hours',
dept_code, 
[m_code], [m_period], [m_desc], [c_code], [c_period], [c_desc], 
[c_moa], [c_hrs], nvq_lvl_ind, [rn], [rn1]


FROM
(
			select a.acad_period, a.register_id, a.register_group, a.staff_code,
			a.week_no,
			c.day_of_week, g.date,
			left(CONVERT ( char(20) , g.date , 101 ),2) as 'month_no',
			c.start_time, c.end_time,
			ROUND(cast((datediff(mi, MIN(c.start_time), MAX(c.end_time))  / 60.0) as FLOAT),2) AS 'Total Hours',
			c.dept_code, 
			b.aos_code as 'm_code', b.aos_period as 'm_period', f.full_desc as 'm_desc',
			e.aos_code as 'c_code', e.aos_period as 'c_period', e.full_desc as 'c_desc', e.moa_code as 'c_moa',
			e.hrs_total as 'c_hrs',
			h.nvq_lvl_ind,
			row_number() over (partition by a.register_id, a.register_group, a.staff_code, a.week_no, c.day_of_week, g.date,
							c.start_time, c.end_time, e.aos_code, e.aos_period
                         order by b.aos_code desc ) as rn ,
			row_number() over (partition by a.register_id, a.register_group, a.staff_code, a.week_no, c.day_of_week, g.date,
							c.start_time, c.end_time
                         order by h.nvq_lvl_ind, e.hrs_total desc ) as rn1 

			from tables ....

			where a.acad_period = '10/11'

			group by a.acad_period, a.register_id, a.register_group, a.staff_code, a.week_no,
			c.day_of_week, g.date, c.start_time,
			c.end_time, c.dept_code, 
			b.aos_code, b.aos_period, f.full_desc,
			e.aos_code, e.aos_period, e.full_desc, 
			e.moa_code, e.hrs_total, h.nvq_lvl_ind

) X

where (rn = 1 and rn1 = 1)

order by staff_code, [c_code], [c_period], register_id, register_group, date

Open in new window

0
Comment
Question by:lisa_mc
  • 4
  • 2
6 Comments
 
LVL 39

Expert Comment

by:lcohan
ID: 36505633
No you can't. SQL table Partitioning is based on a single partition key and that's why is so important to choose the right partitioning key before you do it.

"The partition key must exist as a single column in the table and must meet certain criteria."
http://msdn.microsoft.com/en-us/library/ms345146(v=sql.90).aspx#sql2k5parti_topic9


from general topic
http://msdn.microsoft.com/en-us/library/ms345146(v=sql.90).aspx
0
 
LVL 3

Author Comment

by:lisa_mc
ID: 36509138
Hi Icohan

I didnt think I could do that becasue everytime I executed it I was getting a different number of rows so I know something wasn't right.

Do you know is there any other way to do this ie could I partition then rank - sort of briefly looked it up last night so need to look at it more.

Or should I just do the first partition copying the results into a #temp_table then carry out the second partition

what do you think - do you have any suggestions?

Thanks
0
 
LVL 3

Accepted Solution

by:
lisa_mc earned 0 total points
ID: 36510411
thinking about this more and more I have figured out that I only ever need one partition because I dont care what the class name is - is not important to me as long as I have the course description but by using the partition

row_number() over (partition by a.register_id, a.register_group, a.staff_code, a.week_no, c.day_of_week, g.date,
                                          c.start_time, c.end_time
                         order by h.nvq_lvl_ind, e.hrs_total desc ) as rn1

This will only ever pick one row therefore I do not need to carry out another partition before this

I was so deep into thinking about this I made it much more difficult than it should have been
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Author Closing Comment

by:lisa_mc
ID: 36534646
I have accepted my own comment as it turns out I only ever need one partition because even if I do select a register which doesn't have a module (beginning with m) attached (using partition rn) I will always gets the correct answer using (partition Rn1) because I don't care what the module name is I am only extracting the course details
0
 
LVL 39

Expert Comment

by:lcohan
ID: 36511646
My objection is that I gave an answer to the question AS IT WAS posted however all the comments above and further to that are not related and far from that.
0
 
LVL 3

Author Comment

by:lisa_mc
ID: 36521187
Hi Icohan

I also asked another question which was

>> I have seperated both scenarios into partitions but im not sure if you can do what I have attached below ie do you need to partition and then store in a table and then partition again? <<

But from your first comment you haven't answered this

I also thought that you maybe could have advised me as to why I was trying to partition twice when I only needed to ever partition once as one partition will only ever bring out one line of data.  Especially since in the second partition I mentioned nothing about modules therefore is not seen as important

I did not ask how to partition as I had attached code showing that I understood this - whereas your answer shows me how to structure a partition correctly and why use partitioning (which I did not ask)

I don't have a problem with assigning points but I am not going to give them away when I feel I came to answer the question first - sorry
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 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

18 Experts available now in Live!

Get 1:1 Help Now