Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 June SQL Server Interview: Advance SQL Query – Don’t use pivot and Do Row aggregation into Column

SQL Server Interview: Advance SQL Query – Don’t use pivot and Do Row aggregation into Column

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing an example of SQL Query which is doing Row aggregation into Column without using PIVOT in SQL Server.

If you are preparing for the interview of database developer, you should practice this kind of query which may ask by an interviewer.

Just execute the below demonstration, and try it yourself.

Create a table with sample data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
CREATE TABLE tbl_NoPivot
(
Itemcode char(1)
,ItemStatus varchar(10)
,ItemQty int
)
GO
 
INSERT INTO tbl_NoPivot VALUES
('A','New',100),('A','Old',200),('A','Current',300)
,('A','Old',250),('A','Hold',300),('A','Old',50)
,('B','Hold',320),('B','Current',450),('B','Old',150)
,('B','Current',360),('B','New',320),('B','New',220)
GO

Solution without using a PIVOT:

1
2
3
4
5
6
7
8
9
10
11
12
13
SELECT
Itemcode
,sum(case when ItemStatus='Current' then TotalQty end) as CurrentItemStatus
,sum(case when ItemStatus='Old' then TotalQty end) as OldItemStatus
,sum(case when ItemStatus='New' then TotalQty end) as NewItemStatus
,sum(case when ItemStatus='Hold' then TotalQty end) as HoldItemStatus
FROM
(
SELECT Itemcode, ItemStatus,sum(ItemQty) as TotalQty
FROM tbl_NoPivot
GROUP BY Itemcode, ItemStatus
)AS T
GROUP BY Itemcode

The solution with using a PIVOT:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SELECT
Itemcode
,[Current] AS CurrentItemStatus
,Old AS OldItemStatus
,New AS NewItemStatus
,Hold AS HoldItemStatus
FROM
(
SELECT Itemcode,ItemQty,ItemStatus
FROM tbl_NoPivot
) T
PIVOT
(
SUM(ItemQty)
for ItemStatus in (New,Old,[Current],Hold)
) PIV;

Jun 3, 2017Anvesh Patel
SQL Server Interview: Advance SQL Query - Find Permutations and Combinations of a String ColumnGreenplum: Script to find all Distribution Keys of Database tables
Comments: 1
  1. Quadri
    August 10, 2018 at 9:20 am

    you can do it this way too

    SELECT Itemcode, sum(case when ItemStatus=’Current’ then itemqty end) as CurrentItemStatus
    ,sum(case when ItemStatus=’Old’ then itemqty end) as OldItemStatus
    ,sum(case when ItemStatus=’New’ then itemqty end) as NewItemStatus
    ,sum(case when ItemStatus=’Hold’ then itemqty end) as HoldItemStatus
    FROM tbl_NoPivot
    GROUP BY Itemcode

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

June 3, 2017 1 Comment SQL PuzzleAnvesh Patel, Complex SQL Query, database, database research and development, dbrnd, Pivot, SQL Query, SQL Server, SQL Server Administrator, SQL Server Error, SQL Server Interview, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, SQL Server Tips and Tricks, T-SQL Developer, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....