?
Solved

SQL QUERY HELP

Posted on 2014-11-26
1
Medium Priority
?
125 Views
Last Modified: 2014-11-26
I have the following table.

CREATE TABLE Table1
	([City] varchar(9), [Title] varchar(6), [Seller] varchar(25), [ActiveStatus] varchar(1))

	
INSERT INTO Table1
	([City], [Title], [Seller],[ActiveStatus])
VALUES 	('Chennai', 'Book1', 'AAA','Y')

INSERT INTO Table1([City], [Title], [Seller],[ActiveStatus])
VALUES 	('Chennai', 'Book2', 'BBB','Y')

INSERT INTO Table1
	([City], [Title], [Seller],[ActiveStatus])
VALUES ('Chennai', 'Book3', 'A1A','Y')

INSERT INTO Table1
	([City], [Title], [Seller],[ActiveStatus])
VALUES ('Mumbai', 'Book1', 'SAS','Y')

INSERT INTO Table1
	([City], [Title], [Seller],[ActiveStatus])
VALUES ('Mumbai', 'Book2', 'SAY','Y')

INSERT INTO Table1
	([City], [Title], [Seller],[ActiveStatus])
VALUES ('Mumbai', 'Book3', 'MBM','Y')

INSERT INTO Table1
	([City], [Title], [Seller],[ActiveStatus])
VALUES ('Bangalore', 'Book3','ADF','Y')

INSERT INTO Table1
	([City], [Title], [Seller],[ActiveStatus])
VALUES ('Bangalore', 'Book2', 'ASD','Y')

INSERT INTO Table1
	([City], [Title], [Seller],[ActiveStatus])
VALUES ('Bangalore', 'Book1', 'DWE','Y')

INSERT INTO Table1
	([City], [Title], [Seller],[ActiveStatus])
VALUES ('London', 'Book2', 'LLA','Y')

INSERT INTO Table1
	([City], [Title], [Seller],[ActiveStatus])
VALUES ('London', 'Book1', 'SLA','Y')

INSERT INTO Table1
	([City], [Title], [Seller],[ActiveStatus])
VALUES ('London', 'Book3', 'SSS','N')

Open in new window


I would like to have the following output

Chennai     [Book1: AAA]  [Book2:BBB]  [Book3: A1A]
 Mumbai     [Book1: SAS]  [Book2:SAY]  [Book3: MBM]
 Bangalore  [Book1: DWE]  [Book2:ASD]  [Book3: ADF]
 London      [Book1: SLA]  [Book2:LLA]  ]

I used the below query for achieving this output:

SELECT City, 
   '[Book1: ' + Book1 + '] ' + 
   '[Book2: ' + Book2 + '] ' + 
   '[Book3: ' + Book3 + '] ' AS BookDetails
FROM (
   SELECT City, 
      [Book1] as Book1,
      [Book2] as Book2,
      [Book3] as Book3
   FROM (SELECT City, Title, Seller FROM Table1 WHERE ActiveStatus='Y') t1
      PIVOT (MIN(Seller) FOR Title IN ([Book1], [Book2], [Book3])) as pvt
   ) a
ORDER BY City

Open in new window


i got the following output when i execute this query


Bangalore      [Book1: DWE] [Book2: ASD] [Book3: ADF]
Chennai           [Book1: AAA] [Book2: BBB] [Book3: A1A]
London          NULL
Mumbai          [Book1: SAS] [Book2: SAY] [Book3: MBM]

For the City London the activestatus for Book1 and Book2 is 'Y'
Then Why it is showing NULL ?  How do i show the Book1 and Book2 for London ?
0
Comment
Question by:Varshini S
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 51

Accepted Solution

by:
Vitor Montalvão earned 2000 total points
ID: 40466706
It's because you are concatenating with NULL value. Just test if is NULL or not:
SELECT City, 
   ISNULL('[Book1: ' + Book1 + '] ','') + 
   ISNULL('[Book2: ' + Book2 + '] ','') + 
   ISNULL('[Book3: ' + Book3 + '] ','') AS BookDetails
FROM (
   SELECT City, 
      [Book1] as Book1,
      [Book2] as Book2,
      [Book3] as Book3
   FROM (SELECT City, Title, Seller FROM Table1 WHERE ActiveStatus='Y') t1
      PIVOT (MIN(Seller) FOR Title IN ([Book1], [Book2], [Book3])) as pvt
   ) a
ORDER BY City

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

770 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