I need to combine two results from a SQL select query - sql

I Have two Select queries that get results for two tables with the same column names.
SELECT
labels.langjrd,
labels.id,
labels.lcphrase
FROM
labels
WHERE
labels.langjrd LIKE 'FRE%'
;
and
SELECT
labels.langjrd,
labels.id,
labels.lcphrase
FROM
labels
WHERE
labels.langjrd LIKE 'ENG%'
;
When I run the query I want to put all the results into one table. I read about union query but when I tried it, it didn’t work. I don't want to overwrite all the duplicate data I just want to have the second select results be added to the bottom of the first select results.

With UNION ALL you can combine results from two queries like this:
SELECT
labels.langjrd,labels.id,labels.lcphrase
FROM
labels
WHERE
labels.langjrd LIKE 'FRE%'
UNION ALL
SELECT
labels.langjrd,labels.id,labels.lcphrase
FROM
labels
WHERE
labels.langjrd LIKE 'ENG%';
You read about UNION which does a similar thing, but it filters out duplicate results. So it's similar as UNION ALL, but not the same.
But since you are actually querying the exact same columns from the same table, you can just put the two conditions in the where clause and split them using or. That way you will get all records that match either of the conditions.
SELECT
labels.langjrd,labels.id,labels.lcphrase
FROM
labels
WHERE
labels.langjrd LIKE 'FRE%' OR
labels.langjrd LIKE 'ENG%';

Related

How to merge data of two tables with different column name in Big Query

How can I get final output based on table 1 and table 2 in Big Query
Table 1
Table 2
Final Output
You can use union all. If the columns are in the same order:
select *
from table1
union all
select *
from table2;
In general, though, it is better to list out the column names instead of using *. Note that in the result set, the names from the first select are used for the result set.

Combining with Union Tables in SQL (values populating incorrectly)

I am trying to perform a Union on 2 tables in SQL, both with columns are MultiNational, CompanyDescrpition, GDPRojectID, Company_Code, 2019TTV, 2020TTV and 2021TTV. These two tables populate values into 2019TTV, 2020TTV and 2021TTV when I make the two separate tables, however when I perform the union, it turns into 3 columns all called TTV and then it just fills with the year number instead of the value that should be there.
Any idea why this is happening?
Here is how I am performing the union.
select multinational, companydescription, GDProjectID, company_code, 2021TTV, 2020TTV, 2019TTV FROM #TTV Union select multinational, companydescription, GDProjectID, company_code, 2021TTV, 2020TTV, 2019TTV FROM #TTVUK
If you have column names starting with numbers, you should contain them in square brackets like [2021TTV].
EDIT: To clarify, if column names start with anything besides letters, you should contain the column name in square brackets or quotes.

Any easier way to group by individual columns in Hive/Impala?

I need to output report of users by their age, gender, education, income, etc from our database. However, there are about 40 variables. It seems just silly to group by each variable one bye one but I'm not aware of other ways and I don't know how to write UDF to solve it yet. I'd appreciate your help.
It's not that complicated but it does come up a lot in daily work. My work environment is Hive/Impala.
We cannot implement 'Group By' task on input rows in UDF , UDAF or UDTF.
UDF takes in a single input row and output a single output row.
UDAF just does Aggregations on one column, but not by Grouping rows.
UDTF transforms a single input row to multiple output rows.
Only possible solution is to write multiple Queries and Combine them using UNION ALL and display/insert into table
Sample Query:
SELECT *
FROM
(
SELECT COUNT(column1),column1 FROM table GROUP BY column1
UNION ALL
SELECT COUNT(column2),column2 FROM table GROUP BY column2
UNION ALL
SELECT COUNT(column3),column3 FROM table GROUP BY column3
) s

Combine results of two table in SQL

i have two tables one contain computer_science_book and Mechanical_engineering_book i want the result which contail all books of computer_science_book and Mechanical_engineering_book .
give me suggetions.
You can use union all for that:
select * from computer_science_book
union all
select * from Mechanical_engineering_book
You can use the UNION operator:
select columns from computer_science_books
UNION
select columns from mechanical_eng_books
Note that the structure of the results from computer_science_books needs to match the structure of results from mechanical_eng_books. The reason is that, as asked for, there is only one result table. There is also the UNION ALL operator. The difference is that UNION removes duplicates whereas UNION ALL returns all combined rows.

Enter multiple rows explicitly in single select statement

I am testing a query and want to use SELECT to get some initial data like so:
SELECT 1,2,3
UNION
SELECT 2,3,4
Is there a syntax to fold these two selects into one or do I have to use a UNION statement for each row?
You can use values clause
select t.* from (values(1,2,3), (2,3,4)) as t(col1,col2,col3)
If you want to display the result as one result set then you have to use union/union all