Results 1 to 3 of 3

Thread: Question on sql

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Question on sql

    I have 2 tables one is called Question, and another one is called Section.

    In Section, there's sectionID, sectionName
    In Question, there's sectionID, questionID, and question

    What I am trying to a ccomplish is to retrieve the sectionName then list the questions that are under that section out. I have tried inner join, left join, but I am getting the sectionName every single time I print each question out. I just want it to appear once. I was going to do group by, but it's saying my statement is not sum, max/min.

    Section1
    Question1
    Question2

    Section2
    Question3
    Question4

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Question on sql

    I would suggest that what you're asking for should not be done at the database level. You should just perform a join in your SQL code and then the format you're asking for should be handled by application logic.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Question on sql

    I agree with jmcilhinney that this should be done in the application. Working off a suggestion from someone in another forum I put this together and I'm not pround of it. It seems to work though:

    Code:
    create table #Section(SectionID int, sectionname varchar(30))
    create table #Question(sectionid int, QuestionID int, Question varchar(30))
    insert into #section(sectionid, sectionname) values(1,'Math')
    insert into #Question(sectionid,questionid,question) values(1,1,'what is 1 + 1')
    insert into #Question(sectionid,questionid,question) values(1,2,'what is 2 + 2')
    
    insert into #section(sectionid, sectionname) values(2,'History')
    insert into #Question(sectionid,questionid,question) values(2,3,'Who is buried in Grant''s tomb')
    insert into #Question(sectionid,questionid,question) values(2,4,'How long was the 100 days war') 
    
    create table #temp(sectionid int, questionid int, text varchar(100))
    
    insert into #temp
    select SectionID, 0 as QuestionID, sectionname 'text'
    from #Section
    
    union all
    
    select SectionID, QuestionID, Question 'text'
    from #Question
    
    order by SectionID, QuestionID 
    
    select text from #temp
    
    drop table #section
    drop table #Question
    drop table #temp

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width