Solved

SQL QUERY HELP

Posted on 2014-11-26
1
115 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 45

Accepted Solution

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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