This question already has answers here:
EntityFramework - contains query of composite key
(12 answers)
Closed 2 years ago.
Let a table TabA, with only 3 int columns : TabA (col1,col2,col3)
Let a list L2int, of type list<int c1,int c2> with several items (ex ((1,2),(2,3)....)
I want to use linq methods (or linq query second way) to find from TabA the values of col1 where (col2,col3) is in Liste2int (col3==c3 && col2==c2).
The result should be a list of int ie List.
Thanks for your attention
var ln= from c in L2int from cols in Tab2 where cols.col1==c.c1 && cols.col2==c.c2 select new {foo=cols.col3}
thx
I want to add that linq queries or linq methods are less performant than raw SQL (direct sql)!
Related
This question already has answers here:
C# SQLite Parameterized Select Using LIKE
(2 answers)
Closed 5 years ago.
I am using SQLite in C++ to query a database. I want to use the LIKE operator in a select statement such as
SELECT * FROM table WHERE columnName LIKE '%abc%'
But I need to bind the value of abc in the above statement to query. Usually, for normal statements such as SELECT * FROM table WHERE columnName=#name, we can use query.bind(#name, 'name');
But for the LIKE operator, I am not able to get this working. I am not sure if I should be giving it as '%#name%'? This doesn't work and throws an binding error exception.
Any suggestions will be really helpful since otherwise, I need to go through the whole search result having 100's of rows and check each row for a value from an big string array. The idea is to give the string array element as the binding value for the LIKE operator to search the database and fetch just one row.
Any ideas?
Thanks a lot, in advance.
Esash
You can add the percent signs after binding:
... WHERE Col Like '%' || ? || '%'
If name is a variable, use this:
name = "%" + name + "%";
query.bind("#name", name);
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.
This question already has answers here:
How do I make an UPDATE while joining tables on SQLite?
(2 answers)
Closed 6 years ago.
My select statement finds the records I want to update- Now I want to invert (multiply x -1) the adjusted_sentiment score for only these records. Here is the select statement:
Select players.name, fbposts.company, fbposts.post_id, reactions.reaction,
fbposts_comments.adjusted_sentiment, fbposts_comments.message, fbposts.message from fbposts
join reactions on reactions.post_id = fbposts.post_id
join players on players.id = reactions.id
join fbposts_comments on fbposts_comments.post_id = fbposts.post_id
where adjusted_sentiment > 0 and reactions.reaction like "ANGRY" and
reactions.id = fbposts_comments.comment_id group by fbposts.post_id
This returns records like:
Baktiyar Romanov,WorldOfWanderlust,387990201279405_994067924004960,ANGRY,0.5965,probably fed very ill-mannered rich man
Sheridan Toms,australiapost,96085205666_10153485650690667,ANGRY,0.04676666666666666,Seriously? You can't even get an express post parcel from victoria to wa in under 2 weeks!!!! Super annoyed
Robert Smissen,australiapost,96085205666_10153487649895667,ANGRY,0.8555,Looks like Australia Post is using Diggers' letters to gain some reflected glory
Eve Ismaiel,australiapost,96085205666_10153500759100667,ANGRY,0.1133333333333333,"Ha ha... Present $20, postage $30!!!"
What I want to do is invert the adjusted_sentiment score. For example in the first record, the adjusted_sentiment score is 0.5965 I want to update that to -0.5965
BTW my queries and updates will be done via Python2.7... One thought I working on now is to create a list from the query above then use that list to create a series of update statements.
Below query will give you the expected output
update fbposts_comments set adjusted_sentiment = -adjusted_sentiment where post_id in (Select fbposts_comments.post_id from fbposts
join reactions on reactions.post_id = fbposts.post_id
join players on players.id = reactions.id
join fbposts_comments on fbposts_comments.post_id = fbposts.post_id
where adjusted_sentiment > 0 and reactions.reaction like "ANGRY" and
reactions.id = fbposts_comments.comment_id group by fbposts.post_id)
There is a questions table with a column called options which contains
'r>>>>>5####answer1|4####answer2|3####answer3|2####answer4|1####answer5'
Which are pairs of possible answers
5 = answer1
4 = answer2
etc.
The questions are set up by users but the pattern is the same.
Another table has a users answer which is the option selected. I'm attempting to write SQL to extract the answer text.
For example 4 should display answer2.
I tried:
SELECT substring(question.options from '%4#####"%#"[\|]%' for '#') AS answertext
FROM ...
But it displays answer2|3####answer3|2####answer4.
How do I get everything up to the first |?
The regular expression supplied by #Abelisto in a comment works nicely. You don't need to escape | in the character class, so it can be:
SELECT substring(options, '4#+([^|]+)' ...
Assuming the number of the answer is limited to a single digit, there is a simple solution without regular expressions, too:
SELECT right(x, -5) AS answer
FROM unnest( string_to_array(right(
'r>>>>>5####answer1|4####answer2|3####answer3|2####answer4|1####answer5'
, -6), '|')) x
WHERE x LIKE '4%'; -- pick number
Applied to your table:
SELECT right(x, -5) AS answer
FROM question q, unnest( string_to_array(right(q.options, -6), '|')) x
WHERE q.question_id = 1
AND x LIKE '4%';
But really, you should normalize your unfortunate design. It's a 1:n design between question and answers. Assuming a PK question.question_id you can fix it quickly like this:
CREATE TABLE answer AS
SELECT q.question_id, left(x,1)::int AS answer_id, right(x, -5) AS answer
FROM question q, unnest(string_to_array(right(q.options, -6), '|')) x;
ALTER TABLE answer
ADD PRIMARY KEY (question_id, answer_id)
, ADD CONSTRAINT q_fk FOREIGN KEY (question_id) REFERENCES question(question_id)
ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE question DROP column options;
Then your query simply is:
SELECT answer
FROM answer
WHERE question_id = 1
AND answer_id = 4;
Related:
How to implement a many-to-many relationship in PostgreSQL?
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