Do query value based on integer length - sql

I have table1 like this
and the expected result like this
How I can do that? I don't know what I must search.

use REPEAT
https://w3resource.com/PostgreSQL/repeat-function.php
select col1,col2,REPEAT(col1,col2) from table1

PostgreSQL has a neat repeat function:
SELECT col1, col2, REPEAT(col1, col2)
FROM table1

Related

Using sum in select statement without GROUP by

select
Col1,
Col1/sum(Col1) as Fraction
from MyTable
This one will not return the desired result. If I add Gruop by Col1 then it will return a bunch of 1'a in the second column. How can I get around the problem?
Basically I want to treat the sum(Col1) as constant parameter. I can use with statement but I want a solution where I don't add another select statement.
I am using Toad and selecting from an Oracle database
Use SUM in its analytic form:
select
col1,
col1 / sum(col1) over () fraction
from mytable

How to use where clause to match the combination of multiple columns in one go without using AND or OR clauses

I wanted to match the combination of multiple columns as that from another table or the same table itself.
For example, see table1 and table2.
So, I want the output like below.
So far, I have been using AND clause to achieve this like below:
select * from table1
where col1 in (select col4 from table2)
and col2 in (select col5 from table2)
and col3 in (select col6 from table2)
But it is not giving me the exact output. So, I am looking for such a query which can work for my scenario. Any help will be appreciated.
I found the answer to my own question and SQL query for the same is given below.
select * from table1
where (col1,col2,col3) in (select col4,col5,col6 from table2);
But still, if anybody has any better solution then it will be appreciated.
What you need is INTERSECT set operator which is used to return the results of multiple (two or more) SELECT statements for those each column values should be equal for the respective order of columns.
So, consider using as below :
SELECT * FROM table1
INTERSECT
SELECT * FROM table2
Demo

How to get all records within a specific group of a GROUP BY result in pure SQL

Do you know the best way to get all records within a specific group of a GROUP BY results in pure SQL (by index if it's possible)?
Update:
SELECT col1, col2 from my_table GROUP BY col1, col2 where col1 = 123
If I'm understanding the question correctly, you used the GROUP BY clause to group your data by particular column data. If you have a specific value you would like to pull by using that column's data then a WHERE clause would be your best bet at getting the data you're looking to get
Easiest way, if I understand your question properly is to use HAVING clause in your query.
CREATE TABLE my_table (col1 INT, col2 INT);
GO
INSERT INTO my_table VALUES
(1,2),(3,4),(123,1),(123,2);
GO
SELECT col1,col2 FROM my_table GROUP BY col1,col2 HAVING col1=123
GO
DROP TABLE my_table
GO

How to manipulate a column selected by * in SQLite?

I want a query to return all rows and all columns with one caveat: if, in a given row, colN is null, then instead return the string 'FOO'.
Why dont I just use SELECT col1, col2, ..., COALESCE(colN, 'FOO')?
I am implementing an abstract interface and thus I am required to use SELECT queries which SELECT * (because I cannot make assumptions on what columns there are). I can only assume 1 columns exists: colN.
What would this provide me?
I need this because this query is used in combination with a UNION and this allows me to keep track of the origin of the data.
Any ideas on how to do this?
One thing you could do is
SELECT *, COALESCE(colN, 'FOO') as CoalescedColN
if it's possible to adjust the other select(s) in the UNION accordingly
I don't know if SQL Lite can use this technique but this is what I would do in most other dbs:
select * from
(SELECT col1, col2, ..., COALESCE(colN, 'FOO') from table ) a

Is there a difference between DISTINCT colname and DISTINCT(colname)?

I've seen both versions around. On iSeries DB2 you can use either and as far as I can tell they do the same thing. Is there a difference?
No, there is no difference because DISTINCT is a keyword and not a function call.
It's the same difference as between SOME_COLUMN and (SOME_COLUMN) (without any keyword in front)
If you have only one column in your select, then there is no difference.
However when you use distinct outside as -
select disctinct col1, col2, col3 from table
It applies distinct on the group tuple of (col1, col2, col3).
Finally there is no difference in using distinct as select distinct or select distinct()