PSLine2000Documentation/Queries/UniversalQ.md

69 lines
2.3 KiB
Markdown

# UniversalQ
Analysis generated on: 4/2/2025 10:13:09 AM
---
## SQL Statement
```sql
SELECT DISTINCTROW Process.*
FROM Process
ORDER BY Left$(Right$(" "+[Process].[PartNumber],5),4), Right$([PartNumber],1) DESC;
```
## Dependencies
- [[Tables/Process]]
## Parameters
- *None*
## What it does
**SQL Code Description**
==========================
### Overview
The provided SQL code retrieves a distinct set of records from the `Process` table, ordered based on two conditions: part number length and its first character.
### Breakdown
#### 1. Selecting Distinct Records
```sql
SELECT DISTINCTROW Process.*
```
This line selects all columns (`*`) from the `Process` table, but only returns distinct records. The `DISTINCTROW` keyword ensures that each row in the result set is unique based on the specified column(s).
#### 2. Sorting Criteria
The sorting criteria for this query are:
- Part number length (first 5 characters) in descending order
- First character of part number in ascending order
These conditions are applied using string manipulation functions (`Left` and `Right`) and comparisons (`ORDER BY` clause).
#### 3. String Manipulation Functions
```sql
LEFT$([PartNumber],4)
```
This function extracts the first 4 characters from the `PartNumber`. The `$` symbol denotes a "literal" string, which means the result is treated as a literal value rather than being interpreted by SQL.
```sql
RIGHT$(" "+[Process].[PartNumber],1)
```
This function takes the following steps:
- Concatenates an additional 5 spaces (`" "`) with the `PartNumber`.
- Extracts the first character from this resulting string using `Right`.
#### 4. ORDER BY Clause
```sql
ORDER BY Left$([PartNumber],5), Right$([PartNumber],1) DESC;
```
The results are sorted according to the two conditions mentioned earlier:
- Part number length (first 5 characters): shorter lengths come first.
- First character of part number: alphabetical order from A to Z.
Both conditions are applied in descending order, meaning records with longer part numbers and starting letters that appear later alphabetically will be prioritized.
### Result
The query returns a list of distinct `Process` records sorted according to the specified criteria. The resulting set may have fewer rows than the original table if duplicate records were present.