Merge columns from two SQL query results with different number of columns - sql

I've two tables which have following columns:
Now I want have a SQL query which gives me an output as follows:
I wrote already some SQL code, but this doesn't work because the union operator needs the same number of columns with the same type. Have a look.
SELECT *
FROM
(SELECT User_tbl1.Username, User_tbl1.Surname, User_tbl1.Givename
FROM User_tbl1
UNION
SELECT User_tbl2.User_PK
FROM User_tbl2)
Can someone help me to bring my SQL query working, so that it will output a hyphen at surname and givename if the record is located in table "User_tbl2"?
Thanks a lot!

As simple as:
SELECT User_tbl1.Username, User_tbl1.Surname, User_tbl1.Givename
FROM User_tbl1
UNION
SELECT User_tbl2.User_PK, '-', '-'
FROM User_tbl2

Related

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

MS Access SQL how to display the unique result

I am new with the MS access SQL and the following is what I would like to do
in my database table
I have the following data
basically no duplicate, I tried "Distinct" but given my result is not one column only so I can't do that. I research online and look like it might need to use inner join but I only have one table only.
Appreciate if anyone can help me up..
Thanks
Regards,
Marc
You should be able to run the following query:
SELECT DISTINCT name, birthday
FROM [table];
select DISTINCT column1, column2, ...
from TABLENAME;
Replace columns and table name with respective values.
like this
select DISTINCT name, birthday from TABLENAME;

how to search for multiple strings in multiple columns in ORACLE SQL where clause

My situation - I have to two tables with several columns each, and I need to find certain strings in certain columns in those two tables. For example the search string could be 'ExampleString1' , 'ExampleString2%' etc around 20 strings and about 5 - 6 columns in each table.
I m using the following to find atleast one string in the multiple columns, but this even is not working.
select * from table1 a where upper('ExampleString1%') in (a.Column1, a.column2, a.column3)
Although I can do some basic sql queries, I m not that acquaint with sql. I like to know the solution or any material I can study to get to solution.
Thanks
rK
You can combine all required fields and run a check on that:
select *
from table1 a
where NVL(upper(a.Column1),'')||NVL(upper(a.column2),'')||NVL(upper(a.column3),'') like upper('ExampleString1%')
The use of upper() can be avoided here by using REGEXP_LIKE
SELECT * FROM TABLE A WHERE
REGEXP_LIKE(COLUMN1 || COLUMN2 || COLUMN3, '<search expression>', 'i')

Need SQL with subquery to get distinct values for VBA code

I have a table BAR_DATA with two fields: LongDate, Time. Both are long integers. No Access Date/Time involved here.
For each distinct LongDate value there are hundreds of records, each with Time value which may be distinct or duplicate within that LongDate.
I need to create an SQL statement that will group by LongDate and give me a count of distinct Times within each LongDate.
The following SQL statement, (built by an Acess query) does NOT work (some LongDates are omitted):
Query A
SELECT DISTINCT BAR_DATA.LongDate, Count(BAR_DATA.Time) AS CountOfTime
FROM BAR_DATA
GROUP BY BAR_DATA.LongDate
HAVING (((Count(BAR_DATA.Time))<>390 And (Count(BAR_DATA.Time))<>210));
However, if I use Query B to reference Query DistinctDateTime, it does work:
Query B
SELECT DistinctDateTime.LongDate, Count(DistinctDateTime.Time) AS CountOfTime
FROM DistinctDateTime
GROUP BY DistinctDateTime.LongDate
HAVING (((Count(DistinctDateTime.Time))<>390 And (Count(DistinctDateTime.Time))<>210));
Query DistinctDateTime
SELECT DISTINCT BAR_DATA.LongDate, BAR_DATA.Time
FROM BAR_DATA;
My problem:
I need to get Query B and Query DistinctDateTime wrapped into a single SQL statement so I can paste it into a VBA function. I presume there
is some subquery techniques, but I have failed at every attempt, and find no pertinent example.
Any help will be greatly appreciated. Thanks!
Subquery your distinct table inside and perform your aggregates outside until you get the desired result:
SELECT DistinctDateTime.LongDate, Count(DistinctDateTime.Time) AS CountOfTime
FROM
(
SELECT DISTINCT BAR_DATA.LongDate, BAR_DATA.Time
FROM BAR_DATA
) AS DistinctDateTime
GROUP BY DistinctDateTime.LongDate
HAVING (((Count(DistinctDateTime.Time))<>390 And (Count(DistinctDateTime.Time))<>210));

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.