Count Top 5 Elements spread over rows and columns - sql

Using T-SQL for this table:
+-----+------+------+------+-----+
| No. | Col1 | Col2 | Col3 | Age |
+-----+------+------+------+-----+
| 1 | e | a | o | 5 |
| 2 | f | b | a | 34 |
| 3 | a | NULL | b | 22 |
| 4 | b | c | a | 55 |
| 5 | b | a | b | 19 |
+-----+------+------+------+-----+
I need to count the TOP 3 names (Ordered by TotalCount DESC) across all rows and columns, for 3 Age groups: 0-17, 18-49, 50-100. Also, how do I ignore the NULLS from my results?
If it's possible, how I can also UNION the results for all 3 age groups into one output table to get 9 results (TOP 3 x 3 Age groups)?
Output for only 1 Age Group: 18-49 would look like this:
+------+------------+
| Name | TotalCount |
+------+------------+
| b | 4 |
| a | 3 |
| f | 1 |
+------+------------+

You need to unpivot first your table and then exclude the NULLs. Then do a simple COUNT(*):
WITH CteUnpivot(Name, Age) AS(
SELECT x.*
FROM tbl t
CROSS APPLY ( VALUES
(col1, Age),
(col2, Age),
(col3, Age)
) x(Name, Age)
WHERE x.Name IS NOT NULL
)
SELECT TOP 3
Name, COUNT(*) AS TotalCount
FROM CteUnpivot
WHERE Age BETWEEN 18 AND 49
GROUP BY Name
ORDER BY COUNT(*) DESC
ONLINE DEMO
If you want to get the TOP 3 for each age group:
WITH CteUnpivot(Name, Age) AS(
SELECT x.*
FROM tbl t
CROSS APPLY ( VALUES
(col1, Age),
(col2, Age),
(col3, Age)
) x(Name, Age)
WHERE x.Name IS NOT NULL
),
CteRn AS (
SELECT
AgeGroup =
CASE
WHEN Age BETWEEN 0 AND 17 THEN '0-17'
WHEN Age BETWEEN 18 AND 49 THEN '18-49'
WHEN Age BETWEEN 50 AND 100 THEN '50-100'
END,
Name,
COUNT(*) AS TotalCount
FROM CteUnpivot
GROUP BY
CASE
WHEN Age BETWEEN 0 AND 17 THEN '0-17'
WHEN Age BETWEEN 18 AND 49 THEN '18-49'
WHEN Age BETWEEN 50 AND 100 THEN '50-100'
END,
Name
)
SELECT
AgeGroup, Name, TotalCount
FROM(
SELECT *,
rn = ROW_NUMBER() OVER(PARTITION BY AgeGroup, Name ORDER BY TotalCount DESC)
FROM CteRn
) t
WHERE rn <= 3;
ONLINE DEMO
The unpivot technique using CROSS APPLY and VALUES:
An Alternative (Better?) Method to UNPIVOT (SQL Spackle) by Dwain Camps

You can check below multiple-CTE SQL select statement
Row_Number() with Partition By clause is used ordering records within each group categorized by ages
/*
CREATE TABLE tblAges(
[No] Int,
Col1 VarChar(10),
Col2 VarChar(10),
Col3 VarChar(10),
Age SmallInt
)
INSERT INTO tblAges VALUES
(1, 'e', 'a', 'o', 5),
(2, 'f', 'b', 'a', 34),
(3, 'a', NULL, 'b', 22),
(4, 'b', 'c', 'a', 55),
(5, 'b', 'a', 'b', 19);
*/
;with cte as (
select
col1 as col, Age
from tblAges
union all
select
col2, Age
from tblAges
union all
select
col3, Age
from tblAges
), cte2 as (
select
col,
case
when age < 18 then '0-17'
when age < 50 then '18-49'
else '50-100'
end as grup
from cte
where col is not null
), cte3 as (
select
grup,
col,
count(grup) cnt
from cte2
group by
grup,
col
)
select * from (
select
grup, col, cnt, ROW_NUMBER() over (partition by grup order by cnt desc) cnt_grp
from cte3
) t
where cnt_grp <= 3
order by grup, cnt

Related

how to get this sql query

hello everyone I have a problem for this query, I need to get the rows where the id has the same numbers:
id2 | num
----+------
28 | 6
28 | 104
28 | 106
50 | 6
50 | 104
expected result:
id2 | num
----+-----
28 | 6
28 | 104
50 | 6
50 | 104
result doesn't include 28 106 because there's no 50 106 .
case 2:
id2 | num
----+-----
29 | 1
30 | 1
31 | 1
expected result:
id2 | num
----+-----
29 | 1
30 | 1
31 | 1
retrieves all because all the ids have num equal to 1
these numbers are random the condition is that if there are more than two ids they must have the same numbers in column 2
One way to do this is to count the occurrences of each num value, and compare it with the number of DISTINCT id2 values. If they are the same, then that num value occurs for every id2 value. You can then SELECT rows from the table which match those num values:
SELECT *
FROM data
WHERE num IN (SELECT num
FROM data
GROUP BY num
HAVING COUNT(*) = (SELECT COUNT(DISTINCT id2) FROM data))
ORDER BY id2, num
Output (for first dataset):
id2 num
28 6
28 104
50 6
50 104
Output (for second dataset):
id2 num
29 1
30 1
31 1
Demo on SQLFiddle
Another way is to use an OVER clause to count the occurrences of each value in num and compare to count of distinct values in id2 - which can be laterally joined:
CREATE TABLE mytable(
id2 VARCHAR(11)
,num INTEGER
);
INSERT INTO mytable(id2,num) VALUES ('28',6);
INSERT INTO mytable(id2,num) VALUES ('28',104);
INSERT INTO mytable(id2,num) VALUES ('28',106);
INSERT INTO mytable(id2,num) VALUES ('50',6);
INSERT INTO mytable(id2,num) VALUES ('50',104);
select id2, num
from (
select
id2, num
, count(*) over(partition by num) c_num
, ca.c_id2
from mytable
left join lateral (select count(distinct id2) c_id2 from mytable) ca on true
) d
where c_num = c_id2
;
id2 | num
:-- | --:
28 | 6
50 | 6
28 | 104
50 | 104
CREATE TABLE mytable(
id2 VARCHAR(11)
,num INTEGER
);
INSERT INTO mytable(id2,num) VALUES ('29',1);
INSERT INTO mytable(id2,num) VALUES ('30',1);
INSERT INTO mytable(id2,num) VALUES ('31',1);
select id2, num
from (
select
id2, num
, count(*) over(partition by num) c_num
, ca.c_id2
from mytable
left join lateral (select count(distinct id2) c_id2 from mytable) ca on true
) d
where c_num = c_id2
;
id2 | num
:-- | --:
29 | 1
30 | 1
31 | 1
db<>fiddle here
Basically, you want to count the number of distinct id2 values in the data and the number of distinct id2 values on each num. If only Postgres supported count(distinct) as a window function, you could do:
select id2, num
from (select t.*,
count(distinct t.id2) over (partition by t.num) as cnt_id2_on_num,
count(distinct t.id2) over () as cnt_id2
from t
) t
where cnt_id2_on_num = cnt_id2;
There is a simple work-around, which is the sum of dense_rank()s:
select id2, num
from (select t.*,
(dense_rank() over (partition by t.num order by t.id2) +
dense_rank() over (partition by t.num order by t.id2 desc)
) as cnt_id2_on_num,
(dense_rank() over (order by t.id2) +
dense_rank() over (order by t.id2 desc)
) as cnt_id2
from mytable t
) d
where cnt_id2_on_num = cnt_id2;
If you know there are no duplicates, you can write this as:
select id2, num
from (select t.*,
count(*) (partition by t.num) as cnt_id2_on_num,
(dense_rank() over (order by t.id2) +
dense_rank() over (order by t.id2 desc)
) as cnt_id2
from mytable t
) d
where cnt_id2_on_num = cnt_id2;

Eliminating duplicate rows except one column with condition

I am having trouble trying to find an appropriate query(SQL-SERVER) for selecting records with condition however, the table I will be using has more than 100,000 rows and more than 20 columns.
So I need a code that satisfies the following condition:
1.)If [policy] and [plan] column is unique between rows then I will select that record
2.)If [policy] and [plan] return 2 or more rows then I will select the record which 'code' column isn't 999
3.)In some cases the unwanted rows may not have '999' in [code] column but may be other specifics
In other words, I would like to get row number 1,2,4,5,7.
Here is an example of what the table looks like
row #|policy|plan|code
-----------------------
1 | a | aa |111
-----------------------
2 | b | bb |112
-----------------------
3 | b | bb |999
-----------------------
4 | c | cc |111
-----------------------
5 | c | cc |112
-----------------------
6 | c | cc |999
-----------------------
7 | d | dd |999
-----------------------
I'm expecting to see something like
row #|policy|plan|code
-----------------------
1 | a | aa |111
-----------------------
2 | b | bb |112
-----------------------
4 | c | cc |111
-----------------------
5 | c | cc |112
-----------------------
7 | d | dd |999
-----------------------
Thank you in advance
This sounds like a prioritization query. You an use row_number():
select t.*
from (select t.*,
row_number() over (partition by policy, plan
order by code
) as seqnum
from t
) t
where seqnum = 1;
The expected output makes this a bit clearer:
select t.*
from (select t.*,
rank() over (partition by policy, plan
order by (case when code = 999 then 1 else 2 end) desc
) as seqnum
from t
) t
where seqnum = 1;
The OP wants all codes that are not 999 unless the only codes are 999. So, another approach is:
select t.*
from t
where t.code <> 999
union all
select t.*
from t
where t.code = 999 and
not exists (select 1
from t t2
where t2.policy = t.policy and t2.plan = t.plan and
t2.code <> 999
);
May be you want this (eliminate the last row if more than one)?
select t.*
from (select t.*
, row_number() over (partition by policy, plan
order by code desc
) AS RN
, COUNT(*) over (partition by policy, plan) AS RC
from t
) t
where RN > 1 OR RN=RC;
Output:
row policy plan code RN RC
1 1 a aa 111 1 1
2 2 b bb 112 2 2
3 5 c cc 112 2 3
4 4 c cc 111 3 3
5 7 d dd 999 1 1
CREATE TABLE #Table2
([row] int, [policy] varchar(1), [plan] varchar(2), [code] int)
;
INSERT INTO #Table2
([row], [policy], [plan], [code])
VALUES
(1, 'a', 'aa', 111),
(2, 'b', 'bb', 112),
(3, 'b', 'bb', 999),
(4, 'c', 'cc', 111),
(5, 'c', 'cc', 112),
(6, 'c', 'cc', 999),
(7, 'd', 'dd', 999)
;
with cte
as
(
select *,
row_number() over (partition by policy, [plan]
order by code
) as seqnum
from #Table2
)
select [row], [policy], [plan], [code] from cte where seqnum=1

Order rows by values

I trying order table by rank, but rows which have position value - have to have position according to value in position field. It is possible do it without additional tables, views etc?
I have table like this:
rank | position | name
999 | 10 | txt1
200 | 4 | txt2
32 | 1 | txt3
1200 | 2 | txt4
123 | null | txt5
234 | null | txt6
567 | null | txt7
234 | null | txt8
432 | null | txt9
877 | null | txt10
Desired output have to look like this:
rank | position | name
32 | 1 | txt3
1200 | 2 | txt4
877 | null | txt10
200 | 4 | txt2
567 | null | txt7
432 | null | txt9
345 | null | txt8
234 | null | txt6
123 | null | txt5
999 | 10 | txt1
Here is an idea. Assign the proper ordering to each row. Then, if the position is available use that instead. When there are ties, put the position value first:
select t.*
from (select t.*, row_number() over (order by rank desc) as seqnum
from t
) t
order by (case when position is not null then position else seqnum end),
(case when position is not null then 1 else 2 end);
SQL Fiddle doesn't seem to be working these days, but this query demonstrates the results:
with t(rank, position, t) as (
select 999, 10, 'txt1' union all
select 200, 4, 'txt2' union all
select 32 , 1, 'txt3' union all
select 1200, 2, 'txt4' union all
select 123, null, 'txt5' union all
select 234, null, 'txt6' union all
select 567, null, 'txt7' union all
select 234, null, 'txt8' union all
select 432, null, 'txt9' union all
select 877, null , 'txt10'
)
select t.*
from (select t.*, row_number() over (order by rank desc) as seqnum
from t
) t
order by (case when position is not null then position else seqnum end),
(case when position is not null then 1 else 2 end);
EDIT;
When I wrote the above, I had a nagging suspicion of a problem. Here is a solution that should work. It is more complicated but it does produce the right numbers:
with t(rank, position, t) as (
select 999, 10, 'txt1' union all
select 200, 4, 'txt2' union all
select 32 , 1, 'txt3' union all
select 1200, 2, 'txt4' union all
select 123, null, 'txt5' union all
select 234, null, 'txt6' union all
select 567, null, 'txt7' union all
select 234, null, 'txt8' union all
select 432, null, 'txt9' union all
select 877, null , 'txt10'
)
select *
from (select t.*, g.*,
row_number() over (partition by t.position order by t.rank) gnum
from generate_series(1, 10) g(n) left join
t
on t.position = g.n
) tg left join
(select t.*,
row_number() over (partition by t.position order by t.rank) as tnum
from t
) t
on tg.gnum = t.tnum and t.position is null
order by n;
This is a weird sort of interleaving problem. The idea is to create slots (using generate series) for the positions. Then, assign the known positions to the slots. Finally, enumerate the remaining slots and assign the values there.
Note: I hard-coded 10, but it is easy enough to put in count(*) from the table there.
suppose you stored data in table1.
Then you should update column "position" as follows:
update a
set position = x.pos_null
from table1
a
inner join
(
select
a.name,
COUNT(a.rank) as pos_null
from
(
select
*
from table1
where position is null
)
a
left join
(
select
*
from table1
)
b
on a.rank <= b.rank
group by
a.name
)
x
on a.name = x.name
select * from table1 order by position
Bye,
Angelo.

Insert column with Max in sql

I did not know how to insert column with max.
Select id,MAX(salary),Min(Salary)
from C
GROUP BY id;
it is give me the all id with it is maximum
and I want just the id with maximum and minimum of salary!!
Several options for you that only require a single scan of the table:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE C ( ID, SALARY ) AS
SELECT 1, 100 FROM DUAL
UNION ALL SELECT 2, 110 FROM DUAL
UNION ALL SELECT 3, 100 FROM DUAL
UNION ALL SELECT 4, 110 FROM DUAL
UNION ALL SELECT 5, 90 FROM DUAL
Query 1 - Get a single ID:
SELECT *
FROM (
SELECT ID, SALARY
FROM c
ORDER BY SALARY DESC
)
WHERE ROWNUM = 1
Results:
| ID | SALARY |
|----|--------|
| 2 | 110 |
Query 2 - Get a single ID (alternate method that will get min and max IDs):
SELECT MAX( ID ) KEEP ( DENSE_RANK LAST ORDER BY SALARY ) AS MAX_SALARY_ID,
MAX( SALARY ) AS MAX_SALARY,
MIN( ID ) KEEP ( DENSE_RANK FIRST ORDER BY SALARY ) AS MIN_SALARY_ID,
MIN( SALARY ) AS MIN_SALARY
FROM C
Results:
| MAX_SALARY_ID | MAX_SALARY | MIN_SALARY_ID | MIN_SALARY |
|---------------|------------|---------------|------------|
| 4 | 110 | 5 | 90 |
Query 3 - Get all the IDs with the maximum salary:
SELECT ID, SALARY
FROM (
SELECT ID,
SALARY,
RANK() OVER ( ORDER BY SALARY DESC ) AS RNK
FROM C
)
WHERE RNK = 1
Results:
| ID | SALARY |
|----|--------|
| 2 | 110 |
| 4 | 110 |
Query 4 - Get all IDs for min and max salary:
SELECT LISTAGG( CASE MIN_RANK WHEN 1 THEN ID END, ',' ) WITHIN GROUP ( ORDER BY ID ) AS MIN_SALARY_IDS,
MAX( CASE MIN_RANK WHEN 1 THEN SALARY END ) AS MIN_SALARY,
LISTAGG( CASE MAX_RANK WHEN 1 THEN ID END, ',' ) WITHIN GROUP ( ORDER BY ID ) AS MAX_SALARY_IDS,
MAX( CASE MAX_RANK WHEN 1 THEN SALARY END ) AS MAX_SALARY
FROM (
SELECT ID,
SALARY,
RANK() OVER ( ORDER BY SALARY ASC ) AS MIN_RANK,
RANK() OVER ( ORDER BY SALARY DESC ) AS MAX_RANK
FROM C
)
Results:
| MIN_SALARY_IDS | MIN_SALARY | MAX_SALARY_IDS | MAX_SALARY |
|----------------|------------|----------------|------------|
| 5 | 90 | 2,4 | 110 |
Query 5:
SELECT ID,
SALARY,
CASE WHEN MIN_RANK = 1 THEN 'MIN'
WHEN MAX_RANK = 1 THEN 'MAX' END AS MIN_MAX
FROM (
SELECT ID,
SALARY,
RANK() OVER ( ORDER BY SALARY ASC ) AS MIN_RANK,
RANK() OVER ( ORDER BY SALARY DESC ) AS MAX_RANK
FROM C
)
WHERE MIN_RANK = 1 OR MAX_RANK = 1
Results:
| ID | SALARY | MIN_MAX |
|----|--------|---------|
| 2 | 110 | MAX |
| 4 | 110 | MAX |
| 5 | 90 | MIN |
Select id,
salary
from C
where salary = (select MAX(salary)
from C)
you can use first_value or last_value
The FIRST_VALUE analytic function is similar to the FIRST analytic
function, allowing you to return the first result from an ordered set.
https://oracle-base.com/articles/misc/first-value-and-last-value-analytic-functions
create table C (id int, salary int);
insert into c values(1, 1);
insert into c values(2, 2);
insert into c values(3, 3);
insert into c values(4, 4);
insert into c values(5, 5);
Select distinct first_value(id) over ( order by salary desc)
from C
FIRST_VALUE(ID)OVER(ORDERBYSAL
1 5

Using Case in a select statement

Consider the following table
create table temp (id int, attribute varchar(25), value varchar(25))
And values into the table
insert into temp select 100, 'First', 234
insert into temp select 100, 'Second', 512
insert into temp select 100, 'Third', 320
insert into temp select 101, 'Second', 512
insert into temp select 101, 'Third', 320
I have to deduce a column EndResult which is dependent on 'attribute' column. For each id, I have to parse through attribute values in the order
First, Second, Third and choose the very 1st value which is available i.e. for id = 100, EndResult should be 234 for the 1st three records.
Expected result:
| id | EndResult |
|-----|-----------|
| 100 | 234 |
| 100 | 234 |
| 100 | 234 |
| 101 | 512 |
| 101 | 512 |
I tried with the following query in vain:
select id, case when isnull(attribute,'') = 'First'
then value
when isnull(attribute,'') = 'Second'
then value
when isnull(attribute,'') = 'Third'
then value
else '' end as EndResult
from
temp
Result
| id | EndResult |
|-----|-----------|
| 100 | 234 |
| 100 | 512 |
| 100 | 320 |
| 101 | 512 |
| 101 | 320 |
Please suggest if there's a way to get the expected result.
You can use analytical function like dense_rank to generate a numbering, and then select those rows that have the number '1':
select
x.id,
x.attribute,
x.value
from
(select
t.id,
t.attribute,
t.value,
dense_rank() over (partition by t.id order by t.attribute) as priority
from
Temp t) x
where
x.priority = 1
In your case, you can conveniently order by t.attribute, since their alphabetical order happens to be the right order. In other situations you could convert the attribute to a number using a case, like:
order by
case t.attribute
when 'One' then 1
when 'Two' then 2
when 'Three' then 3
end
In case the attribute column have different values which are not in alphabetical order as is the case above you can write as:
with cte as
(
select id,
attribute,
value,
case attribute when 'First' then 1
when 'Second' then 2
when 'Third' then 3 end as seq_no
from temp
)
, cte2 as
(
select id,
attribute,
value,
row_number() over ( partition by id order by seq_no asc) as rownum
from cte
)
select T.id,C.value as EndResult
from temp T
join cte2 C on T.id = C.id and C.rownum = 1
DEMO
Here is how you can achieve this using ROW_NUMBER():
WITH t
AS (
SELECT *
,ROW_NUMBER() OVER (
PARTITION BY id ORDER BY (CASE attribute WHEN 'First' THEN 1
WHEN 'Second' THEN 2
WHEN 'Third' THEN 3
ELSE 0 END)
) rownum
FROM TEMP
)
SELECT id
,(
SELECT value
FROM t t1
WHERE t1.id = t.id
AND rownum = 1
) end_result
FROM t;
For testing purpose, please see SQL Fiddle demo here:
SQL Fiddle Example
keep it simple
;with cte as
(
select row_number() over (partition by id order by (select 1)) row_num, id, value
from temp
)
select t1.id, t2.value
from temp t1
left join cte t2
on t1.Id = t2.id
where t2.row_num = 1
Result
id value
100 234
100 234
100 234
101 512
101 512