SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to create a marker field based on data


How to create a marker field based on data

Author
Message
amar_kaur16
amar_kaur16
Valued Member
Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)

Group: General Forum Members
Points: 58 Visits: 178
I have a dataset for students and they have either selected 'Math' or 'Science' or both.
So I have 2 rows, 1 for Math and 1 for Science if they have selected both or 1 row if they have selected either of those 2 subjects.
Now I need to select data for those students who have selected only Math or both and I need to create a marker field (1,0) if they selected both then 1 else 0.
So 1 row for each student and if opted both then 1 in the marker field.
How can I do that?


Thanks,
Blyzzard
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26526 Visits: 17557
amar_kaur16 (11/26/2012)
I have a dataset for students and they have either selected 'Math' or 'Science' or both.
So I have 2 rows, 1 for Math and 1 for Science if they have selected both or 1 row if they have selected either of those 2 subjects.
Now I need to select data for those students who have selected only Math or both and I need to create a marker field (1,0) if they selected both then 1 else 0.
So 1 row for each student and if opted both then 1 in the marker field.
How can I do that?


Thanks,
Blyzzard




Kind of an odd requirement but this works for your description.


IF OBJECT_ID('TempDB..#MathScience','U') IS NOT NULL
drop table #MathScience

create table #MathScience
(
StudentID int,
SubjectChoice varchar(10)
)

insert #MathScience
select 1, 'Math' union all
select 1, 'Science' union all
select 2, 'Science' union all
select 3, 'Math'

select StudentID, Case when COUNT(*) > 1 then 1 else 0 end as Marker
from #MathScience
group by StudentID
having COUNT(*) > 1 or MIN(SubjectChoice) = 'Math'




Notice how I posted ddl and sample data. That is something you should do in the future to make things easier for us.

_______________________________________________________________

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.

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)
amar_kaur16
amar_kaur16
Valued Member
Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)

Group: General Forum Members
Points: 58 Visits: 178
Could you please explain?

Thanks,
Blyzzard
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26526 Visits: 17557
amar_kaur16 (11/26/2012)
Could you please explain?

Thanks,
Blyzzard


What part don't you understand? I am not even really sure if this meets your criteria.

_______________________________________________________________

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.

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)
amar_kaur16
amar_kaur16
Valued Member
Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)

Group: General Forum Members
Points: 58 Visits: 178
Actually I need to select all the fields and rows for Math subject and craete a marker so that if any student has selected Science as well then I it should be 1.
So basically I need to get Math data but need to indicate that some of them has selected both.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26526 Visits: 17557
That is what the query I posted will show you.

Here is another way to get the same results, but it has a bit more complicated execution plan.


select StudentID, Case when COUNT(*) > 1 then 1 else 0 end as Marker
from #MathScience
where StudentID in (select StudentID from #MathScience where SubjectChoice = 'Math')
group by StudentID



If there is something missing then you will need to post YOUR ddl, sample data and desired output. The stuff I am working with is because you didn't provide so I had to make some up.

_______________________________________________________________

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.

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)
amar_kaur16
amar_kaur16
Valued Member
Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)

Group: General Forum Members
Points: 58 Visits: 178
Your query is selecting all the students who have selected Math only but the Marker field is empty for all the returned students.
Thus I think that students who have selected both are not being included

Below is the input format
Student Id Name Subject Selection Date
10021 john Math 11/1/2011
10021 John Science 11/12/2011
10022 Becky Math 11/2/2011
10022 Becky Science 11/10/2011
10023 Will Math 11/4/2011
10024 Matt Math 11/5/2011

Output desired:

Student Id Name Subject Selection Date Marker ( Math& Science)
10021 john Math 11/1/2011 1
10022 Becky Math 11/2/1011 1
10023 Will Math 11/4/2011 0
10024 Matt Math 11/5/2011 0

I appreciate your help.
Thanks,
Blyzzard
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26526 Visits: 17557
amar_kaur16 (11/26/2012)
Your query is selecting all the students who have selected Math only but the Marker field is empty for all the returned students.
Thus I think that students who have selected both are not being included

Below is the input format
Student Id Name Subject Selection Date
10021 john Math 11/1/2011
10021 John Science 11/12/2011
10022 Becky Math 11/2/2011
10022 Becky Science 11/10/2011
10023 Will Math 11/4/2011
10024 Matt Math 11/5/2011

Output desired:

Student Id Name Subject Selection Date Marker ( Math& Science)
10021 john Math 11/1/2011 1
10022 Becky Math 11/2/1011 1
10023 Will Math 11/4/2011 0
10024 Matt Math 11/5/2011 0

I appreciate your help.
Thanks,
Blyzzard


It seems you did not run the query I posted. And you really need to look at the link in my signature about how to post data. The format you posted is not helpful. Here is your data in a consumable format. I added Name to both of the queries I posted and they BOTH return exactly what you are looking for. I removed the selection date from the results because it doesn't make sense, which one will be in the result set when there are 2?


IF OBJECT_ID('TempDB..#NewMathScience','U') IS NOT NULL
drop table #NewMathScience

create table #NewMathScience
(
StudentID int,
Name varchar(20),
SubjectChoice varchar(20),
SelectionDate datetime
)

insert #NewMathScience
select 10021, 'john', 'Math', '2001-11-01' union all
select 10021, 'John', 'Science', '2011-11-12' union all
select 10022, 'Becky', 'Math', '2011-11-02' union all
select 10022, 'Becky', 'Science', '2011-11-10' union all
select 10023, 'Will', 'Math', '2011-11-04' union all
select 10024, 'Matt', 'Math', '2011-11-05'


select StudentID, Name, Case when COUNT(*) > 1 then 1 else 0 end as Marker
from #NewMathScience
group by StudentID, Name
having COUNT(*) > 1 or MIN(SubjectChoice) = 'Math'

select StudentID, Name, Case when COUNT(*) > 1 then 1 else 0 end as Marker
from #NewMathScience
where StudentID in (select StudentID from #NewMathScience where SubjectChoice = 'Math')
group by StudentID, Name



_______________________________________________________________

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.

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)
Dennis Post
Dennis Post
Old Hand
Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)

Group: General Forum Members
Points: 397 Visits: 547
Wow Sean,
10 points for effort and patience!



For better, quicker answers on T-SQL questions, read Jeff Moden's suggestions.

"Million-to-one chances crop up nine times out of ten." ― Terry Pratchett, Mort
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