Spark SQL select a sub query into a struct - apache-spark-sql

I have an UDF accept a Row which is the row of sub query. My SQL shows below
SELECT
main.id as id,
my_udf(other) as other_content
FROM
main_tabel AS main
LEFT JOIN other_table AS other ON main.id = other.id
However, other is not a valid field in this SQL, and the SQL dose not work. My question is how can I get a Row of the other with all fields in other_table?

Related

How to show all information from sql query

i have a sql query, but i have problem with this, i would like that the sql query show me all the information in both table that i have...
And it works, but when i put a condition in the sentence, i can't have the full info...
With this query, i can see all information of my table, even if i don't have info in some columns... And i need that, but i want to add a condition, and when i add the condition i can't see all.
Result:
Query without condition
SELECT dbo.tblHoras.IdHora, dbo.tblHoras.Hora, dbo.tblHoras.Meta, COUNT(dbo.tblProductos.Serial) as Cantidad
FROM tblHoras full join
tblProductos
ON tblHoras.IdHora = tblProductos.IdHora
GROUP By tblHoras.IdHora, tblHoras.Hora, tblHoras.Meta
ORDER By tblHoras.IdHora;
And this is the query with the condition, and this doesn't bring me all the info.
SELECT dbo.tblHoras.IdHora, dbo.tblHoras.Hora, dbo.tblHoras.Meta, COUNT(dbo.tblProductos.Serial) as Cantidad
FROM tblHoras full join
tblProductos
ON tblHoras.IdHora = tblProductos.IdHora
WHERE tblProductos.ActualFecha = '2017-04-19'
GROUP By tblHoras.IdHora, tblHoras.Hora, tblHoras.Meta
ORDER By tblHoras.IdHora;
Result:
Query with condition
Based on the fact that you are aggregating only by columns in tblHoras, I think you want a LEFT JOIN:
SELECT h.IdHora, h.Hora, h.Meta, COUNT(p.Serial) as Cantidad
FROM tblHoras h LEFT JOIN
tblProductos p
ON p.IdHora = p.IdHora AND p.ActualFecha = '2017-04-19'
GROUP By h.IdHora, h.Hora, h.Meta
ORDER By h.IdHora;
The filtering condition should then be in the ON clause.
Notice that I also introduced table aliases which make the query easier to write and to read.

SQL Server : stored procedure add table content to another table

How can I add the results from a query by parameter to another table using a stored procedure?
Here is my select query text:
SELECT
dbo.PortfolioH.Epic,
dbo.PortfolioH.AlertRatings,
dbo.UserAccounts.Email,
dbo.PortfolioH.UserN
FROM
dbo.PortfolioH
LEFT OUTER JOIN
dbo.UserAccounts ON dbo.PortfolioH.UserN = dbo.UserAccounts.UserN
GROUP BY
dbo.PortfolioH.Epic,
dbo.PortfolioH.AlertRatings,
dbo.UserAccounts.Email,
dbo.PortfolioH.UserN
HAVING
(dbo.PortfolioH.AlertRatings = N'yes')
I want to append to table AlertEmails where dbo.PortfolioH.Epic = #Epic
any help greatly appreciated.
Just use the standard INSERT.....SELECT construct, which takes the data to be inserted from a query, something like this:
INSERT INTO dbo.AlertsEmails (Epic, AlertRatings, Email, UserN, AlertType)
SELECT dbo.PortfolioH.Epic, dbo.PortfolioH.AlertRatings,
dbo.UserAccounts.Email, dbo.PortfolioH.UserN, 1 As AlertType
FROM dbo.PortfolioH LEFT OUTER JOIN
dbo.UserAccounts ON dbo.PortfolioH.UserN = dbo.UserAccounts.UserN
GROUP BY dbo.PortfolioH.Epic, dbo.PortfolioH.AlertRatings, dbo.UserAccounts.Email,
dbo.PortfolioH.UserN
HAVING (dbo.PortfolioH.AlertRatings = N'yes') ;
It just runs the query, but instead of returning a result set, inserts the resulting rows into the table. Note that I've added the AlertType column in the INSERT part and added a fixed value in the SELECT part, as they must match exactly.

SQLite query select all records that does not exist in another table

I am having some problem when trying to perform a SQLite query to get the records which does not exist from another table. Basically I have two database tables:
My exercise table stored all the exercises available whereas the bookedExercise table store the exercises booked by each users. What I am trying to do is, for example if the exercise does exist in the bookedExercise, it should be filtered out.
Here is the SQLite query which I used:
SELECT exercise.exerciseID, exercise.exerciseType, exercise.amout FROM exercise LEFT JOIN bookedExercise WHERE exercise.exerciseID = bookedExercise.exerciseID AND bookedExercise.exerciseID IS NULL
However, it returned me empty records. Any ideas?
Thanks in advance.
If you're fine with not using joins you could use
SELECT * FROM exercise WHERE exerciseID not in (SELECT exerciseID FROM bookedExercise)
When you are using LEFT JOIN, you must put the join condition into the ON clause:
SELECT exercise.exerciseID,
exercise.exerciseType,
exercise.amout
FROM exercise /* !! */
LEFT JOIN bookedExercise ON exercise.exerciseID = bookedExercise.exerciseID
WHERE bookedExercise.exerciseID IS NULL
Your SQL looked okay... I think the problem might be you have a datatype mismatch. You have exercise ID as an integer in one table and text in another.
Also, if you have huge data volumes, you may want to consider an anti-join:
select
e.*
from
exercise e
where not exists (
select 1
from bookedExercise be
where
e.excerciseId = be.exerciseID
)
-- edit --
On second glance, your SQL was not okay. You had your join condition in the where clause. This little change would fix your existing SQL:
SELECT exercise.excerciseId , exercise.exerciseType , exercise.amout
FROM exercise LEFT JOIN bookedExercise on
exercise.excerciseId = bookedExercise.exerciseID
WHERE bookedExercise.exerciseID IS NULL

Query to find sub departments vb.net mssacces 97 sql

I am trying to find subdepartments where they link by description if one is their it should return the id of the one in the table I am using the following query.
SELECT DISTINCT Subdeptcode
,SubDepartment
FROM SkechersPricat
WHERE SubDepartment IN ( SELECT DISTINCT description
FROM SubDept )
AND processed = false
AND SubDeptCode = 0
I need To be able to return the subdeptcode from the sub dept table if one exists if one Doesnt exist create it so I need a query that can account for both condision im using vb.net
Main File looks like the following
Sub Debt table above
For starters lets look at your query
SELECT DISTINCT Subdeptcode
,SubDepartment
FROM SkechersPricat
WHERE SubDepartment IN ( SELECT DISTINCT description
FROM SubDept )
AND processed = false
AND SubDeptCode = 0
you are trying to say that SubDepartment column needs to be in one of the values of description from SubDept table. Unless your SubDepartment matches description exactly it will not return anything.
Proper syntax for your query would be using LEFT OUTER JOIN. By using LEFT OUTER JOIN you are saying to get all results from your left table and only records that match in your right table. Thus you still get all the records from main table even if there is no match in SubDept table.
Your query will be like this
SELECT DISTINCT s.Subdeptcode
,s.SubDepartment
FROM SkechersPricat s
LEFT OUTER JOIN SubDept sd
ON s.SubDepartment = sd.description
WHERE s.processed = false
AND s.SubDeptCode = 0
What I do see in your screenshot that GeminDepartmentID corresponds to DeptCode from SubDept table. I think you are not joining on correct value.

How to use inner join in oracle 11g?

I am running my Oracle query here but it's not working but the same query is working in SQL Server
Here is my query:
SELECT d.dept_code,
d.dept_name,
d.dept_desc,
e.comp_name
FROM dept_master d
inner join comp_master e
ON d.comp_id = e.comp_id
where in dept_master.comp_id value is the same as in Dept_Master table.
The reason you are not getting any result is mainly because of the data
do this check to see if data is available in the tables
select * from dept_master;
select * from comp_master;
and see if both tables have any matching rows, i.e.; at least 1 row has same comp_id in both tables
I hope you will find the answer after doing this exercise
Is comp_id a character field? In that case define it as VARCHAR2 in Oracle. or try trim(d.comp_id) = trim(e.comp_id)
See a demonstration in SQL Fiddle.