March 28, 2014 at 7:13 am
IDNAME
1Deepak Kumar
2Sourabh Shrivastav
2Amit Wadhwa
3Ashutosh Tripathi
3Ashutosh Kumar
3Rizvi
“NameList” table has name of persons with ID.Select query is required for following condition:
If the Id’s are exactly two in number (e.g. count of ID = 2 is exactly two) then the names should be concatenated with 'OR'. When Ids are more than two (e.g. Id # 3 has a count of 3) , the values should be concatenated with 'AND'
Expected Result
IDName
1Deepak Kumar
2Sourabh Shrivastav OR Amit Wadhwa
3Ashutosh Tripathi AND Ashutosh Kumar AND Rizvi
March 28, 2014 at 7:49 am
You might start with this article. http://www.sqlservercentral.com/articles/comma+separated+list/71700/[/url]
It won't cover every requirement but it will be a good start. Give it a shot.
If you run into issues, feel free to post back and we can help. However, we are going to need a few things:
1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data
Please take a few minutes and read the first article in my signature for best practices when posting questions.
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply