Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


Hello Frndz,
I have a table named product as shown below:
product_id product_name
1 AAA
1 BBB
1 CCC
2 PPP
2 QQQ
2 RRR

Now my output should be:
product_id product_name_1 product_name_2 product_name_3
1 AAA BBB CCC
2 PPP QQQ RRR

Answers were Sorted based on User's Feedback



Hello Frndz, I have a table named product as shown below: product_id product_name 1 AAA 1 BBB ..

Answer / tdguy

Hi,
Below query can be used assuming that there would not be
duplicates of product_names in the parent table and only
three product_names would be the output required.

SEL PRODUCT_ID,MIN(PRODUCT_NAME_1) AS PROD1,MIN
(PRODUCT_NAME_2) AS PROD2,MIN(PRODUCT_NAME_3) AS PROD3
FROM
(SEL PRODUCT_ID,PRODUCT_NAME,
ROW_NUMBER() OVER (PARTITION BY PRODUCT_ID ORDER BY
PRODUCT_NAME ASC) AS PRODRANK,
CASE WHEN PRODRANK=1
THEN PRODUCT_NAME END AS PRODUCT_NAME_1,
CASE WHEN PRODRANK=2
THEN PRODUCT_NAME END AS PRODUCT_NAME_2,
CASE WHEN PRODRANK=3
THEN PRODUCT_NAME END AS PRODUCT_NAME_3
FROM PRODUCT) A
GROUP BY 1;


If the above case is numeric, SUM should be used instead of
MIN.

Is This Answer Correct ?    7 Yes 0 No

Hello Frndz, I have a table named product as shown below: product_id product_name 1 AAA 1 BBB ..

Answer / yuvaevergreen

Hi,
Since the no of output columns is a variable, I think we
may have to write a SP or macro to get the desired output.

Is This Answer Correct ?    1 Yes 0 No

Hello Frndz, I have a table named product as shown below: product_id product_name 1 AAA 1 BBB ..

Answer / vaishnavi a

Thanks for ur response, i just gave a sample table with few
records, but in a real time scenario we have a table with
duplicates & there can be many products under a particular
prod_id.It is not that there should be only 3 products
under a prod_id , it could be even 50. wat could be an
optimised way of writing a query for this scenario??

Is This Answer Correct ?    0 Yes 0 No

Hello Frndz, I have a table named product as shown below: product_id product_name 1 AAA 1 BBB ..

Answer / vishvajeet mistry

We can also use Full outer join in this case.

sel
t1.pid,
t1.pn as pn1,
t2.pn as pn2,
t3.pn as pn3
from
(sel pid,pn
from prod
qualify rank() over (partition by pid order by pn)=1)t1
Full outer join
(sel pid,pn
from prod
qualify rank() over (partition by pid order by pn)=2)t2
On
t1.pid=t2.pid
Full outer join
(
sel pid,pn
from prod
qualify rank() over (partition by pid order by pn)=3)t3
on
t1.pid=t3.pid
order by 1 ;


Hope it helps :)

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Teradata Interview Questions

how to improve the query perfoemance in teradata.with example?and how explain this in interview?please forward answer

2 Answers   IBM,


What are the various indexes in teradata? How to use them?

0 Answers  


What are the components provided on node?

0 Answers  


can we have an unconnected lkp to lookup a DB2 record against a Teradata record?

3 Answers  


What is the difference between teradata and basic rdbms?

0 Answers  


Briefly explain each of the following terms related to relational database management system (rdbms) – database, tables, columns, row, primary key and foreign key.

0 Answers  


If the script is aborted. why it is aborted and how can you identify ?

1 Answers  


How can we load single row of data into teradata database. please give me the process.

3 Answers  


Explain vproc in teradata?

0 Answers  


Explain the new features of teradata?

0 Answers  


Can we collect statistics on multiple columns?

0 Answers  


What is the meaning of Caching in Teradata?

0 Answers  


Categories