PSLine2000Documentation/Queries/CntlPanel4.md

70 lines
2.7 KiB
Markdown

# CntlPanel4
Analysis generated on: 4/2/2025 9:57:46 AM
---
## SQL Statement
```sql
SELECT Left$([PartNumber],7) AS pn, Process.*, Machines.*
FROM (CntlPanel3 LEFT JOIN Process ON CntlPanel3.PartNumber = Process.PartNumber) LEFT JOIN Machines ON Process.PartNumber = Machines.PartNumber
WHERE (((Machines.Prime)=Yes));
```
## Dependencies
- [[Tables/Process]]
- [[Tables/Machines]]
## Parameters
- *None*
## What it does
**SQL Query Description**
==========================
### Overview
This SQL query retrieves data from three tables: `CntlPanel3`, `Process`, and `Machines`. The query filters the results based on a condition involving the `Prime` column in the `Machines` table.
### Breakdown
#### Table Joining
The query starts by joining two tables using `LEFT JOIN`: `CntlPanel3` and `Process`. The join is based on the matching values in the `PartNumber` columns between these two tables. This creates a new temporary table with the data from both `CntlPanel3` and `Process`.
#### Second Join
The query then performs another `LEFT JOIN` with the `Machines` table, this time linking it to the intermediate result from the previous join. The join is again based on matching values in the `PartNumber` column.
#### Filtering
The final step applies a filter condition using the `WHERE` clause:
* Only rows where the value in the `Prime` column of the `Machines` table is equal to 'Yes' (`YES`) are included in the results.
* The `Left$()` function extracts the first 7 characters from the `PartNumber` column.
#### Derived Column
A new derived column, `pn`, is created by taking the result of the `Left$()` function on the `PartNumber` column and assigning it an alias (`AS pn`). This allows for easier reference to the shortened part number in subsequent parts of the query or the final result set.
### Result Set
The resulting data includes:
* The shortened `PartNumber` (7 characters) as `pn`.
* All columns from both the `Process` and `Machines` tables, but only rows where the `Prime` value is 'Yes'.
**Example Use Case**
Suppose you're analyzing inventory levels for parts used in manufacturing. This query would help identify which parts with a prime status are stored in certain machines or devices.
**Code Explanation**
```sql
-- Join CntlPanel3 and Process tables on PartNumber
FROM (CntlPanel3 LEFT JOIN Process ON CntlPanel3.PartNumber = Process.PartNumber)
-- Left join the result to Machines table based on Process.PartNumber
LEFT JOIN Machines ON Process.PartNumber = Machines.PartNumber
-- Filter results where Prime value is 'Yes'
WHERE (((Machines.Prime) = Yes));
```
In summary, this SQL query retrieves part information with a prime status from `CntlPanel3`, `Process`, and `Machines` tables.