Assuming this sample data in a sql table:
people income
Mary 100
Joe 60
Sam 70
Bob 85
Sarah 85
I am trying to create a column which will identify each row as within the top 20% of income. So, the expected output is:
people income performers
Mary 100 Y
Joe 60 N
Sam 70 N
Bob 85 Y
Sarah 85 Y
I have tried TOP (20) PERCENT and a CASE statement, no luck.
Just tried even extracting the top 20% and kept getting errors as well.
In Pandas, I got this, no problem. In SQL, it's like underwater brain surgery using oven mitts.
If you're looking to compare each record to the max value of income, you can use a CASE expression to compare the current record:
SELECT people, income,
CASE
WHEN income >= (SELECT MAX(income) FROM Foo) * 0.8
THEN 'Y' ELSE 'N'
END AS performers
FROM Foo;
people income performer
Mary 100 Y
Joe 60 N
Sam 70 N
Bob 85 Y
Sarah 85 Y
You can use a case statement
select
people,
income,
case when income >= (select max(income) from Table) * .8 then 'Yes' else 'No' end as performer
from Table
Related
I have a table called match_score which have following data
id
participant
round
score
1
gabe
1
100
2
john
1
90
3
duff
1
80
4
vlad
1
85
5
gabe
2
75
6
john
2
70
Let's just say that round 1 is the preliminary round and 2 is the final round
I want to rank the result based on the score and grouped by the participant , if I'm using some normal sql group by participant and order by score desc.
vlad are the 1st, duff 2nd and gabe 3rd, which one is wrong.
what i want is
1st gabe with 75 point in the final round
2nd john with 70 point in the final round
3rd vlad with 85 point in the preliminary round
4th duff with 80 point in the preliminary round
maybe something like this:
select
h.participant,
h.round,
h.score
from
match_score h
where
not exists (
select 1
from
match_score t
where
t.participant = h.participantand t.round > h.round
)
order by
h.round desc,
h.score desc;
how to split one value from sql database and devide into two rows (one row set 1000000, one row is the balance) Others less than 1000000.or any idea using coldfusion . Thanks in advance.
Example
No. Code Name Account No Total
1 123 black 123456789 160000.00
2 124 red 111111222 5200.00
3 124 blue 444555666 121000.00
I want This result
No. Code Name Account No Total
1 123 black 123456789 100000.00
2 123 black 123456789 60000.00
3 124 red 111111222 5200.00
4 124 blue 444555666 100000.00
5 124 blue 444555666 21000.00
First you need to insert a new row:
INSERT INTO TABLE (CODE, NAME, ACCOUNT_NO, TOTAL)
SELECT
CODE,
NAME,
ACCOUNT_NO,
TOTAL MOD 100000 AS NEW_TOTAL
FROM TABLE
WHERE TOTAL > 100000;
That command will insert the second row. Now you need to fix the first row.
UPDATE TABLE
SET TOTAL = TOTAL - (TOTAL MOD 100000)
WHERE TOTAL > 100000;
The query I'm trying to answer is 'How many sales above or equal to 60 has each person made?'
My table (sales$):
SaleID name salevalue
1 Steve 100
2 John 50
3 Ellen 25
4 Steve 100
5 Mary 60
6 Mary 80
7 John 70
8 Mary 55
9 Steve 65
10 Ellen 120
11 Ellen 30
12 Ellen 40
13 John 40
14 Mary 60
15 Steve 50
My code is:
select name,
COUNT(*) as 'sales above 60'
from Sales$
group by salevalue, name
having salevalue >= 60;
Which gives:
Ellen 1
John 1
Mary 2
Mary 1
Steve 1
Steve 2
The information is correct in that Mary & Steve both have 3 sales, however I'm forced by the HAVING command to group them out.
Any ideas? I'm sure I've just taken a wrong turning.
You can use conditional aggregation for this:
select name,
COUNT(case when salevalue >= 60 then 1 end) as 'sales above 60'
from Sales$
group by name
This way COUNT will take into consideration only records having salevalue >= 60.
I've swapped the HAVING statement for a WHERE and achieved the desired result:
select name, count(*) 'sales above 50'
from sales$
where salevalue >=60
group by name
(Lightbulb moment after posting)
I have the following table:
id gender age highest weight lowest weight abc
a f 30 90 70 1.3
a f 30 90 65 null
a f 30 null null 1.3
b m 40 100 86 2.5
b m 40 null 80 2.5
c f 50 105 95 6.4
I need this result in sql server. What I need is the minimum of the weight and maximum of the weight and one record per user.
id gender age highest weight lowest weight abc
a f 30 90 65 1.3
b m 40 100 80 2.5
c f 50 105 95 6.4
Just do a grouping:
select id,
max(gender),
max(age),
max([highest weight]),
min([lowest weight]),
max(abc)
from SomeTable
group by id
You can do this using grouping:
select id, gender, max(highest_weight), min(lowwest_weight) from student
group by id, gender
But you need do define the rule for the other fields with variable value, like abc
Can you post more information?
I'm trying to answer these questions but I couldn't and I need here
1) List the number of days that have elapsed since each student joined.
this what I did
Select FR_FIRSTNAME,
FR_LASTNAME,
trunc(sysdate - FR_DATEJOINED) / 7 DAYS
from alharbi_bandar5_FRESHMEN;
no rows selected
2) List the student names and city in upper case.
This what i did
Select FR_FIRSTNAME, FR_LASTNAME, CITY FROM alharbi_bandar5_FRESHMEN
where UPPER (FR_FIRSTNAME, FR_LASTNAME, CITY) like 'SMITH%';
> where UPPER (FR_FIRSTNAME, FR_LASTNAME, CITY) like 'SMITH%'
*
ERROR at line 2:
ORA-00909: invalid number of arguments
3) List the no and last name of the student(s) with the highest ACT score.
This what i did
Select FR_NO, FR_LASTNAME, ACT from alharbi_bandar5_FRESHME
where ACT = MAX(ACT);
where ACT = MAX(ACT)
*
ERROR at line 2:
ORA-00934: group function is not allowed here
this is my table
FR_ FR_FIRSTNAME FR_LASTNAME FR_DATEJO ACT CITY
--- ------------------------------ ------------------------------ --------- ---------- ------------------------------
100 Mark Ramon 12-JUL-13 21 Florence
101 John Wright 13-JUN-13 31 Edgewood
102 Peter Sellers 06-JAN-13 30 Blue Ash
103 Eric Bates 14-MAY-13 24 Milford
104 Theresa Boyers 23-APR-13 22 Covingtion
105 Alex William 04-MAR-13 24 Edgewood
106 Eric Byrd 23-MAR-13 19 Alexandria
107 Steve Norris 21-DEC-12 21 Highland
108 Lisa Nkosi 13-FEB-13 33 Florence
109 Bradley Rego 21-FEB-12 29 Covington
110 Kathy Thomas 15-OCT-12 27 Milford
111 Catherine Jones 17-APR-13 34 Edgewood
112 Emily Hess 15-NOV-12 36 Highland
113 Josha Hunter 19-MAY-14 31 Florence
A lot of these questions have answers in the Oracle SQL reference and are mostly syntax issues.
1) trunc(sysdate - FR_DATEJOINED) / 7 DAYS
Oracle gies out the number of days in the units of difference, so sysdate - FR_DATEJOINED would gie you number of days, which could also involve fractional component (2.5 days for example, if it has been 2 days and 12 hours since the candidate joined). Trunc would get rid of the fractional component, but "/7" would convert the result into number of weeks instead. why are you doing this?
Either way, i don't believe this query is being fired against the table below, otherwise you'd not get zero rows as you are not filtering anything at all.
Check these out for more info on Oracle's date functions.
http://docs.oracle.com/cd/E17952_01/refman-5.1-en/date-and-time-functions.html
https://www.youtube.com/watch?v=H18UWBoHhHY
2) UPPER function accepts a column name or an expression, so if you need multiple columns. you'd need to use UPPER around each column.
3) For this example, you'll need to use a subquery to get the max value first and then use the query on top.
getting the max value
Select max(act) from alharbi_bandar5_FRESHME;
so, final query would be...
Select FR_NO, FR_LASTNAME, ACT from alharbi_bandar5_FRESHME
where ACT = (select MAX(ACT) from alharbi_bandar5_FRESHME);
Or, you could use the oracle rank function..
select fr_no,
fr_last_name,
act
from (
select fr_no, fr_lastname, act,
rank () over (order by act desc) rnk
from alharbi_bandar5_FRESHME
) where rnk = 1