PSLine2000Documentation/Queries/Scott'sQ2.md

66 lines
3.2 KiB
Markdown

# Scott'sQ2
Analysis generated on: 4/2/2025 10:10:52 AM
---
## SQL Statement
```sql
SELECT DISTINCTROW Left$([PartNumber],7) AS parts, First(Right$(Trim$([PartNumber]),1)) AS Expr2, First([Scott'sQ1].PartNumber) AS FirstOfPartNumber, First([Scott'sQ1].Prime) AS FirstOfPrime, First(Left$([PartNumber],1)) AS Expr3, [Scott'sQ1].MachineName, [Scott'sQ1].Expr1, Process.PartsPerBlank
FROM [Scott'sQ1] INNER JOIN Process ON [Scott'sQ1].PartNumber = Process.PartNumber
GROUP BY Left$([PartNumber],7), [Scott'sQ1].MachineName, [Scott'sQ1].Expr1, Process.PartsPerBlank
HAVING (((First(Left$([PartNumber],1)))="9"))
ORDER BY Left$([PartNumber],7), First(Right$(Trim$([PartNumber]),1)) DESC;
```
## Dependencies
- [[Queries/[Scott'sQ1]]]
- [[Tables/Process]]
## Parameters
- *None*
## What it does
**SQL Query Description**
==========================
This SQL query retrieves specific data from two tables: `Scott'sQ1` and `Process`. The query uses various aggregation functions, joining the two tables based on a common column, and applies conditions to filter the results.
### Table Join and Data Retrieval
The query joins the `Scott'sQ1` table with the `Process` table on the condition that the `PartNumber` column in both tables is equal. The joined table contains data from both sources, allowing for aggregation and filtering operations.
The query selects various columns:
* `Left$([PartNumber],7)`: Truncates the `PartNumber` to 7 characters from the left.
* `First(Right$(Trim$([PartNumber]),1))`: Removes the first character from the right side of the trimmed `PartNumber`.
* `First([Scott'sQ1].PartNumber)`, `First([Scott'sQ1].Prime)`: Retrieves the unique value for each column.
* `Left$([PartNumber],1)`: Truncates the `PartNumber` to 1 character from the left.
* `[Scott'sQ1].MachineName`, `[Scott'sQ1].Expr1`, `Process.PartsPerBlank`: Retrivees data from these columns directly.
### Grouping and Filtering
The query groups the results by:
* Truncated `PartNumber` (`Left$([PartNumber],7)`).
* `MachineName`.
* `Expr1`.
* `PartsPerBlank`.
A filter condition is applied to the grouped data using the `HAVING` clause. The filter checks if the first character of the truncated `PartNumber` is equal to "9". Only rows that satisfy this condition are included in the final results.
### Sorting
The query sorts the filtered results by:
* Truncated `PartNumber` (`Left$([PartNumber],7)`).
* First character removed from trimmed `PartNumber` (`First(Right$(Trim$([PartNumber]),1))`).
**Example Output**
-----------------
| parts | Expr2 | FirstOfPartNumber | FirstOfPrime | Expr3 | MachineName | Expr1 | PartsPerBlank |
| ----- | ---------- | ----------------- | -------------- | -------- |------------- | --------- | ------------ |
| ... | ... | ... | ... | ... | ... | ... | ... |
**Notes**
* The `SELECT DISTINCTROW` clause is used to ensure that each row in the result set is unique.
* The `First(Right$(Trim$([PartNumber]),1))` expression uses the `RIGHT` function to remove the first character from the right side of the trimmed string, and the `FIRST` aggregation function to retrieve the resulting value.
* The use of `$` in column names suggests that these are SQL Server-specific identifiers.