Solved

SQL QUERY HELP

Posted on 2014-11-26
1
119 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
1 Comment
 
LVL 46

Accepted Solution

by:
Vitor Montalvão earned 500 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 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

21 Experts available now in Live!

Get 1:1 Help Now