Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Group by based upon condition


Group by based upon condition

Author
Message
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16680 Visits: 17038
Luis Cazares (7/23/2013)
Sean Lange (7/23/2013)
I didn't just provide a solution because this looks so much like homework and the OP did not answer that one way or the other.


If this post was on a 2008 forum, I would agree. However, I can't believe that they're teaching using SQL Server 2000. Maybe the OP can be clear on that now that he has the answer.


I have seen far worse things being taught. :-D

After the last response I am not as certain this is homework but who knows.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
gksharmaajmer
gksharmaajmer
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 112
Hi Sean Lange,

Thanks for correcting me for pronounce the name and I am sorry or my mistake on it. Since, I am an Oracle DBA, and actually this question is related to my friend who is SQL Server DBA. I posted the same question on Oracle Forum too, because I am really very poor in SQL, so first I posted here and then tried to see the solution how it will work in Oracle, so that I can try to see its flow in SQL Server, but again my lack of knowledge failed to achieve the required output.

I am really Thankful to you and Luis Cazares for great replies.

But, I don't know why I am getting below error when I ran your way :
Server: Msg 209, Level 16, State 1, Line 27
Ambiguous column name 'rollno'.

If you and other members allow me to post my friend's question which are only related to version 2000, I will learn SQL and my friend will follow me.

Regards
Girish Sharma
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16680 Visits: 17038
gksharmaajmer (7/23/2013)

But, I don't know why I am getting below error when I ran your way :
Server: Msg 209, Level 16, State 1, Line 27
Ambiguous column name 'rollno'.


Try this.

It actually worked fine on my 2008 box but honestly I would expect to see that message too.


select s.*, ISNULL(se.sortorder, 6) as SortOrder
from student s
left join #Sequence se on se.ClassName = s.class
join
(
select rollno, min(ISNULL(sortorder, 6)) as SortOrder
from student s
left join #Sequence se on se.ClassName = s.class
group by rollno
) g on g.rollno = s.rollno and g.SortOrder = ISNULL(se.sortorder, 6)
order by s.rollno, ISNULL(se.sortorder, 6)



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8592 Visits: 18175
You'll always be welcome here to learn as we all learn here.
The error you're getting is because the rollno on the order by is not qualified. Just add "s." before rollno and everything should be ok.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16680 Visits: 17038
Luis Cazares (7/23/2013)

The error you're getting is because the rollno on the order by is not qualified. Just add "s." before rollno and everything should be ok.


Here is an odd one...I changed my compatibility mode to 2k and I got that error. Looking at the code it should throw that in 2008 also because the column truly is ambiguous. In 2k mode it fails but it can be qualified with either g or s and it works (as expected). In 2008 it apparently doesn't care that it is ambiguous. w00t

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
gksharmaajmer
gksharmaajmer
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 112
Yes, now no error, no more doubt on this question... only big thanks from mine and my friend side.

We both learnt good stuff with your and Luis Cazares's great replies.

Regards
Girish Sharma
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search