Below are 2 collections - Merchants and Tokens.
Tokens is a table that holds tokens for a merchant to use.
Each merchant can have multiple tokens.
Table: Merchants
Columns:
- Id (nvarchar, not null),
- Name (nvarchar, not null),
- PartnerId (nvarchar, not null),
- Group (nvarchar, not null),
- JoinDate (nvarchar, not null)
Table: Tokens
Columns:
- Id (nvarchar, not null),
- InsertDate (DateTime, not null),
- MerchantId (nvarchar, not null),
- TokenType (int, not null),
I am trying to write an SQL query that will fetch merchants that have inactive tokens
that were created after Aug 30, 2021 for merchants that joined after Jan 1, 2020.
Required output: merchant name, merchant join date.
Rows with duplicate data should not be displayed.
I tried to do the following:
SELECT Merchants.Name, Merchants.JoinDate
FROM Merchants
WHERE Tokens.TokenType=1 AND Tokens.InsertDate > 8/30/2021 AND Merchants.JoinDate > 1/1/2020
LIMIT 1;
However, this doesnt seem to work.
I am new to SQL and I think I should use JOIN in the WHERE clause,
but I can`t seem to make it work.
Can someone please assist?
JOINin the where clause, but you can do one before. That will bring theTokenstable into scope, which will let you filter in its columns in yourWHEREclause.LIMIT 1doesn't delete duplicate data, it shows you only the first record. To avoid duplicates you need to add 'distinct' in select statement