You have a list of 2.0 million IDs in Table A. You have another list of 3.5 million IDs in Table B. Some customer IDs show up multiple times in each table and some IDs show up in both tables. Which of the following would you use to create (in one step - no subqueries) a Table C that contains a list of distinct (no duplicates) customer IDs present in either Table A, Table B, or both?
Union, Union ALL, Outer Join, Union Join-?
Union is a SET operation (in SQL and otherwise in mathematics in the field of set theory). A set is a collection of distinct items. Like a list that does not contain any duplicates. A UNION of sets is the combination of all the distinct items in one or more lists.
So in SQL, the UNION function will combine all of its parameters and return a SET of unique values in the combined list.
Related
I have a table of models with columns
Both columns are of a varchar type that contains an array of strings.
What I need to achieve here is to get all tags separately, without dups and in specific order. First should be system_tags in alphabetical order, then tags in alphabetical order as well
SELECT
unnest(system_tags) as tag_name
FROM
"models"
left join projects on projects.id = models.project_id
where projects.is_public = true
union
SELECT
unnest(tags)
FROM
"models"
left join projects on projects.id = models.project_id
where projects.is_public = true
I got as far as to get all separated tags without duplications, but can I order them one by one with union?
So I can get result as on the first picture instead as it on the second
Use a union query to combine multiple queries into a single result. Sometimes you might want to list the records from one table or query with those from one or more other tables to form one set of records - a list with all the records from the two or more tables. This is the purpose of a union query in Access.
When I query I do not get match list side by side and database reference fails, why?
I tried using one connection and two different schemas, first schema name table has same reference details with second table column, and I would like to compare side by side what equals exactly from rw.reference table 1, with table 2 qt.ref_number because they should match between both. I only get output data from top first query with rd.reference and second table does not list anything with, qt.ref_number, why postgresql does not cross database reference?
Query:
Select distinct
rd.reference,
region1_scorecard
From local_dev.user.rawdata rd
Inner join customerid as id
on rd.site=id.site
union all
Select distinct
qt.ref_number,
region2_decision
From local_dev.account.quote qt
Inner join rep_id id
on qt.application=id.application
Order by rd.reference
So I have a postgresql table that has userids who created a party in one column and an array of userids whom have joined their party.
im wondering if there is any way to return a list of the distinct userids from both the column of their userid and the array, so the distinct list of userids who has either created or joined a party?
If I understand correctly, you can use union:
select created_userid as userid
from t
union -- on purpose to remove duplicates
select joined_userid as userid
from t cross join lateral
unnest(t.joined_userids) joined_userid;
I have two separate tables in my access database, which both use a third table as the reference for one particular field on each table. The data is entered onto the different tables by separate forms. Then I have several queries that then reference those particular fields that count and show unique values. Those queries show the actual values, then I created an sql query that does the same thing, only it shows the reference ID instead of the value in the actual field.
table ODI----------table CDN----------reference table
id RHA---------id CHA----------------id HA
1 blank----------1 radio---------------1 internet
2 internet-------2 tv------------------2 radio
3 referral-------3 radio---------------3 referral
4 tv-------------4 blank---------------4 repeat customer
5 blank----------5 internet------------5 tv
6 internet-------6 referral------------6 employee
7 referral-------7 referral------------7 social media
this is the code I am trying to make work.
SELECT m.[Marketing Results], Count(*) AS [Count]
FROM (SELECT RHA as [Marketing Results] FROM ODI
UNION ALL
SELECT CHA as [Marketing Results] FROM CDN) AS m
GROUP BY m.[Marketing Results]
HAVING (((m.[Marketing Results]) Is Not Null))
ORDER BY Count(*) DESC;
and what my desired result is,
Marketing Results--Counts
referral------------------4
internet------------------3
radio---------------------2
tv------------------------2
Lookup fields with alias don't show what is actually stored in table. ID is stored, not descriptive alias. Lookup alias will carry into regular queries but Union query only pulls actual stored values. At some point need to include reference table in query by joining on key fields in order to retrieve descriptive alias. Options:
in each UNION query SELECT line, join tables
join UNION query to reference table
join aggregate query to reference table
Most experienced developers will not build lookups in table because of confusion they cause. Also, they are not portable to other database platforms. http://access.mvps.org/Access/lookupfields.htm
Table A contains basic customer information including account number. Each valid account number appears in exactly one row in table A.
Table B contains information about which flags are set for which customers.
Columns in Table B are account number, flag key, flag value. Most account numbers appear in multiple rows in Table B, one for each flag that is set on that account. However some account numbers do not have any flags set and do not appear at all in table B. Flags do not have default settings and can be undefined. If there is no row in table B for a particular account number and flag key combination, that flag is undefined on that account.
The only flag we care about for this question is flag key Foo. There are many customers who have Foo set to something and many who do not have it set at all.
Goal: Select all the customer info from table A for all customers that do not have flag Foo set at all.
My current solution that works is doing 2 queries and then processing the results. One query selects all rows from table A. The other query selects all rows from table A join table B on account number where tableB.FlagKey=Foo.
Then some code I wrote just iterates through the results of the table B query and generates a list of account numbers. Then it iterates through the results of the table A query and just keeps rows with account numbers that don't appear in the list of account numbers I built.
This works fine but I feel like this "should" be possible to do in one DB query, but I can't figure out how to write that query. I'm not very experienced in writing DB queries.
Try this-
SELECT *
FROM TABLE_A
INNER JOIN TABLE_B
ON A.ACC_NO = B.ACC_NO
WHERE B.FOO <> 'bar'
Use not exists:
select a.*
from a
where not exists (select 1 from b where b.flagkey = 'foo');
This assumes that "not set at all" means that the key is not in the second table.