Solved

SQL QUERY HELP

Posted on 2014-11-26
1
124 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 50

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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