Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing COUNT on a self join query
    primarykey
    data
    text
    <p>I have a query that populates a double select list. The query retrieves main categories and sub categories from the same table via a self join. Basically it joins itself to get the subcategories in each category using id and parent id. I have tried to use COUNT to get the number of subcategories so I can add the number to the output of the first list. I didn't write the query , I just want to refactor it to count the subs in each main. </p> <pre><code>SELECT root.name AS root_name, root.link AS root_link, root.RETAIL AS root_RETAIL, root.WHOLESALE AS root_WHOLESALE, root.linkto AS root_linkto, root.keywords AS root_keywords, root.description AS root_description, root.id AS root_id, down1.name AS down1_name, down1.linkto AS down1_linkto, down1.link AS down1_link, down1.keywords AS down1_keywords, down1.description AS down1_description, down1.id AS down1_id, down1.parentid AS down1_parentid, down1.RETAIL AS down1_RETAIL, down1.WHOLESALE AS down1_WHOLESALE FROM categories AS root LEFT OUTER JOIN categories AS down1 ON down1.parentid = root.id WHERE root.parentid = 0 ORDER BY root_name, down1_name </code></pre> <p>This returns everything to populate both lists. I try to surround either <code>down1_name</code> or <code>down1_id</code> with <code>COUNT</code> but it counts everything - not the number of subs in each main. I'm not sure about using another self join to do it or a nested <code>SELECT</code> with <code>COUNT</code>. Any insight would be much appreciated. Tks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload