Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
mikecro
Starting Member
21 Posts |
Posted - 2010-02-18 : 10:24:25
|
I need to provide a view that contains data on incoming students (starting within 14 days or currently attending), however the view will return multiple rows for each student if they are returning the next month. What I need to do is select the min start_dt so that only one record will be returned for each student. I can do this easily in a stored proc by creating a temp table and then selecting from that. Unfortunately, SQLSERVER won't allow tmp tables in views.Any ideas on how to create the view without using a tmp table would be greatly appreciated!!Here's the sql from the SP: SELECT s.student_id, s.LastName, s.FirstName, s.MiddleName, d.department, MIN(b.start_dt) AS start_dt, MAX(b.end_dt) AS end_dt INTO #TMPQUERY1 FROM (SELECT student_id, LastName, FirstName, MiddleName FROM dbo.Student) AS s INNER JOIN (SELECT student_id, block_id, assignment_id FROM dbo.Assignments WHERE status = 'approved') AS a ON s.student_id = a.student_id INNER JOIN (SELECT block_id, elective_id, start_dt, end_dt FROM dbo.Block_Schedule WHERE (end_dt > GETDATE()) AND (start_dt <= GETDATE() + 14)) AS b ON a.block_id = b.block_id INNER JOIN (SELECT elective_id, dept_id FROM dbo.Electives) AS e ON b.elective_id = e.elective_id INNER JOIN (SELECT Dept_id, name AS department FROM dbo.Departments) AS d ON e.dept_id = d.Dept_id GROUP BY s.student_id, s.LastName, s.FirstName, s.MiddleName, d.department SELECT T1.* FROM #TMPQUERY1 T1 WHERE T1.START_DT = (SELECT MIN(START_DT) FROM #TMPQUERY1 WHERE T1.student_id=student_id GROUP BY student_id) ORDER BY LASTNAME DROP TABLE #TMPQUERY1 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-18 : 10:27:07
|
| [code]SELECT reqd columns...FROM(SELECT ROW_NUMBER() OVER(PARTITION BY s.student_id ORDER BY b.start_dt) AS Seq, s.student_id, s.LastName, s.FirstName, s.MiddleName, d.department, b.start_dt AS start_dt, b.end_dt AS end_dt INTO #TMPQUERY1 FROM (SELECT student_id, LastName, FirstName, MiddleName FROM dbo.Student) AS s INNER JOIN (SELECT student_id, block_id, assignment_id FROM dbo.Assignments WHERE status = 'approved') AS a ON s.student_id = a.student_id INNER JOIN (SELECT block_id, elective_id, start_dt, end_dt FROM dbo.Block_Schedule WHERE (end_dt > GETDATE()) AND (start_dt <= GETDATE() + 14)) AS b ON a.block_id = b.block_id INNER JOIN (SELECT elective_id, dept_id FROM dbo.Electives) AS e ON b.elective_id = e.elective_id INNER JOIN (SELECT Dept_id, name AS department FROM dbo.Departments) AS d ON e.dept_id = d.Dept_id)tWHERE Seq=1 [/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mikecro
Starting Member
21 Posts |
Posted - 2010-02-18 : 10:37:58
|
| Thank you visakh16! Works like a charm!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-18 : 10:40:01
|
welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mikecro
Starting Member
21 Posts |
Posted - 2010-02-19 : 09:23:43
|
Sorry to rehash this, but I'm getting the following error when I save the view:"The OVER SQL construct or statement is not supported."The view runs fine, but it's causing a problem with an application that is accessing the view.I'm running SqlServer 2005 with compatibility level=90.SELECT TOP (100) PERCENT student_id AS pk_student_id, LastName, FirstName, MiddleName, department,start_dt, end_dtFROM (SELECT ROW_NUMBER() OVER (PARTITION BY s.student_id ORDER BY b.start_dt) AS SEQ, s.student_id, s.LastName, s.FirstName, s.MiddleName, d .department, MIN(b.start_dt) AS start_dt, MAX(b.end_dt) AS end_dtFROM (SELECT student_id, LastName, FirstName, MiddleName FROM dbo.Student) AS s INNER JOIN (SELECT student_id, block_id, assignment_id FROM dbo.Assignments WHERE status = 'approved') AS a ON s.student_id = a.student_id INNER JOIN (SELECT block_id, elective_id, start_dt, end_dt FROM dbo.Block_Schedule WHERE (end_dt > GETDATE()) AND (start_dt <= GETDATE() + 14)) AS b ON a.block_id = b.block_id INNER JOIN (SELECT elective_id, dept_id FROM dbo.Electives) AS e ON b.elective_id = e.elective_id INNER JOIN (SELECT Dept_id, name AS department FROM dbo.Departments) AS d ON e.dept_id = d .Dept_idGROUP BY s.student_id, s.LastName, s.FirstName, s.MiddleName,b.start_dt, b.end_dt, d .department) AS TWHERE SEQ = 1 Thanks for any help! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-19 : 09:27:24
|
| If you've using SQL 2005 with compatibility level 90 or above then it should work. Can you run below and post back result?SELECT @@VERSIONEXEC sp_dbcmptlevel 'your db name'------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mikecro
Starting Member
21 Posts |
Posted - 2010-02-19 : 10:33:54
|
| Result: The current compatibility level is 90. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-19 : 10:35:04
|
quote: Originally posted by mikecro Result: The current compatibility level is 90.
then ROW_NUMBER() should work.Which editor are you using?Can you try this in query analyser?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mikecro
Starting Member
21 Posts |
Posted - 2010-02-19 : 10:42:03
|
| I'm using the SSMS, which I believe incorporates query analyzer in the editor. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-19 : 10:48:22
|
quote: Originally posted by mikecro I'm using the SSMS, which I believe incorporates query analyzer in the editor.
Its working for me anyways------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mikecro
Starting Member
21 Posts |
Posted - 2010-02-19 : 11:03:55
|
| Thanks again for your help. It does run and the results are correct, but SqlServer doesn't like it for some reason. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-19 : 11:13:15
|
quote: Originally posted by mikecro Thanks again for your help. It does run and the results are correct, but SqlServer doesn't like it for some reason.
how did you see result then?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|