SQLite C++ Compare two tables within the same database for matching records - c++

I want to be able to compare two tables within the same SQLite Database using a C++ interface for matching records. Here are my two tables
Table name : temptrigrams
ID TEMPTRIGRAM
---------- ----------
1 The cat ran
2 Compare two tables
3 Alex went home
4 Mark sat down
5 this database blows
6 data with a
7 table disco ninja
++78
Table Name: spamtrigrams
ID TRIGRAM
---------- ----------
1 Sam's nice ham
2 Tuesday was cold
3 Alex stood up
4 Mark passed out
5 this database is
6 date with a
7 disco stew pot
++10000
The first table has two columns and 85 records and the second table has two columns with 10007 records.
I would like to take the first table and compare the records within the TEMPTRIGRAM column and compare it against the TRIGRAM columun in the second table and return the number of matches across the tables. So if (ID:1 'The Cat Ran' appears in 'spamtrigrams', I would like that counted and returned with the total at the end as an integer.
Could somebody please explain the syntax for the query to perform this action?
Thank you.

This is a join query with an aggregation. My guess is that you want the number of matches per trigram:
select t1.temptrigram, count(t2.trigram)
from table1 t1 left outer join
table2 t2
on t1.temptrigram = t2.trigram
group by t1.temptrigram;
If you just want the number of matches:
select count(t2.trigram)
from table1 t1 join
table2 t2
on t1.temptrigram = t2.trigram;

Related

Informatica Cloud Data Integration - find non matching rows

I am working on Informatica Cloud Data
Integraion.I have 2 tables- Tab1 and Tab2.The joining column is id.I want to find all records in Tab1 that do not exist in Tab2.What transformations can I use to achieve this?
Tab1
id name
1 n1
2 n2
3 n3
Tab2
id
1
5
6
I want to get records with id 2 and 3 from tab1 as they do not exist in tab2
You can use database source qualifier overwrite sql
Select * from table1 where id not in ( select id from table2)
Or else you can use informatica like below.
Do a lookup on table2, on join condition on id.
In exp transformation, create a flag
out_flag= iif(isnull (:lkp(id)),'pass','fail')
Put a filter next and keep the condition as out_flag= 'pass'
Whole map should be like this
Lkp
|
Sq --exp|-----> fil---tgt

data model in Power BI with multiple tables each has an FK

Assume I have the following tables:
Table 1:
Prj_id, name
1 , prj1
2 , prj2
3 , prj3
Table 2
prj_id, cost, year
1 ,100 ,1999
1 ,200 ,2000
1 ,300 ,3000
2 ,150 ,1998
Table 3
Prj_id, manager
1 , xxx
1 , yyy
2 , xxx
3 , zzz
Now my question is:
Shall I connect all tables together in the model (relation tab) of BI, if I have all the attributes as filters on the page
i.e.
Table 1 to 2 ->Both direction
Table 1 to 3 ->Both direction
Table 2 to 3 ->Both direction
or shall I need to only join tables as follows:
Table 1 to 2->Both direction
Table 1 to 3->Both direction
That highly depends on what your final goal is.
However I would create a relationship between this tables. As I looks, these data coming from a database with a good structure, so why joining every together again.

Power BI: Grouping results in a many to many relationship

I am new to power BI, I imported 3 tables from SQL server to Power BI Desktop: 2 main tables and 1 for many to many relationship (bridge), and I need to get results from the second main table based on grouping from the first and second main tables.
Tables are: Customers, Orders, OrderCustomers (bridge table). Orders table has a SalesChannelId field, and I need to get each customer's orders grouped by sales channels, and the percentage of all the customer's orders
I already achieved this with a SQL query (which is the thing I am good at):
select
Customers.FirstName,
all_orders.orders_count,
SalesChannels.Name as SalesChannel,
COUNT(OrderCustomers.OrderId) as SaleChannelOrdersCount,
cast (((COUNT(OrderCustomers.OrderId) * 100) / all_orders.orders_count) as nvarchar(3)) + '%' as SaleChannelOrdersPercent
from
Customers
inner join OrderCustomers
on OrderCustomers.CustomerId = Customers.Id
inner join Orders
on Orders.Id = OrderCustomers.OrderId
inner join SalesChannels
on SalesChannels.Id = Orders.SalesChannelId
inner join
(select
Customers.id as customer_id,
COUNT(OrderCustomers.OrderId) as orders_count
from
Customers
inner join OrderCustomers
on OrderCustomers.CustomerId = Customers.Id
where 1=1
group by Customers.id) as all_orders
on all_orders.customer_id = Customers.Id
where 1=1
group by Customers.id, Customers.FirstName, SalesChannels.Name, all_orders.orders_count
order by Customers.id, SalesChannels.Name
With this query I get results like these:
FirstName orders_count SalesChannel SaleChannelOrdersCount SaleChannelOrdersPercent
Adam 9 Online 1 11%
Adam 9 Counter 8 88%
Henrik 3 Counter 3 100%
Mary 15 Online 3 20%
Mary 15 Counter 12 80%
How to achieve the same results using Power BI?

How to get latest record for matching ID and date in Power BI Query Editor

I have two tables:
Table A, which indicates where my truck is for each day,
Date Truck Region
5/20/2018 1014 NY
5/21/2018 1014 NJ
and Table B (which contains when my truck inspection was done for each day). Sometimes there may be more than one inspection record, but I only need the last one by truck by day. As you can see, I added a rank column. Truck 1014 has two records for 5/20/2018, but the last one gets ranked as 1 (I will filter the table by 1).
Date Time Truck Rank
5/20/2018 5/20/18 9:00 AM 1014 2
5/20/2018 5/20/18 2:00 PM 1014 1
5/21/2018 5/21/18 2:50 PM 1014 1
I want to merge those two tables together. The reason I ask how to do it in the query editor is that, in the relationship view, you cannot create a relationship on two columns. For example, in my example, I want to join data by date and by truck number, which I cannot. What is the right direction for this situation?
In the query editor, you can use the Merge Queries button under the Home tab.
(You'll need to hold down the Ctrl key to select multiple columns.)
Once you've merged, just expand the columns from Table B that you want to bring over (e.g. Time and Rank). If you did not filter Rank = 1 before merging, you can include it when expanding and filter afterward.
Note that you can also use the LOOKUPVALUE DAX function outside of the query editor. As a new column on Table A:
Time = LOOKUPVALUE('Table B'[Time],
'Table B'[Date], [Date],
'Table B'[Truck], [Truck],
'Table B'[Rank], 1)

How to check how many rows is in table using SOCI and C++?

Simple question. I have a table Person. I have in it two rows:
1 Joe Doe joe.doe#ymail.com
2 Vivien Doe v.doe#gmail.com
How to write a SOCI statement, which will tell me (return) how many rows I have in my table? (Here, in my example, I have 2 rows).
To get the number of rows in an SQL table use the count(*) function like this:
select count(*) from Person
To be more specific - to get the number into C++ variable use:
int count;
sql << "select count(*) from person", into(count);