Link to home
Start Free TrialLog in
Avatar of diecasthft01
diecasthft01

asked on

Help using a join in a SQL Query pivot query

Good morning Everyone, I was looking for some expert help on a SQL Query that Im working on. The query I have works as written but now I need to add some data from a different table and wanted to do a join....I cant seem to figure this out as my existing query has a PIVOT in it, and it doesnt seem clear to me where or if I can add another table. Any help would be greatly appreciated!!


The below gets the info I need to export to a spreadsheet in a pivot, and it is giving me exactly what I want:

SELECT * FROM (

  SELECT

   commit_doc_number,

   disb_amount,

   CONCAT(MONTH(post_date), YEAR(post_date)) AS Y

   

  FROM MCA.P2P_TABLE1

) PROBEResults

   

PIVOT (

  SUM([disb_amount])

  FOR [Y]

  IN (

    [102022],

   [112022],

   [122022],

   [12023],

   [22023],

   [32023],

   [42023],

   [52023],

   [62023],

   [72023],

   [82023],

   [92023]

  )

AS PivotTable 

order by [commit_doc_number] asc


But now I need some fields added from another table, primarily the budget_miprs_sent table.fy and budget_miprs_sent.mipr_type for starters. The field commit_doc_number appears in each table.


Avatar of diecasthft01
diecasthft01

ASKER

Actually, now that I think about it...maybe I dont need a left join like I was thinking...the only thing I was worried about was having commit_doc_numbers in the P2P_TABLE that doesnt appear in the BUDGET_MIPRS_SENT table

Avatar of slightwv (䄆 Netminder)
Without sample data and expected results it is sort of a guess.

So, my guess is:
Can you not join inside the inner query before the pivot in the outer query?
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial