July 3, 2022 at 7:55 am
July 3, 2022 at 1:35 pm
Hello Community,
Sorry for supplying so much data in the sample table - I have noticed it has slowed down the page when I scroll.
Anway, I was wondering if I could get some help with this question please.
July 3, 2022 at 3:30 pm
I recommend starting here: https://www.sqlservercentral.com/articles/cross-tabs-and-pivots-part-1-converting-rows-to-columns-1
Then follow up with the second article here: https://www.sqlservercentral.com/articles/cross-tabs-and-pivots-part-2-dynamic-cross-tabs
Instead of trying to use the PIVOT operator you can use a simple GROUP BY with conditional aggregates to achieve the desired results.
--==== Sample Only
Select MontaguOwner
, [Action - 1. Analysing] = max(Case When CurrentOpportunityStatus = 'Action - 1. Analysing' Then LastDateStatusChanged End)
From temptable3
Group By
MontaguOwner
You can add the additional conditions as needed.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
July 3, 2022 at 5:21 pm
Hi Jeff,
I think I'll be able to build on what you've provided for the other conditions.
Thanks
July 3, 2022 at 5:59 pm
First, really nice job on providing "Readily Consumable Data" AND it actually works without error! 😀
With respect to the output you posted, are you looking to pivot ONLY those rows where the CurrentOpportunityStatus column starts with the word "Action"?
Also, do you need for other columns to be included? For example, should the OpportunityName name be included in the output?
--Jeff Moden
Change is inevitable... Change for the better is not.
July 3, 2022 at 7:26 pm
Hi Jeff,
Using the same test data I provided I wonder if you could help me build on your code to achieve an additional outcome.
July 3, 2022 at 8:16 pm
First, really nice job on providing "Readily Consumable Data" AND it actually works without error!
My pleasure
If I'm asking the community for help, I think it's only fair to make it as easy for you guys/girls to provide the help I need.
July 3, 2022 at 10:21 pm
Hi Jeff,
Using the same test data I provided I wonder if you could help me build on your code to achieve an additional outcome.
So, on the test data, I simply added the following code to filter out a field to help someone (if not you) help me:
SELECT
*
FROM dbo.opportunitiespresentation
WHERE opportunitiespresentation.MontaguOwner = 'Christian Wolf'The provided the following table:
I would like to build on your code to achieve the following:
If you could just kick me off, I'm sure I'll be able to take it from there.
Thanks
Seems like we should get the first issue solved before doing anything else. In order for me to answer that, I need you to answer my previous questions. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
July 3, 2022 at 10:40 pm
First, really nice job on providing "Readily Consumable Data" AND it actually works without error! 😀
Thanks
July 4, 2022 at 1:07 am
Here's the query to cover your first request.
SELECT MontaguOwner = IIF(MontaguOwner>'',MontaguOwner,'*** None Given ***')
,OpportunityName
,[Action - 1. Analysing] = MAX(IIF(CurrentOpportunityStatus LIKE 'Action - 1.%',CONVERT(CHAR(10),LastDateStatusChanged,101),''))
,[Action - 2. Trying to meet] = MAX(IIF(CurrentOpportunityStatus LIKE 'Action - 2.%',CONVERT(CHAR(10),LastDateStatusChanged,101),''))
,[Action - 3. Date agreed] = MAX(IIF(CurrentOpportunityStatus LIKE 'Action - 3.%',CONVERT(CHAR(10),LastDateStatusChanged,101),''))
,[Action - 4. Post meeting] = MAX(IIF(CurrentOpportunityStatus LIKE 'Action - 4.%',CONVERT(CHAR(10),LastDateStatusChanged,101),''))
,[Action - 5. Chopped] = MAX(IIF(CurrentOpportunityStatus LIKE 'Action - 5.%',CONVERT(CHAR(10),LastDateStatusChanged,101),''))
FROM dbo.temptable3
GROUP BY MontaguOwner,OpportunityName
ORDER BY MontaguOwner,OpportunityName
;
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply