More efficient way to compare records in DataGridView - vb.net

I have a DataGridView (bound to a DataTable) which has data something like this:
| Col1 | Col2 |
|------|------|
| 1 | Y |
| 1 | N |
| 2 | Y |
What I need to do is find where a duplicated value in Col1 has a different value in Col2.
At the moment I do a nested For Each loop to compare each row in the DataGridView with every row in the DataGridView but this is, obviously, very slow and scales horribly... Cost is somewhere around n^n.
Is there a better way I can do this without having to loop over the whole DataGridview for every record?
I want to filter the DataGridView to only show records where there is a mismatch between records of value in Col1 and values in Col2 (in this case, '1' should be shown because it contains both 'Y' and 'N').

Related

Replace rows from table that has specific value of multiple columns?

Let's say I have a table like this called MyTable.
| Column A | Column B || Column C | Column D |
| -------- | -------- || -------- | -------- |
| Cell 1 | Cell 2 || Cell 3 | Cell 4 |
| Cell 5 | Cell 6 || Cell 7 | Cell 8 |
And now I am inserting new row into this table that has format like this:
| Cell 1 | Cell 2 || Cell 3 | Cell Something else |.
What I want to do is replace an existing row from MyTable if the row I am inserting has the same value of the first 3 columns of MyTable (column A, column B, column C). As my real table has 250+ columns and
I want to replace rows if they have same value of 5 columns, I don't think INSERT ON CONFLICT UPDATE is good for this. In my opinion, it would be best to DELETE rows that need to be replaced and just INSERT new ones, but I don't know how to write that query.
I was thinking of INSERT ON CONFLICT UPDATE but firstly: I don't think I can specify more columns in ON CONFLICT part, and secondly: I think that I would need to specify 250 columns in UPDATE part, so that also doesn't work for me.
There is no problem specifying multiple columns in the on conflict clause, you just need a unique constraint on those columns. (see demo). As far as you having 250 columns (a highly questionable design, but another question altogether) you have no way around it you must list every column you want updated.

SQL - Retrieve only one record of related records

I have table which depicts shares of a particular type of record. 2 records are created for a shared item which results in something like this
|--------------|------------|
| Shared From | Shared To |
|--------------|------------|
| Record 1 | Record 2 |
|--------------|------------|
| Record 2 | Record 1 |
|--------------|------------|
Is it possible to retrieve a single share record ? Meaning that from the table above I get only one record (Doesn't make a difference which)
|--------------|------------|
| Shared From | Shared To |
|--------------|------------|
| Record 1 | Record 2 |
Using distinct on both columns doesn't work since the combination is different
Use case expressions to return the smaller value in the first column, and the larger value in the seceond column. Do SELECT DISTINCT to remove duplicates.
select distinct case when SharedFrom < SharedTo then SharedFrom else SharedTo end,
case when SharedFrom > SharedTo then SharedFrom else SharedTo end
from tablename
Note: May switch columns for unique combinations. (If col1 > col2.)
If I understand you correctly, you want to get a single line from the table.
To get the top N of rows in a table, you can use TOP('N'), for example:
SELECT TOP(1) share_column FROM shares_table

Logic behind show/hide rows in charts

I am confused about the logic that exists behind the showing and hiding of rows in charts of QlikView/QLik Sense. Here is what I thought was the case:
If, for some row, the value of a dimension is NULL, and for that dimension "Supress NULL" is on (QV) or "Include NULLs" is off(QS), then the row is not shown.
If, for some row, all its expressions/measures are zero or NULL, and the object-level setting "Supress Zero Values" is on (QV), or "Include Zero Values" is off (QS), then the row is not shown.
The rest of the rows are shown.
However, I get a confusing example of a measure which causes rows to disappear even though I have suppress zero values off/ inlude zero values on. Here is a small script of some sample customers and their consultation:
customer:
LOAD * INLINE [
custcode,descr
C1,pan1
C2,pan2
C3,pan3
];
consultation:
LOAD * INLINE [
custcode,grp,val,x
C2,eye,sth1,1
C2,age,20,1
C3,legs,sth2,1
C3,skin,sth5,1
C3,age,20,1
C3,age,30,1
];
As you can see, custcode C1 has no consultation lines. I proceed to create a straight table with custcode as dimension and sum(x) as measure. Here is what I get:
+----------+--------+
| custcode | sum(x) |
+----------+--------+
| C1 | 0 |
| C2 | 2 |
| C3 | 4 |
+----------+--------+
Everything fine until now. Sure enough I haven't supressed zero values: If I did, the C1 row would get removed. Also, let's note that no aggr is needed for whatever reason.
Now, let's add a set analysis to that measure to only sum x for grp='age':
sum({<grp={'age'}>}x)
This hides row C1 from sight:
+----------+-----------------------+
| custcode | sum({<grp={'age'}>}x) |
+----------+-----------------------+
| C2 | 1 |
| C3 | 2 |
+----------+-----------------------+
Question 1: Why does set analysis hide the row in this case?
Adding an additional measure with a value of 1 changes nothing. We can be sure this has nothing to do with zero values settings.
Now, let us add this measure:
aggr(min(0),[custcode])
The row got back, even though the new measure is NULL :
+----------+-----------------------+-------------------------+
| custcode | sum({<grp={'age'}>}x) | aggr(min(0),[custcode]) |
+----------+-----------------------+-------------------------+
| C1 | 0 | - |
| C2 | 1 | - |
| C3 | 2 | - |
+----------+-----------------------+-------------------------+
Now, about aggr, here are two strong reasons why I think it should not be neccessary:
If the set analysis measure was wrong and needed to include aggr in some way, this would still be no reason for the engine to hide the rows - it would just return a NULL for having an invalid formula. Also, this measure actually works correct, as we can see
custcode is the field which creates the association between the two tables. But this doesn't seem to be the cause for it to unhide rows; actually, I get the same even with aggr(min(0),[]):
+----------+-----------------------+-----------------+
| custcode | sum({<grp={'age'}>}x) | aggr(min(0),[]) |
+----------+-----------------------+-----------------+
| C1 | 0 | - |
| C2 | 1 | - |
| C3 | 2 | - |
+----------+-----------------------+-----------------+
Question 2: Why does this strange aggr measure unhide the row?
Question 1:
When you only have the one expression and add the set analysis it is like telling Qlik to select the set value.
So picture 1 no selections
picture 2 with the selection
So it's not that the answer is a null, it is that the associative engine has reduced that data out of the data set based on the selection / set rule.
The aggr() should definitely not be necessary there. The dimentionality of the chart will take care of the aggregation across the dimension. aggr() is only needed when you want to use an aggregation that is not controlled by the dimensions.
I do not understand what your aggr(min(0),[]) is trying to achieve and I don;'t get the same result as your table. The expression is just creating nulls because it is can't evaluate
If you want to see all members of the dimension you should tick "Show all values" on the dimensions tab rather than trying to change the expressions

Comparing Column1 to Column 2 and writing to Column3 if match

I have an excel worksheet linked to a SQL query in column [Raw Data]. After adding a few columns with formulas to clean up the raw data, i need to find if the value in column [ProcDataQ] exists in column [ProcDataO], all columns comprise to make Table1.
ProcDataQ | ProcDataO | Stat
--------- | --------- | ----
C1234 | C7126 | Ordered
C8372 | C6152 | No Order
C7126 | C1234 | Ordered
I am able to do this with the below formula but i have more than 20,000 records and it takes on or around 30 seconds to load or refresh the table and i figured i could speed this up using a little vba that I'll trigger to run on the query refresh.
=IF(AND(LEFT([#[Raw Data]],1)="q", (NOT(ISERROR(MATCH([#ProcDataQ],[ProcDataO], 0))))),"Ordered", "No Order Placed")
fyi, i am running excel 2010 on PC.
Just use an IF and COUNTIF statement:
=IF(COUNTIF(range, item to look up)>0,"Ordered","Not ordered")

Sqlite : Loop through rows and match, break when distinct row encountered

I want to compare two tables A and B, row by row on the basis of a column name.As soon as I encounter a distinct row I want to break.
I want to do this using a query, something like this :
select case
when ( compare row 1 of A and B
if same continue with row+1
else break
)
if all same, then 1
else 0
end
as result
I am not sure how to loop through rows and break? Is it even possible in sqlite?
EDIT
Table looks like this
-------------------------- -----------------------------------
id | name id | name
-------------------------- -----------------------------------
1 | A 1 | A (same)
2 | C 2 | C (same)
3 | B 3 | Z (different break)
4 | K
Both tables have same structure. I want to just compare the names row by row, to see whether there is any order difference.