Click here to Skip to main content
15,913,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to get some report in sql like this.

i have three tables 1.Location,2.Brands,3.Sales
Location(MasterTable)
Name Description
AAA Place AAA
BBB Place BBB
CCC Place CCC
Brands(MasterTable)
Name Description
XYZ XYZ Brand
PQR PQR Brand
MNO MNO Brand
Sales
SLID Sdate BrandName Location Qty Amount
1 9/21/2015 XYZ AAA 2 100
2 9/21/2015 XYZ CCC 3 100
3 9/21/2015 MNO BBB 3 100

there is any option to get report like this , i got no idea to try this way.
HTML
Brand	AAA	BBB	CCC
XYZ	2		3
MNO		3	


please help me to solve this issue.
Posted

1 solution

 
Share this answer
 
Comments
tastini 21-Sep-15 5:52am    
please check what i try to write as per your suggestion i try the pivot table. this is what i try now. if i give the location name directly its returns report, but my case lacation table is master so it will change,i cannot hard type the names. please check the query what i did the wrong here in this.
Select BrandName,Location
From
(
Select BrandName,Location,SLID From Sales) as BaseTable
Pivot
(
Count(SLID) For Location in (Select Name from MasterLocation)
) as PivotTable
Herman<T>.Instance 21-Sep-15 6:07am    
In my article you will read that you have to create a string of fieldnames to use with the PIVOT command. It cannot be done in a select statement. That returns data, not columnnames (that is stored in mastertables and not delimited with a , when selected. Therefore the middlequery in my article.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900