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
Related
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
I want to check in a powerquery new column if a string like "This is a test string" contains any of the strings list items {"dog","string","bark"}.
I already tried Text.PositionOfAny("This is a test string",{"dog","string","bark"}), but the function only accepts single-character values
Expression.Error: The value isn't a single-character string.
Any solution for this?
This is a case where you'll want to combine a few M library functions together.
You'll want to use Text.Contains many times against a list, which is a good case for List.Transform. List.AnyTrue will tell you if any string matched.
List.AnyTrue(List.Transform({"dog","string","bark"}, (substring) => Text.Contains("This is a test string", substring)))
If you wished that there was a Text.ContainsAny function, you can write it!
let
Text.ContainsAny = (string as text, list as list) as logical =>
List.AnyTrue(List.Transform(list, (substring) => Text.Contains(string, substring))),
Invoked = Text.ContainsAny("This is a test string", {"dog","string","bark"})
in
Invoked
Another simple solution is this:
List.ContainsAny(Text.SplitAny("This is a test string", " "), {"dog","string","bark"})
It transforms the text into a list because there we find a function that does what you need.
If it's a specific (static) list of matches, you'll want to add a custom column with an if then else statement in PQ. Then use a filter on that column to keep or remove the columns. AFAIK PQ doesn't support regex so Alexey's solution won't work.
If you need the lookup to be dynamic, it gets more complicated... but doable you essentially need to
have an ID column for the original row.
duplicate the query so you have two queries, then in the newly created query
split the text field into separate columns, usually by space
unpivot the newly created columns.
get the list of intended names
use list.generate method to generate a list that shows 1 if there's a match and 0 if there isn't.
sum the values of the list
if sum > 0 then mark that row as a match, usually I use the value 1 in a new column. Then you can filter the table to keep only rows with value 1 in the new column. Then group this table on ID - this is the list of ID that contain the match. Now use the merge feature to merge in the first table ensuring you keep only rows that match the IDs. That should get you to where you want to be.
Thanks for giving me the lead. In my own case I needed to ensure two items exist in a string hence I replaced formula as:
List.AllTrue(List.Transform({"/","2017"},(substring) => Text.Contains("4/6/2017 13",substring)))
it returned true perfectly.
You can use regex here with logical OR - | expression :
/dog|string|bark/.test("This is a test string") // retruns true
I have a table, DEBTOR, with a structure like this:
and a second table, DEBTOR.INFO structured like this:
I have a select list made of record IDs from the DEBTOR.INFO table. How can I
select * from DEBTOR WHERE 53 IN (name of select list)?
Is this even possible?
I realize this query looks more like SQL than RetrieVe but I wrote it that way for an easier understanding of what I'm trying to accomplish.
Currently, I accomplish this query by writing
SELECT DEBTOR WITH 53 EQ [paste list of DEBTOR.INFO record IDs]
but obviously this is unwieldy for large lists.
It looks to me that you cant do that. Even if you use and i-descriptor, It only works in one direction. TRANS("DEBTOR.INFO",53,0,"X") works from the DEBTOR file but not the other way. So TRANS("DEBTOR",#ID,53,"X") from DEBTOR.INFO will return nothing.
See this article on U2's site for a possible solution.
Would something like this work (two steps):
SELECT DEBTOR.INFO SAVING PACKET
LIST DEBTOR ....
This creates a select list of the data in the PACKET field in the DEBTOR.INFO file and makes it active. (If you have duplicate values that way you can add the keyword UNIQUE after SAVING).
Then the subsequent LIST command uses that active select list which contains values found in the #ID field of the file DEBTOR.
Not sure if you are still looking at this, but there is a simple option that will not require a lot of programming.
I did it with a program, a subroutine and a dictionary item.
First I set a named common variable to contain the list of DEBTOR.INFO ids:
SETLIST
*
* Use named common to hold list of keys
COMMON /MYKEYS/ KEYLIST
*
* Note for this example I am reading the list from SAVEDLISTS
OPEN "SAVEDLISTS" TO FILE ELSE STOP "CAN NOT OPEN SAVEDLISTS"
READ KEYLIST FROM FILE, "MIKE000" ELSE STOP "NO MIKE000 ITEM"
Now, I can create a subroutine that checks for a value in that list
CHECKLIST
SUBROUTINE CHECKLIST( RVAL, IVAL)
COMMON /MYKEYS/ KEYLIST
LOCATE IVAL IN KEYLIST <1> SETTING POS THEN
RVAL = 1
END ELSE RVAL = 0
RETURN
Lastly, I use a dictionary item to call the subroutine with the field I am looking for:
INLIST:
I
SUBR("CHECKLIST", FK)
IN LIST
10R
S
Now all I have to do is put the correct criteria on my list statement:
LIST DEBTOR WITH INLIST = 1 ACCOUNT STATUS FK
Id use the very powerfull EVAL with an XLATE ;
SELECT DEBTOR WITH EVAL \XLATE('DEBTOR.INFO',#RECORD<53>,'-1','X')\ NE ""
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
I am a DBA of 7 months so please bear with me. I am needing to write a code that will find a particular ProductIdentifier. When this particular ProductIdentifier is found, 1. I need to grab this ProductIdentifier. 2. I need to go 2 rows up and place that ProductIdentifier in the field that is 2 rows above it.
Here is my code(everything is sorted properly already in this table)
SELECT
SipID,
SaleInvoiceID,
AssociationNumber,
Priority,
TotalPrice,
TotalCost,
SerialNumber,
ContractNumber,
ActivatedThroughPAW,
DateCreatedatMidnight,
ReceivedDate,
InvoiceIDByStore,
Location,
ProductIdentifier,
Description,
ShortDescription,
CategoryName,
RevenueStreamID,
RevenueType
FROM REVISEDTABLE.
I will better show you what needs to be done ![enter image description here][1]
ProductIdentifier
AWUPG2001RGP -- replace this product identifier with the 'AWRPNS000%'
POSC0021PRW
AWRPNS000343 --take this product identifier
What I need for this code to do is this: whenever I find any ProductIdentifier like 'AWRPNS000%', I need for the query to take this and go 2 rows up and replace whatever ProductIdentifier is in this with 'AWRPNS000%'. I then need to insert the results into a table. I believe the best thing to do is to select the ProductIdentifier row again and give it an alias. This will be the row that I need to transform. I can then do a comparison to see if things worked out. I do not know how to write the code to do the actual grabbing of the ProductIdentifier and going up 2 rows and replacing it, so any help or input would be greatly appreciated.
So what does two rows up mean. Why is it two rows up.
e.g.
ID Class Type Date
1 1 2 20/12/2012
2 1 2 21/12/2012
3 1 2 22/12/2012 *
ie yes ID is two rows up but that's because The records are in ID and date order and there are at least three of them.
If you can come up with that rule e.g.
Select * From SomeTable Where Class = 1 and Type = 2 And Date = 20/12/2012
Then all your problems go away...