?
Solved

how to do multiple partitioning

Posted on 2011-09-08
6
Medium Priority
?
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 40

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 40

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

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

770 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