Solved

how to do multiple partitioning

Posted on 2011-09-08
6
193 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

717 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