• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

SQL Problem

Hi Experts.

It's one of those days, I just cant get this to work as I want it to, can anyone help.

I have a table:
ID      ParentID      Title

1      NULL      Personal, social and emotional development      
2      NULL      Communication, language and literacy            
3      NULL      Problem solving, reasoning and numeracy            
4      NULL      Knowledge and understanding of the world      
5      NULL      Physical development                        
6      NULL      Creative development                        
7      1      Dispositions and attitudes                  
8      1      Social development                        
9      1      Emotional development                        
10      2      Language for communication and thinking            
11      2      Linking sounds and letters      
12      2      Reading                  
13      2      Writing            
14      3      Numbers as labels and counting      
15      3      Calculating
16      3      Shape, space and measure

I want to recreate the menu structure such that I return the title and next to it the relavent sub menus based on ID/Parent ID. This I can do. In addition if the parentID id null (its a top level) and it has no children then it's title appears in the same column as the sub menus. It can appear twice once in the main men and again in the sub ment columns, all I can get is it displays Null, which is it value.

Just to help there should be 6 different main menu headings, and 13 sub menus, 3 of which should be the same as the main menu.

Andy
0
Andy Green
Asked:
Andy Green
  • 4
  • 3
1 Solution
 
Kevin CrossChief Technology OfficerCommented:
Is this what you are looking to do?
SELECT m.ID, CASE WHEN p.Title IS NOT NULL THEN p.Title + ' - ' ELSE '' END + m.Title
FROM MenuTable m
LEFT JOIN MenuTable p ON p.ID  = m.ParentID

Open in new window

0
 
Andy GreenAuthor Commented:
Thanks, but not quite.
I dont need the concatenation, I need them in separate columns, there will be 13 in total with 3 rows, the id, the main menu and sub menu columns
I'll have a play with what you've given me so far.
Andy
0
 
varungdCommented:
Try this
SELECT parentId, Title from
 ( SELECT p1.ParentID,
          ( SELECT  Title + ','
              FROM TableName  p2
             WHERE p2.parentID = p1.ParentId
               FOR XML PATH('') ) AS Title
  FROM TableName  p1 
      GROUP BY ParentID
 
) A

Open in new window

0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Andy GreenAuthor Commented:
Sorry, I should have said 13 rows, 3 colums.
Andy
0
 
Kevin CrossChief Technology OfficerCommented:
-- select sub menu items with parent information
SELECT m.ID AS MenuID
, p.Title AS MainMenu
, p.Title AS SubMenu
FROM MenuTable m
INNER JOIN MenuTable p ON p.ID  = m.ParentID
UNION ALL -- can use UNION to filter out duplicates
-- join in parent menu with no children
SELECT ID AS MenuID
, Title AS MainMenu
, Title AS SubMenu
FROM MenuTable
WHERE ID NOT IN (SELECT ParentID FROM MenuTable)
AND ParentID IS NULL
0
 
Andy GreenAuthor Commented:
Still not right - it returns 3 column, but the menu and sub menu are the same.
this sql

SELECT a.AssessmentScale_ID, CASE WHEN b.AssessmentScale_Title IS NOT NULL THEN b.AssessmentScale_Title ELSE '' END , a.AssessmentScale_Title
FROM tbl_AssessmentScale a
LEFT JOIN tbl_AssessmentScale b ON b.AssessmentScale_ID = a.AssessmentScaleParent_ID

Returns this data
1                                                                                           Personal, social and emotional development
2                                                                                          Communication, language and literacy
3                                                                                           Problem solving, reasoning and numeracy
4                                                                                            Knowledge and understanding of the world
5                                                                                           Physical development
6                                                                                           Creative development
7  Personal, social and emotional development          Dispositions and attitudes
8 Personal, social and emotional development           Social development
9 Personal, social and emotional development          Emotional development
10 Communication, language and literacy                  Language for communication and thinking
11 Communication, language and literacy                     Linking sounds and letters
12 Communication, language and literacy                    Reading
13 Communication, language and literacy                    Writing
14 Problem solving, reasoning and numeracy            Numbers as labels and counting
15 Problem solving, reasoning and numeracy           Calculating
16 Problem solving, reasoning and numeracy           Shape, space and measure
This is what I want except - 1,2,& 3 have sub menus so I dont want to see them at the top, and 4, 5 & 6 dont have submenus so I want to see the text in the second colum as the menu title.
Andy
0
 
Kevin CrossChief Technology OfficerCommented:
That is a type-o, sorry.
-- select sub menu items with parent information
SELECT m.ID AS MenuID
, p.Title AS MainMenu
, m.Title AS SubMenu -- <<< this needed to be m.Title not p.Title
FROM MenuTable m
INNER JOIN MenuTable p ON p.ID  = m.ParentID
UNION ALL -- can use UNION to filter out duplicates
-- join in parent menu with no children
SELECT ID AS MenuID
, Title AS MainMenu
, Title AS SubMenu
FROM MenuTable
WHERE ID NOT IN (SELECT ParentID FROM MenuTable)
AND ParentID IS NULL

Open in new window

0
 
Andy GreenAuthor Commented:
Thank you, I unioned in the first 3 rows and it exactly what I wanted.

Andy
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now