Note that there are some explanatory texts on larger screens.

plurals
  1. POCrosstab Queries: using fixed values to create additional columns
    primarykey
    data
    text
    <p>Has anyone read the MSDN page on using the TRANSFORM statement to create crosstab queries?</p> <p><a href="http://msdn.microsoft.com/en-us/library/office/bb208956(v=office.12).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/office/bb208956(v=office.12).aspx</a></p> <p>It includes the following assertion, unsupported by code samples:</p> <p><BLOCKQUOTE> You can also include fixed values for which no data exists to create additional columns.</BLOCKQUOTE></p> <p>Yes, I would like to create a pivot table with a fixed <b>ordered</b> set of column headings from a pre-existing list. Here's a simplified SQL query:</p> <p><PRE> TRANSFORM SUM(tblData.Losses) As TotalLosses SELECT tblData.LossType FROM tblData GROUP BY tblData.Region PIVOT tblData.Year;</PRE></p> <p>I would like to add region names that are not in the table and I would like the regions to appear in a specific order. Yes, I can create a region listing table and left-join it: but that won't impose an arbitrary order, either - Crosstab queries always sort the columns left-to-right alphabetically. </p> <p>And I might just want to add arbitrary fixed values for which no data exists.</p> <p>Here's MSDN's information:</p> <p><BLOCKQUOTE><p>Syntax</p></p> <p>TRANSFORM <em>aggfunction</em>     <em>selectstatement</em>     PIVOT <em>pivotfield</em> [IN (<em>value1</em>[, <em>value2</em>[, …]])]</p> <p>The TRANSFORM statement has these parts: </p> <ul> <li><em>aggfunction</em>:&#9; An SQL aggregate function that operates on the selected data. </li> <li><em>selectstatement</em>:&#9; A SELECT statement. </li> <li><em>pivotfield</em>:&#9; The field or expression you want to use to create column headings in the query's result set. </li> <li><em>value1</em>,<em>value2</em>:&#9; Fixed values used to create column headings. </BLOCKQUOTE></li> </ul> <p>...And the rest is just fluff for creating a plain-vanilla pivot table from textbook data.</p> <p>So, my question is: <h2>Has anyone ever actually used fixed values to create column headings?</h2></p> <p>A sample of your SQL would be useful.</p> <hr> <p>This is a question about the published syntax for Microsoft Access SQL.</p> <p>Thank you for not asking why I want to do this, giving lengthy SQL examples that answer the question '<em>Is there ANSI SQL that does what a TRANSFORM statement does, by hardcoding everything</em>?' or pointing out that this would be easier in Postgres on a mainframe.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload