Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8208339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:18:24+00:00 2026-06-07T09:18:24+00:00

Can anybody help me in writing an sql query which is defined below: I’m

  • 0

Can anybody help me in writing an sql query which is defined below:
I’m having two table Itemtype and Items

Itemtype
-------------------
ItemTypeID ItemParent TypeName
-------------------------------
1           0          XXX
2           1          YYY
3           1          ZZZ
4           0          SSS
5           4          GGG

Items
--------------------
ItemID ItemTypeID ItemCost
----------------------------------
1        1         5000
2        2        1000
3        4        250
4        3        2000
5        5        400

Output
---------------------------
ItemtypeName ItemCost
------------------------------
XXX            8000 (1000+5000+2000)
SSS            650 (250+400)

These are the two tables:I joined both table using itemtypeid.
Now i need to display Items with ItemParentId = 0
and
calculate the Itemcost of Items with itemtypeId and ItemparentTypeId having value same as that of ItemTypeID.

I wrote a query using CTE,but its not displaying ItemTypeName.

WITH it_cte AS ( select itemtypeid from ItemType WHERE
ItemType.ItemParentType IS NULL UNION ALL select i.ItemTypeid from
ItemType i  INNER JOIN it_cte icte ON icte.itemtypeid = i.itemtypeid )
select ItemParentType,SUM(Items.ItemCost) as itemcost from ItemType 
left join Items on ItemType.ItemTypeID = Items.ItemTypeID  or
ItemType.ItemParentType= Items.ItemTypeID  group by
ItemType.ItemParentType

Can anybody help?

Thanks
Jamuna

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:18:26+00:00Added an answer on June 7, 2026 at 9:18 am

    Following statement

    • uses a CTE to retrieve a list of each ItemTypeID with its root ItemTypeID
    • joins with ItemType to get the TypeName
    • joins with Items to get the ItemCost
    • groups on TypeName to get the sum of the ItemCost

    SQL Statement

    ;WITH q AS (
      SELECT  ItemTypeID , Root = ItemTypeID
      FROM    ItemType
      WHERE   ItemParent = 0
      UNION ALL
      SELECT  t.ItemTypeID, q.Root
      FROM    q
              INNER JOIN ItemType t ON t.ItemParent = q.ItemTypeID
    ) 
    SELECT  it.TypeName, SUM(i.ItemCost)
    FROM    q
            INNER JOIN ItemType it ON it.ItemTypeID = q.Root
            INNER JOIN Items i ON i.ItemTypeID = q.ItemTypeID
    GROUP BY
            it.TypeName        
    

    Test script

    ;WITH ItemType (ItemTypeID, ItemParent, TypeName) AS (
      SELECT 1, 0, 'XXX' UNION ALL
      SELECT 2, 1, 'yyy' UNION ALL
      SELECT 3, 1, 'ZZZ' UNION ALL
      SELECT 4, 0, 'SSS' UNION ALL
      SELECT 5, 4, 'GGG'
    )
    , Items (ItemID, ItemTypeID, ItemCost) AS (
      SELECT 1, 1, 5000 UNION ALL
      SELECT 2, 2, 1000 UNION ALL
      SELECT 3, 4, 250 UNION ALL
      SELECT 4, 3, 2000 UNION ALL
      SELECT 5, 5, 400
    )
    , q AS (
      SELECT  ItemTypeID , Root = ItemTypeID
      FROM    ItemType
      WHERE   ItemParent = 0
      UNION ALL
      SELECT  t.ItemTypeID, q.Root
      FROM    q
              INNER JOIN ItemType t ON t.ItemParent = q.ItemTypeID
    ) 
    SELECT  it.TypeName, SUM(i.ItemCost)
    FROM    q
            INNER JOIN ItemType it ON it.ItemTypeID = q.Root
            INNER JOIN Items i ON i.ItemTypeID = q.ItemTypeID
    GROUP BY
            it.TypeName        
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anybody help me writing a regular expression to replace these characters with a
Some beginner help needed if anybody can.. I'm writing a contact form with a
Can anybody help me with this: I want select all records from datatable which
Im having very tough challenge in converting the following code C#, can anybody help
Can anybody help me how can I invoke a JSP page from a Servlet
can anybody help me understand how Honda achieved this effect: http://testdays.hondamoto.ch/ I mean the
Can anybody help me in finding out how to verify that _tmkdir succeeded. For
Can anybody help how to setup a automatic backup of mysql database and then
can anybody help me how to nice integrate doctrine 2.0 into ZF 1.10? I
Can anybody help me with retrieving some elements from the following example text: sdfaasdflj

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.