How do i achieve this in SQL query?

create table CityList (CityCode varchar(5),City varchar(20),
                       BillNo INT, Name varchar(25),
                       BillDate date,
                       Amount decimal(10,2),
                       Total decimal(10,2))

INSERT INTO CityList VALUES ('1','AAA',119,'EDWIN','2015-01-18',100,500)
INSERT INTO CityList VALUES ('2','AAA',119,'EDWIN','2015-01-18',400,0)
INSERT INTO CityList VALUES ('3','AAA',120,'EDWIN','2015-01-18',800,800)
                             
INSERT INTO CityList VALUES ('8','AAB',118,'JOHN','2015-01-18',100,100)
                             
INSERT INTO CityList VALUES ('18','AAD',120,'ANDREW','2015-01-18',100,300)
INSERT INTO CityList VALUES ('19','AAD',120,'ANDREW','2015-01-18',100,0)
INSERT INTO CityList VALUES ('4','AAD',120,'ANDREW','2015-01-18',100,0)
INSERT INTO CityList VALUES ('77','AAD',128,'ANDREW','2015-01-18',1000,1000)

INSERT INTO CityList VALUES ('28','AAY',123,'ANDREW','2015-01-18',150,150)

Query Output:

Query output
I need a output in the following format

Required Output
I need a subtotal group by city and name. I am not considering citycode,Billno or billdate.
And column total has the sum of amount  total.
I need grant total in the last row. How do i achieve this in SQL query?
Varshini SAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
E.g.
 
DECLARE @CityList TABLE
    (
      CityCode VARCHAR(5) ,
      City VARCHAR(20) ,
      BillNo INT ,
      Name VARCHAR(25) ,
      BillDate DATE ,
      Amount DECIMAL(10, 2) ,
      Total DECIMAL(10, 2)
    );

INSERT  INTO @CityList
VALUES  ( '1', 'AAA', 119, 'EDWIN', '2015-01-18', 100, 500 ) ,
        ( '2', 'AAA', 119, 'EDWIN', '2015-01-18', 400, 0 ) ,
        ( '3', 'AAA', 120, 'EDWIN', '2015-01-18', 800, 800 )    ,
        ( '8', 'AAB', 118, 'JOHN', '2015-01-18', 100, 100 )    ,
        ( '18', 'AAD', 120, 'ANDREW', '2015-01-18', 100, 300 ) ,
        ( '19', 'AAD', 120, 'ANDREW', '2015-01-18', 100, 0 ) ,
        ( '4', 'AAD', 120, 'ANDREW', '2015-01-18', 100, 0 ) ,
        ( '77', 'AAD', 128, 'ANDREW', '2015-01-18', 1000, 1000 )  ,
        ( '28', 'AAY', 123, 'ANDREW', '2015-01-18', 150, 150 );

WITH    Data
          AS ( SELECT   CL.CityCode ,
                        CL.City ,
                        CL.BillNo ,
                        CL.Name ,
                        CL.BillDate ,
                        CL.Amount ,
                        CL.Total
               FROM     @CityList CL
               UNION
               SELECT   CL.CityCode ,
                        CL.City ,
                        NULL AS BillNo ,
                        CL.Name ,
                        NULL AS BillDate ,
                        SUM(CL.Amount) ,
                        SUM(CL.Total)
               FROM     @CityList CL
               GROUP BY CL.CityCode ,
                        CL.City ,
                        CL.Name
                        WITH ROLLUP
             )
    SELECT  D.CityCode ,
            D.City ,
            D.BillNo ,
            D.Name ,
            D.BillDate ,
            D.Amount ,
            D.Total
    FROM    Data D
    WHERE   ( NOT D.CityCode IS NULL
              AND NOT D.Name IS NULL
            )
            OR ( D.CityCode IS NULL
                 AND D.Name IS NULL
               )
    ORDER BY CASE WHEN D.City IS NULL THEN 'zzzzzzzzzzzzzzzzzzzz'
                  ELSE D.City
             END ,            
            D.Name ,
            CASE WHEN D.BillNo IS NULL THEN 999999999999999999
                 ELSE D.BillNo
            END;

Open in new window

Varshini SAuthor Commented:
Hi Ste5an,
I got the following output when i execute your query. Its not giving subtotal

result
ste5anSenior DeveloperCommented:
Cause your data is inconsistent. CityCode and City must imho correlate. Otherwise simply don't GROUP BY CityCode..

DECLARE @CityList TABLE
    (
      CityCode VARCHAR(5) ,
      City VARCHAR(20) ,
      BillNo INT ,
      Name VARCHAR(25) ,
      BillDate DATE ,
      Amount DECIMAL(10, 2) ,
      Total DECIMAL(10, 2)
    );

INSERT  INTO @CityList
VALUES  ( '1', 'AAA', 119, 'EDWIN', '2015-01-18', 100, 500 ) ,
        ( '2', 'AAA', 119, 'EDWIN', '2015-01-18', 400, 0 ) ,
        ( '3', 'AAA', 120, 'EDWIN', '2015-01-18', 800, 800 )    ,
        ( '8', 'AAB', 118, 'JOHN', '2015-01-18', 100, 100 )    ,
        ( '18', 'AAD', 120, 'ANDREW', '2015-01-18', 100, 300 ) ,
        ( '19', 'AAD', 120, 'ANDREW', '2015-01-18', 100, 0 ) ,
        ( '4', 'AAD', 120, 'ANDREW', '2015-01-18', 100, 0 ) ,
        ( '77', 'AAD', 128, 'ANDREW', '2015-01-18', 1000, 1000 )  ,
        ( '28', 'AAY', 123, 'ANDREW', '2015-01-18', 150, 150 );

WITH    Data
          AS ( SELECT   CL.CityCode ,
                        CL.City ,
                        CL.BillNo ,
                        CL.Name ,
                        CL.BillDate ,
                        CL.Amount ,
                        CL.Total
               FROM     @CityList CL
               UNION
               SELECT   NULL AS CityCode ,
                        CL.City ,
                        NULL AS BillNo ,
                        CL.Name ,
                        NULL AS BillDate ,
                        SUM(CL.Amount) ,
                        SUM(CL.Total)
               FROM     @CityList CL
               GROUP BY CL.City ,
                        CL.Name
                        WITH ROLLUP
             )
    SELECT  D.CityCode ,
            D.City ,
            D.BillNo ,
            D.Name ,
            D.BillDate ,
            D.Amount ,
            D.Total
    FROM    Data D
    WHERE   ( NOT D.City IS NULL
              AND NOT D.Name IS NULL
            )
            OR ( D.City IS NULL
                 AND D.Name IS NULL
               )
    ORDER BY CASE WHEN D.City IS NULL THEN 'zzzzzzzzzzzzzzzzzzzz'
                  ELSE D.City
             END ,
            D.Name ,
            CASE WHEN D.BillNo IS NULL THEN 999999999999999999
                 ELSE D.BillNo
            END;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.