create a table from total values fron all tables in athena - sql

table 1:
name
rollno
marks
mark
01
25
ram
02
30
june
03
45
table 2:
emp.id
emp.name
emp.sal
01
raj
20000
02
ram
25000
03
pie
30000
table 3:
pro.id
pro.name
pro.price
01
dove
40
02
colgate
60
03
lays
50
how do i query this in athena..?
my athena query:
> create view table4
> as
> select sum(marks) as sumvalues
> from table1
> union
> select sum(emp.sal) as sumvalues
> from table 2
> union
> select sum(pro.price) as sumvalues
> from table 3;
im getting output:
id | sumvalues
01 |100
02 |75000
03 |150
i need table name or any field name in place of id like
table 4:
name
sumvalues
marks
100
emp.sal
75000
pro.price
150

Related

How can I get distinct data all because if Thana Code same then get last date Update in SQL

id name Thana Date
------------------------
01 A j 2021-05-27
01 A k 2021-05-27
01 A l 2021-05-27
02 B j 2021-06-29
02 B w 2021-06-29
02 B x 2021-06-29
I want all rows except those which contain thana code j.
If you want to have all the rows execpt those where Thana = 'J' you can use a WHERE clause in your query :
SELECT id, Name, Thana, Date
FROM MyTable
WHERE Thana <> 'J'

How to write a SQL query to select batsman wiht highest score than the previous year

Given this table:
Name Score Year
----------------------
Richie 50 2017
Richie 40 2016
Richie 30 2015
Mark 20 2017
Mark 30 2016
Smith 60 2015
Smith 50 2014
Select batsman, score whose score is greater than the previous score.
For example, here the output should be
Richie 50 2017
Richie 40 2016
Smith 60 2015
I don't know how to get this answer - my attempt so far:
select Name, score
from table
order by score ...
Output should be
Richie 50 2017
Richie 40 2016
Smith 60 2015
Use lag():
select t.*
from (select t.*, lag(score) over (partition by name order by year) as prev_score
from t
) t
where score > prev_score;
You can do it with a self join:
select t.*
from tablename t inner join tablename tt
on tt.name = t.name and tt.year = t.year - 1
where t.score > tt.score
or with exists:
select t.*
from tablename t
where exists(
select 1 from tablename
where name = t.name and year = t.year - 1 and score < t.score
)
See the demo.
Results:
> name | score | year
> :----- | ----: | ---:
> Richie | 50 | 2017
> Richie | 40 | 2016
> Smith | 60 | 2015
Do not use py = (cy.year - 1) or cy = (py.year + 1) in the join, it wouldn't work when there is a gap of 2 or more years.
Try this :
select distinct cy.name,cy.score,cy.year from tablename cy
join tablename py
on cy.NAME = py.NAME and cy.year > py.year and cy.score > py.score
order by cy.year desc;
Results :
NAME SCORE YEAR
-------------------- ---------- ----------
Richie 50 2017
Richie 40 2016
Smith 60 2015

How to select records if all of the conditions satisfies in sql

I have two tables stage and hier table
stage table:
POL_NO AGENT COMPANY_CODE
100 001 01
101 002 01
102 003 01
103 004 01
hier table:
PAY_REASON PAY_CODE AGENCY_CODE FINANCIAL_AGENT COMPANY_CODE AGENT
S BO - - 01 001
P - H 01 001
- - B - 01 001
S BO B 420 01 002
S BO - - 01 002
S BO - - 01 003
P DD U - 01 003
- - B - 01 003
- - B - 01 004
- - B - 01 004
- - - 420 01 004
the stage table will compare with the hier table based on (company_code and agent) to look if the below criteria is met in the hier table:
1.(pay_reason='S'and pay_code='BO') or agency_code in ('B','H','U')
2.Financial agent!=420
If any of the records in hier table does not match the criteria,then the company_code and agent won't show up in the expected result
expected result:
POL_NO AGENT COMPANY_CODE
100 001 01
102 003 01
You're almost there:
Select *
From stage s
where not exists (
select *
from hier h
where s.AGENT = h.AGENT
And s.COMPANY_CODE = h.COMPANY_CODE
and not(((pay_reason='S'and pay_code='BO') or agency_code in ('B','H','U'))
and Financial_agent!=420))
If I understand the conditions correctly, you seem to want:
select *
from stage s
where not exists (select 1
from hier h
where s.agent = h.agent and
s.company_code = h.company_code and
h.Financial_agent = 420 and
not ( (h.pay_reason = 'S' and pay_code = 'BO') or
h.agency_code in ('B', 'H', 'U')
)
);

SQL Server, Find duplicate rows base on a column

Say if I have a datatable as follow:
ID Name Birthday
------ -------- ------------
01 Eric 06/20
02 Judy 03/15
03 Mary 04/01
04 John 03/15
05 Judy 06/20
06 John 09/11
How can I get the result base on Name:
ID Name Birthday
------ -------- ------------
02 Judy 03/15
04 John 03/15
05 Judy 06/20
06 John 09/11
Because both Judy and John appeared more than once (duplicates).
Base on Birthday:
ID Name Birthday
------ -------- ------------
01 Eric 06/20
02 Judy 03/15
04 John 03/15
05 Judy 06/20
Because both 06/20 and 03/15 appeared more than once!
You should look up WHERE, GROUP BY clauses as well as INNER queries and JOINs in SQL server.
Here are the link to MSDN documentation
https://learn.microsoft.com/en-us/sql/t-sql/queries/where-transact-sql
https://learn.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql?view=sql-server-2017
Using GROUP BY and WHERE your query for case 1 should be:
Select * from Tbl where Name in
(
Select Name
from Tbl
Group by Name
HAVING COUNT(1)>1
)
Similarly second result should be like:
Select * from Tbl where Birthday in
(
Select Birthday
from Tbl
Group by Birthday
HAVING COUNT(1)>1
)
based on Name
select * from myTable where name in(
select Name from myTable group by Name having count(*)>1)
you can make small change on that for birtday
this is the SQL Fiddle link where you can change the table Name or field data type based on your real DB
SQL Fiddle Link
Why not use exists ?
select *
from table t
where exists (select 1
from table
where Name = t.name
having count(1) > 1
);
In order to get date based on Birthday, use Birthday column in inner query.

Iterate Data Table ontop of another Data table and make one table

I have a question.
Here is the story, I have Student mark sheet (Table A)
Name Course Marks
Student 01 32472 44
Student 02 32472 80
Student 03 32472 67
Student 04 32472 76
Student 05 32472 56
Student 06 32472 98
Student 07 32472 13
Student 08 32472 68
Student 09 32472 84
Student 10 32472 93
And I have sheet (Table B ) for cut off marks for medals.
Medel Course CutOff
Silver 32472 0-69
Gold 32472 70 -84
Platinum 32472 85 -100
Could you please help me to get following sheet (Table) in SQL.
Name Course Medal
Student 01 32472 Silver
Student 02 32472 Gold
Student 03 32472 Silver
Student 04 32472 Gold
Student 05 32472 Silver
Student 06 32472 Platinum
Student 07 32472 Silver
Student 08 32472 Silver
Student 09 32472 Gold
Student 10 32472 Platinum
How Can I join Table A (Student marks sheet) and Table B (Medal cut off marks sheet) then get Final Table ?
Really appreciate your answers/comments.
Thank you
Gayan
After your edit:
;WITH CteMedals AS(
SELECT *,
rStart = LEFT(CutOff, CHARINDEX('-', CutOff) - 1),
rEnd = SUBSTRING(CutOff, CHARINDEX('-', CutOff) + 1, LEN(CutOff) - CHARINDEX('-', CutOff))
FROM TableB
)
SELECT
a.Name, a.Course, ISNULL(b.Medal, 'Silver') AS Medal
FROM TableA a
OUTER APPLY(
SELECT TOP 1 Medal
FROM CteMedals
WHERE
a.Marks BETWEEN rStart AND rEnd
ORDER BY rEnd DESC
)b
You can use OUTER APPLY:
SELECT
a.Name, a.Course, ISNULL(b.Medal, 'Silver') AS Medal
FROM TableA a
OUTER APPLY(
SELECT TOP 1 Medal
FROM TableB
WHERE
a.Marks >= CutOff
ORDER BY CutOff DESC
)b