How would I do the following in SQL:
select distinct(territory_id) from main_territorypricing
"minus"
select distinct(territory_id) from main_territorypricing where store_url is not null
Basically, I want all territory_id
s that are contained in the top line that are not contained in the second line.
The set difference operation returns all the rows in one table not in another. You can do this with not exists. For example: select colour, shape from your_brick_collection ybc where not exists ( select null from my_brick_collection mbc where ybc. colour = mbc.
Learn Python + JavaScript + Microsoft SQL for Data science The SET operators available in Oracle 11g are UNION,UNION ALL,INTERSECT,and MINUS. The UNION set operator returns the combined results of the two SELECT statements.
SQL Server DIFFERENCE() Function The DIFFERENCE() function compares two SOUNDEX values, and returns an integer. The integer value indicates the match for the two SOUNDEX values, from 0 to 4. 0 indicates weak or no similarity between the SOUNDEX values.
SQL provides set operators to compare rows from two or more tables or to combine the results obtained from two or more queries to obtain the final result. These operators are used to join the results of two (or more) SELECT statements.
If you want to do as you said:
select distinct territory_id
from main_territorypricing
where territory_id not in (
select territory_id from main_territorypricing where store_url is not null
)
But based on the logic of what you have described, the easier solution is:
select territory_id
from main_territorypricing
where store_url is null
This, is, provided that you have only these two fields (territory_id and store_url) and the rows are unique.
Otherwise, there is another tricky solution:
select territory_id
from main_territorypricing
group by territory_id
having (max(store_url) is null)
select distinct(territory_id) from main_territorypricing
where
territory_id in
(select territory_id from main_territorypricing)
and territory_id not in
(select territory_id from main_territorypricing where store_url is not null)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With