1

I need help converting this MS Access query to SQL Server 2012:

TRANSFORM SUM(TTQTY * qty) AS quantity SELECT matlprodcode AS product_code, cvend_name AS supplier, CITMCD AS material, matldesc AS description, MIN(unit_cost) AS unit_cost FROM acs_Budget WHERE matlprodcode NOT LIKE ';I8*' AND matlprodcode NOT LIKE 'I1' GROUP BY matlprodcode, cvend_name, CITMCD, matldesc PIVOT budgetmonth; 

I have tried converting it myself here:

SELECT matlprodcode AS product_code, cvend_name AS supplier, CITMCD AS material, matldesc AS 'description', MIN(unit_cost) AS unit_cost FROM ( SELECT TTQTY, qty, matlprodcode, cvend_name, CITMCD, matldesc, unit_cost, budgetmonth FROM acs_Budget WHERE matlprodcode NOT LIKE ';I8*' AND matlprodcode NOT LIKE 'I1' ) AS T PIVOT ( SUM(TTQTY * qty) ) P 

Is this correct? Any suggestions? Or am I way off?

1 Answer 1

1

Well I figured it out myself. I am not sure if this will help others but here is the final query that will work in SQL Server. I converted this from MS Access:

SELECT pt.* FROM (SELECT SUM(TTQTY * qty) AS quantity, matlprodcode AS product_code, cvend_name AS supplier, CITMCD as material, matldesc AS description, MIN(unit_cost) AS unit_cost, budgetmonth FROM acs_Budget WHERE matlprodcode NOT LIKE ';I8*' AND matlprodcode NOT LIKE 'I1' GROUP BY matlprodcode, cvend_name, CITMCD, matldesc, budgetmonth) AS src PIVOT (MIN(quantity) FOR src.budgetmonth IN ([4/1/2015], [5/1/2015], [6/1/2015], [7/1/2015], [8/1/2015], [9/1/2015], [10/1/2015], [11/1/2015], [12/1/2015], [1/1/2016], [2/1/2016], [3/1/2016])) AS pt; 

Some things that helped me:

  • First I selected everything that I wanted to see. Including what MS Access was transforming and pivoting.
  • Then I found out what each column MS Access was pivoting, in this case the quantity.
  • So I did a MIN of the quantity because the PIVOT needs an aggregated function.
  • After the aggregated function you need the actual PIVOT that MS Access is using which goes after the FOR.
  • Then I did something similar to a SELECT DISTINCT budgetmonth FROM acs_Budget to get each item I could pivot by and that's where the IN came in.

Not sure if I explained very well but this is how I got mine to work!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.