Using output of one sql query into another - sql

I have 2 SQL queries in single line as follows:
SELECT * FROM (SELECT NameCode,Name FROM tblNames) AS X, (SELECT SUM(Mo+Tu) FROM tblFieldDays WHERE tblFieldDays.NameCode =36)
The first query i.e. (SELECT NameCode,Name FROM tblNames) gives a list of users.
Now I want to calculate sum of Mo+Tu i.e. SUM(Mo+Tu) for each user generated by first query.
i.e. I want to provide NameCode generated in first query instead of current 36 value which static just for example
I also tried to use IN statement as follows:
SELECT * FROM (SELECT NameCode,Name FROM tblNames) AS X, (SELECT SUM(Mo+Tu) FROM tblFieldDays WHERE tblFieldDays.NameCode IN (X.NameCode)) AS Y
But didnt work.
Can anyone help?
Thanks.

SELECT NameCode,
Name,
UserFieldDays = SUM(fieldDays.Mo + fieldDays.Tu)
FROM tblNames users
JOIN tblFieldDays fieldDays ON users.NameCode = fieldDays.NameCode
GROUP BY users.NameCode, users.Name

This is probably what you are looking for:
SELECT NameCode, Name,
(SELECT SUM(MO + TU)
FROM tblFieldDays Y
WHERE Y.NameCode IN (X.NameCode))
FROM tblNames X;
This statement selects all names and code from your table tblNames and adds the sum with a sub select.
Check out this Fiddle.
Hope this helps ... Cheers!

Related

Change duplicate value in a column

Can you please tell me what SQL query can I use to change duplicates in one column of my table?
I found these duplicates:
SELECT Model, count(*) FROM Devices GROUP BY model HAVING count(*) > 1;
I was looking for information on exactly how to change one of the duplicate values, but unfortunately I did not find a specific option for myself, and all the more information is all in abundance filled by deleting the duplicate value line, which I don't need. Not strong in SQL at all. I ask for help. Thank you so much.
You can easily use a Window Functions such as ROW_NUMBER() with partitioning option in order to group by Model column to eliminate the duplicates, and then pick the first rows(rn=1) returning from the subquery such as
WITH d AS
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY Model) AS rn
FROM Devices
)
SELECT ID, Model -- , and the other columns
FROM d
WHERE rn = 1
Demo
use exists as follows:
update d
set Model = '-'
from Devices d
where exists (select 1 from device dd where dd.model = d.model and dd.id > d.id)
After the command:
SELECT Model, count (*) FROM Devices GROUP BY model HAVING count (*)> 1;
i get the result:
1895 lines = NULL;
3383 lines with duplicate values;
and all these values are 1243.
after applying your command:
update Devices set
Model = '-'
where id not in
(select
min(Devices .id)
from Devices
group by Devices.Model)
i got 4035 lines changed.
if you count, it turns out, (3383 + 1895) = 5278 - 1243 = 4035
and it seems like everything fits together, the result suits, it works.

Only select one record if there are more than one of only certain same value of record

In the sql, i wanted all the record but for value RM in column MCCU occurs twice..but i'm in a situation that cannot distinct it because the value of misc of both value are not same..
How can i make if MCCU have more than one RM and then only select the one that have higher position in column POSI and in the misc column, add up their two value together. Hope idea to solve it. Thank you very much!
This is my sql statement
select * from Oclaimc Where cono='NP' and CLNO='7150000032'
There is no column names misc in your image. I am assuming you need to sum gamntMisc and gttlMisc.
So try this. Add other columns when needed.
select max(CONO) as CONO,max(CLNO) as CLNO,max(posi) as posi,MCCU,
sum(gamntMisc) as totalgamntMisc,sum(gttlMisc) as totalgttlMisc from Oclaimc
where cono='NP' and CLNO='7150000032'
group by mccu
Note: Query will fail if you remove the where clause. If you need this result for each combination of cono and clno then change the group by clause to
group by cono,clno,mccu
This may help:
select *
from oclaimc
where cono = 'NP'
and clno = '7150000032'
and mccu <> 'RM'
union
select *
from (select *
from oclaimc
where cono = 'NP'
and clno = '7150000032'
and mccu = 'RM'
order by posi)
where rownum = 1

How to do an In Statement with a sub query returning 2 columns and one of the columns is a Count

I have this query:
SELECT *
FROM GUITARS.FENDER
WHERE FENDER.GUITARTYPE IN (
SELECT GUITARTYPE,Count(*)
FROM GUITARS.GUITAR_TYPE
WHERE GuitarColor = 'RED'
Group By GUITARTYPE
Having Count(*) = 1)
Basically I want to make sure I am only checking the Guitartypes that don't have duplicates with a count. The issue is the IN is only checking for 1 column, but i need the count(*)in there for instances of more than one guitar type. Is there a way to make this query work, or possible another way around doing the count.
You don't need to have the count() returned in the select statement, having the group by and the count() is sufficient.
SELECT *
FROM GUITARS.FENDER
WHERE FENDER.GUITARTYPE IN (
SELECT GUITARTYPE
FROM GUITARS.GUITAR_TYPE
WHERE GuitarColor = 'RED'
Group By GUITARTYPE
Having Count(*) = 1)
Adding the code so it looks right.

Only a single result allowed for a SELECT that is part of an expression

I have the following SQL statement. It's throws the following error: "Only a single result allowed for a SELECT that is part of an expression". The goal of my sql statement is to get the name of the employee who made the 'cheapest' bribe.
The part between the brackets return the employee_id and the money it costs a day (of the relative cheapest bribe). These are two results while I only want the employee_id. So I just want to use the MIN part to get the right employee_id. How can I do this?
SELECT Voornaam, Achternaam
FROM Medewerker m JOIN
(
SELECT Medewerker_id
FROM Steekpenning
ORDER BY -1*Bedrag/(julianday(Begindatum) - julianday(Einddatum))
limit 1
) s
on m.Medewerker_id = s.Medewerker_id;
EDITED the answer. How can I expand this query to only show the bribes started this month? I think I need to use something like this? (julianday(Begindatum) - julianday('now')) > 31 but where?
Regards.
Cas
I think the following will work in SQLite:
select Firstname, Surname
from Employee e join
(select employee_id
from bribe
order by -1*Amount/(julianday(Startdate) - julianday(Enddate))
limit 1
) b
on e.employee_id = b.employee_id;

How to use min() in where/having clause (to avoid subquery) in Hive/SQL

I have a large table of events. Per user I want to count the occurence of type A events before the earliest type B event.
I am searching for an elegant query. Hive is used so I can't do subqueries
Timestamp Type User
... A X
... A X
... B X
... A X
... A X
... A Y
... A Y
... A Y
... B Y
... A Y
Wanted Result:
User Count_Type_A
X 2
Y 3
I could not get the "cut-off" timestamp by doing:
Select User, min(Timestamp)
Where Type=B
Group BY User;
But then how can I use that information inside the next query where I want to do something like:
SELECT User, count(Timestamp)
WHERE Type=A AND Timestamp<min(User.Timestamp_Type_B)
GROUP BY User;
My only idea so far are to determine the cut-off timestamps first and then do a join with all type A events and then select from the resulting table, but that feels wrong and would look ugly.
I'm also considering the possibility that this is the wrong type of problem/analysis for Hive and that I should consider hand-written map-reduce or pig instead.
Please help me by pointing in the right direction.
First Update:
In response to Cilvic's first comment to this answer, I've adjusted my query to the following based on workarounds suggested in the comments found at https://issues.apache.org/jira/browse/HIVE-556:
SELECT [User], COUNT([Timestamp]) AS [Before_First_B_Count]
FROM [Dataset] main
CROSS JOIN (SELECT [User], min([Timestamp]) [First_B_TS] FROM [Dataset]
WHERE [Type] = 'B'
GROUP BY [User]) sub
WHERE main.[Type] = 'A'
AND (sub.[User] = main.[User])
AND (main.[Timestamp] < sub.[First_B_TS])
GROUP BY main.[User]
Original:
Give this a shot:
SELECT [User], COUNT([Timestamp]) AS [Before_First_B_Count]
FROM [Dataset] main
JOIN (SELECT [User], min([Timestamp]) [First_B_TS] FROM [Dataset]
WHERE [Type] = 'B'
GROUP BY [User]) sub
ON (sub.[User] = main.[User]) AND (main.[Timestamp] < sub.[First_B_TS])
WHERE main.[Type] = 'A'
GROUP BY main.[User]
I did my best to follow hive syntax. Let me know if you have any questions. I would like to know why you wish/need to avoid a subquery.
In general, I +1 coge.soft's solution. Here it is again for your reference:
SELECT [User], COUNT([Timestamp]) AS [Before_First_B_Count]
FROM [Dataset] main
JOIN (SELECT [User], min([Timestamp]) [First_B_TS] FROM [Dataset]
WHERE [Type] = 'B'
GROUP BY [User]) sub
ON (sub.[User] = main.[User]) AND (main.[Timestamp] < sub.[First_B_TS])
WHERE main.[Type] = 'A'
GROUP BY main.[User]
However, a couple things to note:
What happens when there are no B events? Assuming you would want to count all the A events per user in that case an inner join as specified in the solution wouldn't work since there would be no entry for that user in the sub table. You would need to change to a left outer join for that.
The solution also does 2 passes over the data - one to populate the sub table, other to join the sub table with the main table. Depending on your notion of performance and efficiency, there is an alternative where you could do this by a single pass of data. You can distribute the data by user using Hive's distribute by functionality and write a custom reducer that would do your count calculation in your favorite language using Hive's transform functionality.