MS SQL Server Query

Posted on 2012-09-11
Last Modified: 2012-09-16
Is there any way to create a query that would return data in the following format:

Category     Name     Desc                Purchase Price
                   Table     Wood table                   299.99
                   Chair     High back                        99.99
                   Desk      Long Desk                     199.99  
Total (3)                                                         599.97

                   Laptop   HP Laptop                     999.99
                   Mouse    Optical                             9.99
Total (2)                                                        1009.98
Question by:koossa
    LVL 25

    Expert Comment

    by:Lee Savidge
    Difficult to help as you haven't given any indication of the table structure where the data is held.

    To answer your question, this looks like a grouping will be required and it will not give you this output in this way. It looks like you need a report to do this where you can group and display data in a formatted way.

    Author Comment

    Original Data is in the following format:

    Name     Desc     Category     Purchase Price
    LVL 2

    Accepted Solution

    Of course to get total, you could use GROUP BY WITH ROLLUP.
    Smth like that:
    SELECT name, desc, SUM(Purchase Price)
    FROM Furniture
    GROUP BY name, desc

    but it will give you only furnitures.

    Probably you really need a report. And it looks like a report :)
    LVL 2

    Expert Comment

    Here you have an example which should help:
    ;WITH cte as (
          SELECT 'table' id, 'furniture' c, 1000 value
          SELECT 'chair' id, 'furniture' c, 2000 value
          SELECT 'table' id, 'furniture' c, 3000 value
          SELECT 'chair' id, 'furniture' c, 4000 value
          SELECT 'table' id, 'furniture' c, 5000 value
          SELECT 'laptop' id, 'IT' c, 6000 value
          SELECT 'mouse' id, 'IT' c, 7000 value
    SELECT id, c, SUM(value)--, GROUPING(c)
    FROM cte
    GROUP BY id, c
    ORDER BY c, id DESC
    LVL 25

    Expert Comment

    Formatting should really be done in the presentation layer (reporting tool, code etc) ... SQL was not really designed for it.  But here is my stab:
    ;with cte as (
    select 'Furniture' as Category, 'Table'  as Name, 'Wood table' as Descr, 299.99 as Price union all
    select 'Furniture' as Category, 'Chair'  as Name, 'High back'  as Descr,  99.99 as Price union all
    select 'Furniture' as Category, 'Desk'   as Name, 'Long Desk'  as Descr, 199.99 as Price union all  
    select 'IT'        as Category, 'Laptop' as Name, 'HP Laptop'  as Descr, 999.99 as Price union all
    select 'IT'        as Category, 'Mouse'  as Name, 'Optical'    as Descr,   9.99 as Price
    select case when rn = 1 then Category 
                when g2 = 1 then 'Total ('+convert(varchar,cnt)+')'
                else null end as Category
         , Name, Descr, tot_price
      from (select Category, Name, Descr, SUM(Price) tot_price, count(*) as cnt
                 , grouping(Category) g1, grouping(Name) g2, grouping(Descr) g3
                 , row_number()over(partition by Category order by ISNULL(Name,CHAR(255)), Descr) rn
              from cte
             group by Category, Name, Descr WITH ROLLUP
            having grouping(Category) = 1 or grouping(Name) = 1 or grouping(Descr) = 0)v
     where v.Category is not null
     order by v.Category, rn

    Open in new window


    Assisted Solution

    It seems like this will do the trick (See attached screenshot):
    SELECT     Category, [My Group], Name, [Desc], [Purchase Price]
    FROM         (SELECT     0 AS MySort, Category, '' AS [My Group], Name, [Desc], [Purchase Price]
                           FROM          test
                           SELECT     1 AS MySort, Category, 'Total (' + CAST(COUNT(*) AS varchar(10)) + ')' AS [My Group], '' AS Expr2, '' AS Expr3, SUM([Purchase Price]) 
                                                 AS Expr4
                           FROM         test AS test_1
                           GROUP BY Category
                           SELECT     - 1 AS MySort, Category, Category AS [My Group], '' AS Expr2, '' AS Expr3, NULL AS Expr4
                           FROM         test AS test_1
                           GROUP BY Category) AS Temp
    ORDER BY Category, MySort

    Open in new window


    Author Closing Comment

    Thank you!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    Via a live example, show how to shrink a transaction log file down to a reasonable size.
    Via a live example, show how to setup several different housekeeping processes for a SQL Server.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now