Query with MS SQL

Dear Expert,

How can query with MS SQL like this?
6-27-2015-4-49-25-PM.jpgquery.xlsx
arkam chouAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:
This solution has been tested:
;with e
as
(
Select d.name, d.price, c.channelname
from TblDataEntry as d inner join TblChannel as c
on d.channelid = c.channelid
)
Select * from e
pivot(sum(price)
for channelname in (a,b,c)) as pvt;

Open in new window

returns:
name         a               b             c
Apple      103.00      107.00      210.00
Nokia      206.00      205.00      105.00

Do you want the other to queries  as well?

Mike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperCommented:
if so, they are:
Select c.channelname, d.name, d.price
from TblDataEntry as d inner join TblChannel as c
on d.channelid = c.channelid

Select  d.name
  , case 
    when c.channelname = 'a' then d.price
	else null end As a
  , case 
    when c.channelname = 'b' then d.price
	else null end As b
  , case 
    when c.channelname = 'c' then d.price
	else null end As c
from TblDataEntry as d inner join TblChannel as c
on d.channelid = c.channelid

-- to replace NULL with blank space, use:
Select  d.name
  , case 
    when c.channelname = 'a' then cast(d.price as nvarchar(10))
	else '' end As a
  , case 
    when c.channelname = 'b' then cast(d.price as nvarchar(10))
	else '' end As b
  , case 
    when c.channelname = 'c' then cast(d.price as nvarchar(10))
	else '' end As c
from #d as d inner join #c as c
on d.channelid = c.channelid

Open in new window

0
arkam chouAuthor Commented:
yes it work. I chose your first solution but It have problem when I try to add more data in "tblchannel" with space "new channel". How to solve it?
6-28-2015-8-34-44-AM.jpg
0
Mike EghtebasDatabase and Application DeveloperCommented:
Please change line 10 in the original solution to:
for channelname in (a,b,c, [New Channel])) as pvt

I am guessing what the error is because you are not telling me what the error is.
0
arkam chouAuthor Commented:
Yes, it work. Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.