SQL retrieve results based on String matching from two tables - sql

I have two tables say A and B. A has many columns like Date, Customer, Price, typedesc etc. B has only one column typedesc. I want to retrieve rows in A whose typedesc is in B. So I wrote
select * from A where typedesc in (select typedesc from B)
I got 0 rows in result. So i tried
select A.* from A inner join B on A.typedesc=B.typedesc
Still I am getting 0 rows in result
I manually checked the typedesc column in both tables, there are matching entries. typedesc contains strings and it is of type varchar2
Here are the sample tables
A
DATE CUSTOMER TYPEDESC SKU PRICE
02/01/2013 4567 CREAM CORDIALS 1234 23
03/01/2013 3256 U.S. BRANDY 3322 10.5
B
TYPEDESC
CREAM CORDIALS
FIRE WHISKY

Try to use the TRIM function before comparison to avoid the mismatch due to extra spaces.

Related

Bigquery join 2 tables with id concated from 4 columns and create a new table dynamically

I have two tables in Bigquery from two different data sources, lets say x and y. I want to join these two tables on os_name, tracker_name, date, country columns. For that i am using concat function and joining like this:
full outer join x on concat(x.date,x.os_name,x.tracker_name, x.country) = concat(y.date,y.os_name,y.tracker_name,y.country_code)
as a query result common columns also gets duplicated. like in the result there is os_name and os_name_1, country_code, country_code_1 etc. columns. I don't want that. Final columns should be as in the example below in Final Table Schema.
I want to return all records from both sides. For example if there is no match in table y
y_install, and y_purcase will be 0, and vice versa.
X TABLE SCHEMA:
os_name,
tracker_name,
date ,
country
install
purchase
Y TABLE SCHEMA:
os_name,
tracker_name,
date,
country,
y_install,
y_purchase
Final Table Schema required:
os_name,
tracker_name,
date ,
country
install
purchase,
y_install,
y_purchase
I am going to schedule the query and write results to destination table at given interval.
Can you help me out with this query.
Regarding the final table, I don't understand whether you want to return first NON NULL result or whether you want to have e.g. an array which will contain both results from both tables in case both tables a valid value. In my sample table, do you want row 1,2 (actually the same thing) or 3?
row_number
x_install
y_install
final_table_install
1
23
50
23
2
NULL
50
50
3
23
50
[23,50]
It comes out that What I wanted to use was union all. First, I added the non-common columns to the two tables so that the schemas of the two tables are equal. So I was able to vertically merge tables using union all. Thanks for trying to help out anyway.

How to join on multiple conditions in SQL?

I have two datasets, and I need to be able to join on null column values
*there are many null values in the 'Model' column for this table
Customer
Brand
Model
Bill
Nike
Dunk
Kayla
Adidas
Shoe 2
Max
Nike
2)
SaleID
Customer
Brand
Model
1234
Mike
Puma
X3
5678
Bill
Nike
Dunk
7433
Max
Nike
I want to join entire rows from table one with table 2, where essentially all three values in a row from table 1 act as a single record to be joined with table 2. (Bill, Nike, Dunk) is one value essentially
So far I have tried:
create table blank as
select columns,
columns,
columns,
from table 1 as x
left join table 2 as y
on x.customer=y.customer and x.brand=y.brand and x.model=y.model
;
quit;
The problem I am running into with this code is the join only includes rows where the 'Model' column is not null. There are many sales IDs with null 'Model' and I would like to be able to join these records.
For example, the final output of joining these tables with my code is:
| SaleID | Customer | Brand | Model |
| 5678 | Bill | Nike | Dunk |
Where I would like for there to be a record for Max, but since there is a null value in that column in that record it is not joined
SAS itself would match those records (unless the values of MODEL in one or the other dataset is not actually all blanks but includes some other invisible characters) because SAS uses strictly binary logic. A=B is either TRUE or FALSE.
But most external database systems (Oracle, etc.) use TRI-level logic for comparisons. A=B will be neither TRUE nor FALSE when either A or B is a NULL value.
So you need to explicitly account for the NULL values in the test condition.
create table want as
select y.saleid
, x.*
from table1 x
left join table2 y
on x.customer=y.customer
and x.brand=y.brand
and (x.model=y.model or (x.model is null and y.model is null))
;

Postgres SQL - Concatenate Tables with different columns

I've looked up ways to solve my problem here on SO and other sources, but nothing i have tried worked, so here i am.
I need to concatenate two tables with different columns, as it follows:
(data is just a represetantion, not my actual data and i'm using Postgres SQL)
Table_1:
name price id location
test 1.0 7 Canada
Table_2
name store sale price location
testing local_store 54 2.0 US
My actual tables have over 30 rows each, but the resulting table i need would look like this:
Table_concat:
name price store sale id location
test 1.0 null null 7 Canada (row from Table_1)
testing 2.0 local_store 54 null US (row from Table_2)
Basically, i need to put one table on top of another and when the columns do not match, a null value should apear. Can anyone help me?
I can provide further explanation if necessary.
Try this query:
SELECT
name, price, null AS store, null AS sale, id, location
FROM Table_1
UNION
SELECT
name, price, store, sale, null AS id, location
FROM Table_2

Left join returns null, even though there is a matching value

2 tables. I match on column ProductIdentifier which is present in both tables. The left table is a big table with many records and ProductIdentifier is not unique and the same value will be present multiple times. The right table has only limited records and a maximum of 1 matching value (this column is unique).
I execute the following query.
select distinct `upe`.`ProductIdentifier` AS `ProductIdentifier`,
`pmt`.`ProductIdentifier` AS `PMT_ID`
from `prices`.`unassigned_price_entries` `upe`
left join `prices`.`product_match_table` `pmt`
on (`pmt`.`ProductIdentifier` = `upe`.`ProductIdentifier`)
where `upe`.`ProductIdentifier` like '%Brand%'
Basically, all works well, except for one thing. The result is like this:
ProductIdentifier | PMT_ID
----------------------------
Brand A | Brand A
Brand A | Brand A
Brand B | Brand B
Brand B | NULL
I don't understand. It can match Brand B obviously but doesn't do it the second time. It does however for A.
This would occur if the two values for upe.ProductIdentifier did not have the same value. They might look the same but be different, for various reasons:
Leading or trailing spaces.
Hidden characters in the string.
Look-alike characters in some collations.
If you use where upe.ProductIdentifier = 'Brand B', you will probably get only one of the rows (there is a possibility that neither would match).

What is the correct way to query two columns in a table using AND OR in SQL?

I have two columns in my products table, category1, and category2. In my categories table I have four different categories, if a product falls under that category, the category number comes under the category1 column, if it comes under another category at the same time, that category is entered into the category2 column, a product does not come under more than two category types simultaneously.
Here is an example:
productID | category1 | category2
1 | 1 |
2 | 1 | 2
3 | 2 |
So category type 1 will always be in the category1 column, and if the same product comes under category type 1 and 2 as shown above in product id 2, then this causes me problems when querying all products that are of category type 2.
Here is my first query where I want to retrieve all the products which come under category type 1:
SELECT * FROM products WHERE category1 = 1
My second query where I want to retrieve all the products which come under category type 2:
SELECT * FROM products WHERE category1 = 2 AND OR category2 = 2
I know the second query is wrong, I just wrote it as I need it to work.
I cannot figure out how to query two columns with one query, only by writing two separate queries, any suggestions is appreciated. I am using SQL Server 2008 so special SQL functions may not be supported.
I would use the following statement:
SELECT * FROM products WHERE category1 = 2 OR category2 = 2
This will select all rows that have value 2 in one (or both) of the columns. Furthermore you can use it for any value (changing the value, of course) including value 1. This makes it easy to create a prepared statement in your client software (if you have one) that uses just one variable.
It sounds like the following is what you want:
Select
*
From
Products
Where
Category1 = 2 or
Category2 = 2
However I think you may want to reconsider your table design. Whenever I see multiple simliar columns labels with numeric suffixes, I consider it a design smell.
An alternative is to create a seperate ProductCategories table with 2 columns: ProductID and Category. Your PK would be a compound key of both. If you used this design, the SQL you'd need to find all category 2 product would be:
Select
Products.*
From
Products
Inner Join
ProductCategories
On
Products.ProductID = ProductCategories.ProductID
Where
ProductCategories.Category = 2