PSLine2000Documentation/Queries/Machines_Crosstab.md

74 lines
2.6 KiB
Markdown

# Machines_Crosstab
Analysis generated on: 4/2/2025 10:02:02 AM
---
## SQL Statement
```sql
TRANSFORM First(Machines.CycleTime) AS FirstOfCycleTime
SELECT Machines.PartNumber, Left([PartNumber],7) AS Part, Max(IIf([Prime],[MachineName])) AS PrimaryMachine, Format(Max(IIf([Prime],[Cycletime])),"0.0") AS StdTime
FROM Machines
GROUP BY Machines.PartNumber
PIVOT Machines.MachineName;
```
## Dependencies
- [[Tables/Machines]]
## Parameters
- *None*
## What it does
**SQL Code Description**
==========================
### Overview
This SQL code transforms the `Machines` table by performing several operations:
1. **Selecting first occurrence of cycle time**: It selects the first record from each group based on the `PartNumber`.
2. **Extracting part number**: It extracts the first 7 characters from the `PartNumber`.
3. **Identifying primary machine**: It identifies the maximum value in the `MachineName` column for each `PartNumber`, considering only records where `[Prime] = TRUE`. If no such record exists, it returns an empty string.
4. **Calculating standard time**: It calculates the maximum cycle time for each `PartNumber` and formats it as a decimal value with two places.
### Detailed Breakdown
#### First Section: SELECT Statement
```sql
SELECT
Machines.PartNumber,
Left([PartNumber],7) AS Part,
Max(IIf([Prime],[MachineName])) AS PrimaryMachine,
Format(Max(IIf([Prime],[Cycletime])), "0.0") AS StdTime
FROM
Machines
GROUP BY
Machines.PartNumber
```
* This section selects the required columns from the `Machines` table.
* It groups the results by the `PartNumber`.
* The `Left([PartNumber], 7)` function extracts the first 7 characters from the `PartNumber`.
#### Second Section: TRANSFORM Statement
```sql
TRANSFORM
First(Machines.CycleTime) AS FirstOfCycleTime
```
* This section transforms the results by selecting the first occurrence of `CycleTime` for each group based on the `PartNumber`.
#### Final Section: PIVOT Statement
```sql
PIVOT
Machines.MachineName;
```
* This section pivots the results, rotating the columns to match the unique values in the `MachineName` column.
### Result
The final result is a table with the following structure:
| Part | Primary Machine | Std Time |
| --- | --- | --- |
| ... | ... | ... |
Each row represents a part number, and the columns contain the extracted part number, primary machine name, and standard time value. The `PrimaryMachine` column contains the maximum machine name for each part number where `[Prime] = TRUE`, and the `StdTime` column contains the formatted maximum cycle time for each part number.