how to get the unique records with min and max for each user - sql

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?

Related

Efficient query for average count in SQL

I have a table:
id age tag
1 26 green
1 26 blue
2 28 yellow
3 45 red
4 40 blue
4 40 green
5 50 red
I need to find both the average and standard deviation of the number of distinct tags per age group (I have coded this with CASE WHEN).
So to get for example:
average_age average_tags sd_tags age_group
26.7 1.5 0.1 25-34
40 2 0 35-44
45 1 0.01 44+
Is there a way to more efficiently calculate the average count of tags and also to avoid repeating the CASE code in the sub-query for efficiency / readability?
SELECT
AVG(age) as average_age,
AVG(countField) as average_spread,
STDDEV(countField) as sd_spread,
CASE
WHEN v.age BETWEEN 25 AND 34 THEN '25-34'
WHEN v.age BETWEEN 35 AND 44 THEN '35-44'
ELSE '44+'
END AS age_group,
FROM vid v
(
SELECT AVG(v.age),
COUNT(DISTINCT(p.tag)) AS countField,
CASE
WHEN v.age BETWEEN 25 AND 34 THEN '25-34'
WHEN v.age BETWEEN 35 AND 44 THEN '35-44'
ELSE '44+'
END AS age_group,
FROM vid v
GROUP BY age_group
) as q WHERE age IS NOT NULL
GROUP BY age_group ORDER BY age_group;
you can use this query
select AVG(T.Age) as average_age,AVG(countField) as average_spread,age_group
from (select v.Age,COUNT(DISTINCT(v.Tag)) as countField,CASE
WHEN v.age BETWEEN 25 AND 34 THEN '25-34'
WHEN v.age BETWEEN 35 AND 44 THEN '35-44'
ELSE '44+' END AS age_group
from VID v
group by v.Age) T
WHERE age IS NOT NULL
GROUP BY age_group ORDER BY age_group;

Using a top x count query as a where clause to show all qualifying records

I have a count of a top 2
My table has this data
Name Age price visited size
Jon 34 53 2018-01-01 9
Don 22 70 2018-03-01 15
Pete 76 12 2018-11-09 7
Jon 34 55 2018-09-13 9
Paul 90 64 2018-07-08 6
Pete 76 31 2018-03-25 7
Jon 75 34 2018-06-06 8
select top 2
name,
count(name) as cnt
from
tbl1
group by name
order by cnt desc
Which returns my top 2 names
Jon 3
Pete 2
This name will change dynamically as the query is run depending on who has made the most visits in total (this is very simplified the actual table has 1000's of entries).
What I would like to do is then use the result of that query to get the following all of which needs to be in a single query;
Name Age price visited size
Jon 34 53 2018-01-01 9
Jon 34 55 2018-09-13 9
Jon 75 34 2018-06-06 8
Pete 76 12 2018-11-09 7
Pete 76 31 2018-03-25 7
In summary, count who has visited the most and then display all the records under those names.
Thanks in advance
Here's one option using in:
select *
from yourtable
where name in (
select top 2 name
from yourtable
group by name
order by count(*) desc
)
order by name
Online Demo

Calculate SUM Column Wise using dinaymic query in SQL Server

id varname 1area 2area 3area 4area
------------------------------------
1 abc 345 3.7 34 87
1 pqr 46 67 78 55
1 lmn 67 99 33 44
2 xyz 78 78 33 32
I need to calculate SUM of column query.
Is it possible to get column count using while loop?
You probably want to do a SUM() of the Narea column group by your id column like
select id, sum(1area),
sum(2area), sum(3area), sum(4area)
from tbl1
group by id;

how to write this sql

I have one table : SCORE
name score
mike 97
tom 86
lucy 44
and another table : RANK
low up rank
90 100 A
80 90 B
70 80 C
60 70 D
0 60 E
and I want the result like that
name score rank
mike 97 A
tom 86 B
lucy 44 E
How to write sql
Try the below query, JOIN two table with condition of rank between low and up
SELECT name,score,rank
FROM score
JOIN rank ON score > low AND score<=up

How to select value based on between Range in SQL?

I need to get the Result value based on given Input from the following table.
Gender |MinAge |MaxAge | Result
-------------------------------
C 0 25 0.11
M 0 25 1.12
M 26 30 2.25
M 31 35 1.20
M 36 40 3.58
F 0 25 1.25
F 26 30 2.25
F 31 35 1.20
F 36 40 2.02
Input values 1
***************
Gender - M
Age - 28
Expected Output
---------------
2.25
Input Values 2
***************
Gender - F
Age - 32
Expected Output
----------------
1.20
I try with Between condition but unable to find solution. I'm new to SQL. Please tell me how to write query for above condition.
Thanks
select
*
from
your_table
where Gender = 'M'
and 28 between MinAge and MaxAge