Unconventional sorting of table elements using SQL in Qt - c++

I'm creating a ticketing system and I want to populate a table for people to view all of the open tickets. I want them to be sorted by priority, from critical --> high --> medium --> low.
I'm reusing code from a test database program I made before, which ordered entries in a table by "last name."
while(currentRow < rows){
qry.prepare("SELECT * FROM users ORDER BY lastname LIMIT :f1, :f2");
qry.bindValue(":f1", currentRow);
qry.bindValue(":f2", currentRow+1);
qry.exec();
qry.next();
currentCol = 0;
//this loop will populate all columns in the current row
while(currentCol < ui->table->columnCount()){
QTableWidgetItem *setdes = new QTableWidgetItem;
setdes->setText(qry.value(currentCol).toString());
ui->table->setItem(count, currentCol-1, setdes);
currentCol++;
}
currentRow++;
}
Obviously I can't just change the query from lastname to priority, because they would be sorted alphabetically instead of the order that I want them to be sorted in. Is there a way I can execute an SQL query to sort them in the order I provided above, or am I going to have to populate the table and then sort it myself?

You can use a CASE WHEN ... THEN construct to map your strings to numeric values to sort on, e.g.
... ORDER BY CASE
WHEN priority='critical' THEN 1
WHEN priority='high' THEN 2
WHEN priority='medium' THEN 3
WHEN priority='low' THEN 4
ELSE 5
END

Related

Sort column with repeated values by another column

In Power BI Desktop, I'm trying to order the following column with repeated values by an ID column (contains primary key).
This returns the error: "There can't be more than one value in "Nível2"...."
In this other post it seems the suggestion is to concatenate the values of the column so they don't get duplicate.
But I want them to be repeated so they can aggregate values in visuals.
So, what's the workaround for this situation?
Thanks in advance for helping!
The issue is that your sort column (i.e. your ID column) contains multiple values for each value in the column you are trying to sort (i.e. your Nivel2 column).
You need to ensure that your sort column contains only one distinct value for each value in the column you are trying to sort.
One way to achieve this would be to create a new (calculated) sort column based on your ID column. It could be defined like this:
SortColumn:=CALCULATE(MAX('YourTable'[ID]),ALLEXCEPT('YourTable','YourTable'[Nivel2]))
Here is an example of how the SortColumn would behave:
Id Nivel2 SortColumn
1 Caixa 4
2 Caixa 4
3 Caixa 4
4 Caixa 4
5 Depósitos à ordem 7
6 Depósitos à ordem 7
7 Depósitos à ordem 7
You can now sort Nivel2 by SortColumn.
EDIT - The implementation of the SortColumn should be done in the data source
There seems to be a limitation in PowerBI where it checks the implementation of the sort column rather than the data in the sort column. Therefore the above solution does not work, even though the data in the sort column is perfectly valid. The above solution will throw this error when you attempt to sort [Nivel2] by SortColumn:
This column can't be sorted by a column that is already sorted, directly or indirectly, by this column.
The implementation of the SortColumn should be moved to the data source instead. I.e. if your data source is an Excel sheet, then the SortColumn should be created inside the Excel sheet.
The above answer does explain the issue and the resolvation correctly. The only change is that the SortColumn must be implemented outside of the tabular model (PowerBI) to ensure that PowerBI does not know about the dependency between the SortColumn and the [Nivel2] column.
In my case, I calculate the levels from a parent-child hierarchy
Path = Path([id],[father])
For each level:
Level1 = LOOKUPVALUE([Name],[id], PathItem([Path],1))
Level2 = LOOKUPVALUE([Name],[id], PathItem([Path],2))
.....
Then I created a new column for each level to sort the column Level:
SortL1 = LOOKUPVALUE([nID],[id], PathItem([Path],1))
SortL2 = LOOKUPVALUE([nID],[id], PathItem([Path],2))
.....
id and nID is the same numeric variable but "id" in string format because Path do not support numeric values.

SAS Counting Keywords Across Columns

I have list of keywords; CORPORATE, REAL ESTATE, COMPETITION, TRADE, DISPUTE
I want to be able to count the number of occurrences these keywords appear between columns practice_area1—Practice_area10. Therefore, I want to scan across 10 columns and create new columns. Each new column will represent each of the keywords (above) and a count as a value e.g. Corporate 4
Once this statement has ran and we have our new columns I want to create a new variable “Practice Group” which is populated with the highest count of the new variables we have just created. The dataset below is an example of how the data should look:
Please could somebody offer me some advice of the best approach to do this?
Many Thanks
Chris
All you need to do is use makean array for all the columns which you want to check. Then do loop it through each column for the word you want to check by using count function and add the count in the loop.
Code below is checking for three columns and three values. You can apply this code to as many columns as you want.
data have(drop= i);
col1 = 'CORPORATE, REAL ESTATE, REAL ESTATE';
col2= 'CORPORATE, CORPORATE, TRADE, TRADE, TRADE, REAL ESTATE';
col3= 'TRADE, TRADE, DISPUTE,REAL ESTATE';
array col[*] col1 - col3;
realestate=0;/*starting with zero*/
trade=0;
corporate= 0;
do i = 1 to 3;
realestate =realestate+count( col(i), 'REAL ESTATE');/* adding through the loop*/
TRADE =trade+count( col(i), 'TRADE');
CORPORATE= corporate+count( col(i), 'CORPORATE');
end;
run;

SQLite increment Integer Primary Key and Unique Constraint conflict

I have a SQLite database.
When writing move rows function, which moves rows from one table to another I need to have a query for incrementing column with name "row" which is INTEGER PRIMARY KEY, but there is an error. It is critical to have indexing with row in my task. The condition in example is WHERE row >= 2 because i am inserting rows from other table into position 2.
"UPDATE '4' SET row = row + 1 WHERE row >= 2"
Error("19", "Unable to fetch row", "UNIQUE constraint failed: 4.row")
The problem's origin WHERE row >= 2" part. How to overcome this problem?
The problem's origin WHERE row >= 2" part.
I'm inclined to disagree. The problem is not with which rows are updated, it is with the order in which they are updated.
Very likely SQLite will process rows in rowid order, which almost certainly is also increasing order of the row column, since that column is an auto-incremented PK. Suppose, then, that the table contains two rows with row values 2 and 3. If it processes the first row first, then it attempts to set that row's row value to 3, but that produces a constraint violation because that column is subject to a uniqueness constraint, and there is already a row with value 3 in that column.
How to overcome this problem?
Do not modify PK values, and especially do not modify the values of surrogate PKs, which substantially all auto-increment keys are.
Alternatively, update the rows into a temporary table, clear the original table, and copy the updated values back into it. This can be extremely messy if you have any FKs referencing this PK, however, so go back to the "Do not modify PK values" advice that I led off with.
First: '4' is not a table name. The UPDATE statement expects a table name where you have written '4'. For example:
UPDATE table1 SET row = row + 1 WHERE row >= 2
Second: Just do not use row as a primary key (or unique key, for that matter) when it obviously is not meant as primary key but as a changing row number. Create a separate column that can be used as primary index of that table instead.

Sorting items by dates in DynamoDB

I come from a strict SQL background.
Now migrating to DynamoDB, I have a table full of items which I would like to sort by dates.
Here is how I do it:
I set up a secondary index Category-Date-Index. Category is Hash and Date is Range. All items I am sorting will have the same value for category.
The problem I now have is that many items have the same dates. This secondary index automatically drops items with the same Category-Date and keeps only one. This is not the behavior I desire.
What would be the right way to do this?
I would also appreciate pointers to a good reading on how to structure tables and indices in DynamoDB when considering these use cases.
how many items with same date do you have?
you can always add to your date an extra postfix (like a random number in range of 0 - X - if your date is an int - epoc time) - this will also ensure your sorting. (only if your range is string, and you always add the same number of digits)
for example:
original item = (hash, 1234567)
converted item = (hash, '1234567010')
original item2 = (hash, 1234567)
converted item2 = (hash, '1234567900')
you can use 'overwrite' param (and set it to false) when inserting an item. in case of an error, you can add an extra number to your range key.
you can find good guidelines here:
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GuidelinesForTables.html

Search query in sqlite3 database for certain items

I have a list of items as an string array in C++. I also have a sqlite3 database which contains blacklisted strings. Now I must Use the list of items that i have to mark them with 0 or 1, telling me if they are blacklisted or not. I could do search for them one by one by using "Select * from ITEMS_TABLE WHERE item = string[i]" but it will take time. I could also pull blacklist from database and then look for them in my list. But is there an efficient way to find out which of the items in my list are blacklisted.
Lets say I have following structure
struct item
{
char name[MAX_NAME_LEN];
bool isBlacklisted;
};
Then i use array of these structures to knows if any of them is blacklisted. So i have to make isBlacklisted flag to true, if the entry is found in database. If i use Select approach, it returns me list of items that were blacklisted. But i still need to find them in my array using string comparisons. Is there some efficient way to do is. Does database provide any such functionality.
Thanks and regards,
Mike.
Design your database structure according to your requirements. You want to know blacklist items simply use a column which contains 0 or 1 for blacklist or not i.e your table ITEMS_TABLE has these columns
itemcode itemname isblacklist
1 item1 0
2 item2 0
3 item3 1
now
Select * from ITEMS_TABLE WHERE isblacklist=0
this will return non blacklist items and
Select * from ITEMS_TABLE WHERE isblacklist=1
will return blacklist items, Hope this will help you