I have a table that looks like this
id
name
count
1
Nishu
4
2
Shivam
2
3
Himanshu
1
I want to get the Output like this:-
id
name
count
1
Nishu
4
1
Nishu
4
1
Nishu
4
1
Nishu
4
2
Shivam
2
2
Shivam
2
3
Himanshu
1
3
Himanshu
1
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
You can use a cross join against generate_series()
select t.*
from the_table t
cross join generate_series(1, t.count) as g
order by t.id;
Online example
Using RECURSIVE CTE you can do:
WITH RECURSIVE cte as (
SELECT 1 as x,m.* FROM mytable m
union all
SELECT x+1,m.*
FROM cte,mytable m
WHERE x<m.count)
SELECT DISTINCT *
FROM cte
ORDER BY count DESC;
see: DBFIDDLE
more info:
WITH Queries (Common Table Expressions)
Learn PostgreSQL Recursive Query By Example
Related
I have the following table structure:
id val1 val2 val3 date
1 1 2 3 14.12.2021
2 2 3 5 17.12.2021
3 4 6 8 18.12.2021
. . . . .
. . . . .
. . . . .
9 3 4 5 04.01.2022
so far i use the following command:
SELECT * FROM table WHERE `date` >= '13.12.2021' and `date` <= '24.12.2021'
but if i want to go into the new year, the command returns me no value
SELECT * FROM table WHERE `date` >= '13.12.2021' and `date` <= '04.01.2022'
Does anyone have an idea how I have to modify the command so that it works?
Because you store date in wrong format you should transform it in where clause:
select *
from test
where substr(date,7)||'-'||substr(date,4,2)||'-'||substr(date,1,2) between '2021-12-13' and '2022-01-04';
SQLite sandbox here
But better way is - store dates in proper format YYYY-MM-DD
I'm using ROW_NUMBER() and PARTITION BY to create a table that looks like this:
VALUE ID ROWNUM
A 4525 1
B 4526 1
C 4527 1
D 4530 1
E 4530 2
F 4530 3
G 4531 1
H 4531 2
I want to get rid of the partitions that consist of only one value. A, B, and C will be gone, but D through H will stay because they're in partitions that contain more than one value. In other words, what can I do to my query to make the results look like this:
VALUE ID ROWNUM
D 4530 1
E 4530 2
F 4530 3
G 4531 1
H 4531 2
I can't filter by ROWNUM > 1 because I want to keep some of the columns where ROWNUM = 1, but figuring out how to keep only some of those columns is stumping me.
However you are calculating rownum, also calculate count(*):
select value, id, rownum
from (select t.*,
row_number() over (partition by . . . order by . . . ) as rownum,
count(*) over (partition by . . . ) as cnt
from t
) t
where cnt > 1;
The partition by should be the same for the two window functions.
Is there a way to show 2nd column when doing a count of the 1st column?
lets say you have:
ID . Name
1 . John
2 . John
3 Peter
4 . Sue
5 . Sue
6 . Sue
I want the resul set to show the Name AND ID that has count > = 2, even though it repeats like below:
Name ID Count(*)
John .1 2
John 2 . 2
Sue . 4 . 3
Sue . 5 . 3
Sue . 6 . 3
I know you can do:
select Name, count(ID)
from A
group by Name
having count(id) >= 2
but this only shows you the Name, so when I try:
select Name, ID, count(*)
from A
group by Name, ID
having count(*) >=2
I get nothing back, since its counting it per each line, is there way to
bring back the Name and doing the count of id for the Name? I am using Oracle
Thanks
You can use window functions:
select Name, count(*) over (partition by name) as cnt
from A;
If you want to filter, then you can use a subquery:
select a.*
from (select Name, count(*) over (partition by name) as cnt
from A
) a
where cnt >= 2;
Input:
1
2
3
4
.
.
.
Expected Output:
1
2
2
3
3
3
4
4
4
4
.
.
..
.
Need to find the Hive /SQL query for the expected output like this
Below is the query, have used CTE with 3 records as my input.
hive> with t1 as (select 1 as col1 union select 2 as col1 union select 3 as col1) select t11.col1 from t1 as t11,t1 as t12 where t11.col1>=t12.col1;
I have a table 'TempC3'
Itemset itemset2
1 3
2 3
2 5
3 5
I want combination of elements in these columns without repetition. So the output table shall be
Itemset itemset2 Itemset3
1 3 5
2 3 5
1 2 3
I designed a query but it wont return the last row of the desired output table -
Select distinct a.Itemset,
a. Itemset2,
c.itemset2
from TempC3 a
Join TempC3 c
ON c.Itemset2 > a.Itemset2
This query only results this:
Itemset itemset2 Itemset3
1 3 5
2 3 5
Since you want all combinations of itemsets, you have to concatenate the two columns in your input table into a single column first. You could do this, for example, using a CTE:
Fiddle Here
WITH CTE AS (
SELECT Itemset FROM TempC3
UNION
SELECT Itemset2 FROM TempC3
)
SELECT I1.Itemset, I2.Itemset, I3.Itemset FROM CTE AS I1
INNER JOIN CTE AS I2 ON I2.Itemset > I1.Itemset
INNER JOIN CTE AS I3 ON I3.Itemset > I2.Itemset