Question

enter image description here

How would the SQL statement look like to return the bottom result from the upper table?

The last letter from the key should be removed. It stands for the language. EXP column should be split into 5 columns with the language prefix and the right value.

I'm weak at writing more or less difficult SQL statements so any help would be appreciated!

Was it helpful?

Solution

The Microsoft Access equivalent of a PIVOT in SQL Server is known as a CROSSTAB. The following query will work for Microsoft Access 2010.

TRANSFORM First(table1.Exp) AS FirstOfEXP
   SELECT Left([KEY],Len([KEY])-2) AS [XKEY]
     FROM table1
 GROUP BY Left([KEY],Len([KEY])-2)
 PIVOT Right([KEY],1);

Access will throw a circular field reference error if you try to name the row heading with KEY since that is also the name of the original table field that you are deriving it from. If you do not want XKEY as the field name, then you would need to break apart the above query into two separate queries as shown below:

qsel_table1:

SELECT Left([KEY],Len([KEY])-2) AS XKEY , Right([KEY],1) AS [Language] , Table1.Exp FROM Table1 ORDER BY Left([KEY],Len([KEY])-2), Right([KEY],1);

qsel_table1_Crosstab:

TRANSFORM First(qsel_table1.Exp) AS FirstOfEXP
   SELECT qsel_table1.XKEY AS [KEY]
     FROM qsel_table1
  GROUP BY qsel_table1.XKEY
  PIVOT qsel_table1.Language;

In order to always output all language columns regardless of whether there is a value or not, you need to spike of those values into a separate table. That table will then supply the row and column values for the crosstab and the original table will supply the value expression. Using the two query solution above we would instead need to do the following:

table2:

This is a new table with a BASE_KEY TEXT*255 column and a LANG TEXT*1 column. Together these two columns will define the primary key. Populate this table with the following rows:

"AbstractItemNumberReportController.SelectPositionen", "D" "AbstractItemNumberReportController.SelectPositionen", "E" "AbstractItemNumberReportController.SelectPositionen", "F" "AbstractItemNumberReportController.SelectPositionen", "I" "AbstractItemNumberReportController.SelectPositionen", "X"

qsel_table1:

This query remains unchanged.

qsel_table1_crosstab:

The new table2 is added to this query with an outer join with the original table1. The outer join will allow all rows to be returned from table2 regardless of whether there is a matching row in the table1. Table2 now supplies the values for the row and column headings.

TRANSFORM First(qsel_table1.Exp) AS FirstOfEXP
   SELECT Table2.Base_KEY AS [KEY]
     FROM Table2 LEFT JOIN qsel_table1 ON (Table2.BASE_KEY = qsel_table1.XKEY) 
      AND (Table2.LANG = qsel_table1.Language)
  GROUP BY Table2.Base_KEY
  PIVOT Table2.LANG;

OTHER TIPS

Try something like this:

select *
from 
(
    select 'abcd' as [key], right([key], 1) as id, expression
    from table1
) x
pivot
(
    max(expression)
    for id in ([D], [E])
) p

Demo Fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top