Hive Explode / Lateral View Table - hive

Table : jd1 (Comaparison Table)
Table : data1 (New Values Table)
I have write this query in sql server it's working but in hive it shows me an error
select * from data1;
1 siva hadoop
1 siva hive
1 siva spark
1 siva hbase
1 siva mapreduce
1 siva hdfs
2 kumar hadoop
2 kumar hive
2 kumar python
2 kumar spark
3 naveen hive
3 naveen hadoop
3 naveen flume
3 naveen kafka
select * from jd1;
1 hadoop
1 hive
1 spark
1 hbase
1 mapreduce
1 hdfs
1 python
1 java
Expected output
1 siva 6 85.71428571428571
2 kumar 4 57.142857142857146
3 naveen 2 28.571428571428573
My Query
select id, name, count(*), ((count(*)*100)/(select count(skills)from jd1))avg
from (select n.id, n.name, n.skills
from data1 n join jd1 t on (n.skills=t.skills))a
group by id,name;
Error
FAILED: ParseException line 1:44 cannot recognize input near 'select' 'count' '(' in expression specification

select id, name, count(*) cnt, count(*)*100/skill_cnt cnt_pct
from (select n.id, n.name, n.skills, t.skill_cnt
from data1 n
inner join (select skills, count(*) over() skill_cnt from jd1) t
on n.skills=t.skills
) a
group by id,name;

You can try below query -
SELECT n.id, n.name, COUNT(n.skills), COUNT(n.skills)/skill_cnt.total_skill
FROM data1 n
JOIN jd1 t ON n.skills=t.skills
CROSS JOIN (SELECT COUNT(*) total_skill FROM jd1) skill_cnt
GROUP BY n.id, n.name, total_skill

Create one more table for jd skill as skill_count and join this tables.
SELECT n.Id, n.Job_Id, n.Name, n.Email, n.Mobile_Number, n.Education, n.Total_Experiance,((count(n.skills)*100)/s.skill_count) Average
FROM new_resume n
JOIN new_jd t ON n.skills=t.skills
JOIN skill_count s ON n.job_id = s.job_id
GROUP BY n.Id, n.Job_Id, n.Name, n.Email, n.Mobile_Number, n.Education, n.Total_Experiance,s.skill_count;

Related

join two sql table as column

i have two tables as below:
firsttable
id
cat_id
name
1
2
name_01
2
2
name_02
3
1
name_03
4
3
name_04
5
3
name_04
secondtable
id
name
1
cat_01
2
cat_02
3
cat_03
my question is how can i create below table result?
id(secondtable)
name(secondtable)
count(firsttable)
1
cat_01
1
2
cat_02
2
3
cat_03
2
select t2.id,t2.name,
(select count(*) from firsttable t1 where t1.cat_id=t2.id )as count
from secendtable t2;
Just using standard aggregation
select
s.id,
s.name,
count(*)
from firsttable f
join secondtable s
on f.cat_id = s.id
group by s.id, s.name order by s.id
It's very Simple like that
SELECT s.id,s.name, (SELECT count(*)
FROM `firsttable` AS f
WHERE f.cat_id = s.id ) as count
FROM `secondtable` AS s

How do I join two tables in SQL limiting the second table data depending on the first one?

I have two tables looking something like this:
IMP DATE CAT
A 03/03/2016 1
B 04/04/2016 1
C 09/09/2016 2
D 01/01/2017 1
E 02/02/2017 1
F 03/03/2017 2
G 04/04/2017 2
===================
EXP DATE CAT
H 01/01/2016 1
I 05/05/2016 1
J 07/07/2016 2
K 11/11/2016 2
L 01/01/2017 1
M 03/03/2017 1
N 04/04/2017 2
O 05/05/2017 2
I want to join the first table to the second one but limit the lines joined from the second table by the latest date on the first table (per category).
The result I'm looking for would be every row in both tables except Item "M" (because Cat 1 in Table 1 has a latest date of February) and Item "O" (because Cat 2 in Table 1 has a latest date of April).
I've tried conditionalds within a where clause in the 2nd table but haven't got far.
Is there a simple way to do this? Any help is appreciated. I'm using SQL Server 2008 by the way.
Your description of the problem is specifically about using join. That suggests a query like this:
select . . .
from (select t1.*, max(t1.date) over (partition by t1.cat) as maxdate
from table1 t1
) t1 join
table2 t2
on t1.cat = t2.cat and t2.date <= t1.maxdate;
Desire output and its format is still not clear.
are you looking for this ?
;With CTE as
(
select *, row_number()over(partition by cat order by DATEs desc) rn
from #table1
)
--select * from cte
--where rn=1
select * from cte t1
left join #table2 t2
on t1.CAT=t2.CAT and t2.DATEs<=t1.DATEs
where rn=1

SQL. How to select multiple rows by using the MIN and GROUP BY

ID UserId Name Amount RewardId
----------------------------
1 1 James 10.00 1
2 1 James 10.00 2
3 1 James 10.00 3
4 2 Dave 20.00 1
5 2 Dave 20.00 3
6 3 Lim 15.00 2
I'm trying to insert to another table, and this is the result that i'm struggling with:
Tbl1ID RewardId
------------------
1 1
1 2
1 3
4 1
4 3
6 2
I'm trying to get the MIN(ID) of each person and select all the RewardId that belong to that person.
You could do a simple self join to get the minimum id value per userid/rewardid combination;
SELECT MIN(a.id) Tbl1ID, b.RewardId
FROM mytable a
JOIN mytable b
ON a.name = b.name
GROUP BY b.userid, b.rewardid
ORDER BY tbl1id, rewardid;
An SQLfiddle to test with.
If you are running SQL Server 2008+, you can simplify it by using Window Function.
INSERT INTO AnotherTable (Tbl1ID, RewardID)
SELECT MIN(ID) OVER (PARTITION BY Name),
RewardID
FROM SourceTable
SQLFiddle Demo
Try this
SELECT tbl1id,RewardID From
table1 S JOIN
(
SELECT MIN(ID) as tbl1id,Name FROM table1 GROUP BY Name
) T ON T.Name = S.Name
ORDER BY tbl1id
FIDDLE DEMO
Output:
Tbl1ID RewardId
----------------
1 1
1 2
1 3
4 1
4 3
6 2
If you want insert into new table then try this out
Insert into Newtable (tbl1id,RewardID)
SELECT tbl1id,RewardID from
table1 S JOIN
(
SELECT MIN(ID) as tbl1id,Name
FROM table1
GROUP BY Name
) T ON T.Name = S.Name
ORDER BY tbl1id;
FIDDLE DEMO

Cross Join with Filter?

i need to make Sp to distribute students to their sections
the procedure take 2 string parameters StuID and SecID
in case I've send '1,2,3,4,5' as StuID and 'a,b' as SecID
i'm using spliting function which well return tables
Tb1 | Tb2
1 | a
2 | b
3 |
4 |
5 |
how can i get the following result
1 a
2 b
3 a
4 b
5 a
....
I've tried to do it via cross join but it did not show the result i want
select US.vItem as UserID,SE.vItem as Section
from split(#pUserID,',') us
cross join split(#pSectionID,',') se
Cross join isn't meant to work like that.
This will give you the results you want, but it's a bodge.
select t1.vItem, t2.VItem from
( select *, ROW_NUMBER() over (order by vItem) r from US ) t1
inner join
( select *, ROW_NUMBER() over (order by vItem desc) -1 r from SE ) t2
on t2.r = t1.r % (select COUNT(*) from SE)
order by t1.vItem

How SQL join work?

i am using MYSQL..
I have two tables:
TABLE 1 (TABLE NAME T1)
SL NAME
1 a
2 b
3 c
4 c
table 2 (table name T2)
SL NAME
1 a
2 c
3 c
4 c
Q1: how i count the total number of 'c' in both table?
Q2: which name is max occurrences in both table?
sl is primary key...
my query is:>
select count(*) from t1,t2
where t1.name=t2.name where t1.name='c';
but it showing 6
To count c in both tables you should use UNION, not JOIN.
Syntax:
SELECT ...
UNION [ALL | DISTINCT] SELECT ...
[UNION [ALL | DISTINCT] SELECT ...]
Doc:
http://dev.mysql.com/doc/refman/5.0/en/union.html
Edit:
I'll explain the query that you provided.
select count(*) from t1,t2 where t1.name=t2.name where t1.name='c';
First of all, you use WHERE clause twice which is a syntax error. Should be:
select count(*) from t1,t2 where t1.name=t2.name AND t1.name='c';
And this is the same that:
SELECT count(*) from t1
JOIN t2 ON t1.name=t2.name
WHERE t1.name='c';
You choose only rows with c value so these are the rows, that we will take under consideration:
TABLE 1 (TABLE NAME T1)
SL NAME
3 c
4 c
table 2 (table name T2)
SL NAME
2 c
3 c
4 c
Now, simple JOIN joins every row from table 1 to every row from table 2 (where condition is true of course)
So the result before counting is:
t1.SL t1.NAME t2.SL t2.NAME
3 c 2 c
4 c 3 c
3 c 4 c
4 c 2 c
3 c 3 c
4 c 4 c
This is 6 rows.
Answers for both of your questions.
SELECT name, count(*) as cnt
FROM(select t1.name from t1
union all
select name from t2) as tem
group by name
order by cnt DESC
This query will give you ranking of names ordered by occurrences.
To retrieve only c count, just add WHERE clause. To retrieve only the most occurring name set LIMIT clause to 1.
INSERT INTO #test
SELECT NAME FROM m_t1 WHERE NAME ='c'
UNION all
SELECT NAME FROM m_t2 WHERE NAME ='c'
SELECT count(*) FROM #test