July 28, 2018 at 5:42 am
andycadley - Wednesday, July 25, 2018 12:34 PMDDL will definitely help people to help you. You might want to investigate the FORMAT function which can probably help you get where you're trying to go, eg:
Select
Format(3.456,'0.00'),
Format(0.2,'0.00'),
Format(0.2,'#.00')
Carefull now... The FORMAT function takes 44 times longer to execute than CONVERT does.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 28, 2018 at 5:46 am
GG_BI_GG - Tuesday, July 24, 2018 12:50 AMSELECT 'Per Category' AS Category,
[1], [2], [3], [4], [5], [6], [7], [8]
FROM
(SELECT CategoryID, AVG(UnitPrice) as UnitPrice
FROM Products
GROUP BY CategoryID) AS SourceTable
PIVOT
(
AVG(UnitPrice) FOR CategoryID IN ([1], [2], [3], [4], [5], [6], [7], [8] )
) AS PivotTable;The results need to look like this:
Per Category 1 2 3 4 5 6 7 8
Average Unit Price 37.98 23.06 25.16 28.73 20.25 54.01 32.37 20.68
Getting back to this original question, use the CONVERT function to convert the output columns in your SELECT list to DECIMAL with 2 decimal places. DO understand what rounding will do to you.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 1, 2018 at 11:52 pm
Jeff Moden - Saturday, July 28, 2018 5:46 AMGG_BI_GG - Tuesday, July 24, 2018 12:50 AMSELECT 'Per Category' AS Category,
[1], [2], [3], [4], [5], [6], [7], [8]
FROM
(SELECT CategoryID, AVG(UnitPrice) as UnitPrice
FROM Products
GROUP BY CategoryID) AS SourceTable
PIVOT
(
AVG(UnitPrice) FOR CategoryID IN ([1], [2], [3], [4], [5], [6], [7], [8] )
) AS PivotTable;The results need to look like this:
Per Category 1 2 3 4 5 6 7 8
Average Unit Price 37.98 23.06 25.16 28.73 20.25 54.01 32.37 20.68Getting back to this original question, use the CONVERT function to convert the output columns in your SELECT list to DECIMAL with 2 decimal places. DO understand what rounding will do to you.
Yes back to the original question, I'd like to see what results the user is getting. Maybe he has his numbers stored as integers. An average of integers will return an integer. Maybe its just rounding.
----------------------------------------------------
June 22, 2020 at 11:59 am
DataCamp accepts it like this however yours is correct too:
SELECT 'Average Unit Price' AS [Per Category],
[1], [2], [3], [4], [5], [6], [7], [8]
FROM
(SELECT CategoryID, AVG(UnitPrice) as UnitPrice
FROM Products
GROUP BY CategoryID) AS SourceTable
PIVOT
(
AVG(PIVOT.UnitPrice) FOR PIVOT.CategoryID IN ([1], [2], [3], [4], [5], [6], [7], [8] )
) AS PivotTable;
June 22, 2020 at 2:07 pm
saravanatn - Tuesday, July 24, 2018 2:02 AMGG_BI_GG - Tuesday, July 24, 2018 12:58 AMProduct Table look like in the Picture.I think Erikur mentioned clearly what needs to be done .Kindly post at least minimum information to answer your question.
Have a look at the link in my signature on how to respond Eirikur's request. 🙂
Not seeing a link in your signature line anymore, Thom.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 22, 2020 at 2:28 pm
Thom A wrote:saravanatn - Tuesday, July 24, 2018 2:02 AMGG_BI_GG - Tuesday, July 24, 2018 12:58 AMProduct Table look like in the Picture.I think Erikur mentioned clearly what needs to be done .Kindly post at least minimum information to answer your question.
Have a look at the link in my signature on how to respond Eirikur's request. 🙂
Not seeing a link in your signature line anymore, Thom.
Pretty sure the "great" forum upgrade borked it 😉
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
June 22, 2020 at 2:48 pm
Jeff Moden wrote:Thom A wrote:saravanatn - Tuesday, July 24, 2018 2:02 AMGG_BI_GG - Tuesday, July 24, 2018 12:58 AMProduct Table look like in the Picture.I think Erikur mentioned clearly what needs to be done .Kindly post at least minimum information to answer your question.
Have a look at the link in my signature on how to respond Eirikur's request. 🙂
Not seeing a link in your signature line anymore, Thom.
Pretty sure the "great" forum upgrade borked it 😉
The link was there in Tom's response, before the forum's upgrademic 😉
😎
June 22, 2020 at 3:30 pm
I sure am happy to see that I'm not the only one that thinks the contributors that seriously helped make this forum great have taken it in the socks in one way or another on every "upgrade" they've ever made.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 16 through 22 (of 22 total)
You must be logged in to reply to this topic. Login to reply