Thanks for looking into the query.
Trying to find a solution of Oracle connect by prior functionality in Unix.
Sample input file
ColA , ColB
Join , Filter
Aggregate, Target
Filter , Aggregate
Source , Join
Requirement is that one record will be identified as start Row - (Here where ColA value is Source)
And now based on joining ColB and ColA value row orders will be sorted
So first row is
Source , Join
Second Row is where ColB value of First row is equal to ColA value of Second Row
So Second Row -
Join , Filter
Third Row is where ColB value of Second row is equal to ColA value of Third Row
So Third Row -
Join , Filter
With this logic output will be
ColA , ColB
Source , Join
Join , Filter
Filter , Aggregate
Aggregate, Target
Related
I use a formula like this to get stock data:
=IMPORTDATA("https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY_EXTENDED&symbol=IBM&interval=15min&slice=year1month1&apikey=demo")
Basically my goal is to filter the data by date and time with a query formula, so the table with the raw data does not show up.
I want the data from Column 1, Column 3 and Column 6, filtered by date (in this case 7/12/2022 from cell J1) and time (between 4:15:00 and 9:30:00).
I tried this formula
=QUERY(IMPORTDATA("https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY_EXTENDED&symbol=IBM&interval=15min&slice=year1month1&apikey=demo"),"select Col1, Col3, Col6 WHERE Col1 > datetime '"&TEXT(J1+time(4,15,0),"yyyy-mm-dd HH:mm:ss")&"' and Col1 <= datetime '"&TEXT(J1+time(9,30,0),"yyyy-mm-dd HH:mm:ss")&"'")
but the only result I can get are the headers.
Here is a link to the Sheet
Answer
The following formula should produce the result you desire:
=QUERY(IMPORTDATA("https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY_EXTENDED&symbol=IBM&interval=15min&slice=year1month1&apikey=demo"),"SELECT Col1, Col3, Col6 WHERE Col1 > "&J1+TIME(4,15,0)&" AND Col1 <= "&J1+TIME(9,30,0))
For easier reading, here is the second argument of =QUERY isolated.
"SELECT Col1, Col3, Col6 WHERE Col1 > "&J1+TIME(4,15,0)&" AND Col1 <= "&J1+TIME(9,30,0)
Explanation
Behind the scenes, all date and time values in Google Sheets are stored as simple numbers. Therefore, simple number comparison can be used in a query to determine if one date is greater than another, skipping a lot of the in-between with the =TEXT function and the datetime argument. The provided =QUERY simply compares each value in Col1 to the sum of J1 and your provided time value.
Functions used:
=QUERY
=IMPORTDATA
=TIME
I have two columns
tab1
col1 col2
a 1
a 2
b 3
b 6
c 4
d 5
I need distinct values from both columns at once like:
result
col1 col2
a 1
b 3
c 4
d 5
select distinct col1, col2 from tab1 #is not giving such results.
Is that possible in sqlite?
From the sample data you posted I see that the easiest way is to group by col1 and get the minimum of col2:
select col1, min(col2)
from tab1
group by col1
To get your expected result (which is not distinct rows, but other thing).
What you want formally is not clear, below will select minimum values of column 2 for each column 1.
select col1, min (col2) from tab1 group by col1
If you want to select first of each, let it be known to you, that order of values is not defined in SQL unless e.g. you add numeric id and sort by it.
NOTE: noted forpas answered before me, so please mark his answer as accepted in case you consider our answers the same.
I have a table like this:
I want to group by this 3 columns (col1, col2, col3) with a specific sequence, to get something like this:
Is there any kind of function to do this?
Analytical function Dense_Rank() would work well here.
SELECT col1 , col2 , col3
, DENSE_Rank() OVER (ORDER BY COL1, COL2, COL3) AS MYGROUP
FROM REFTABLE
Rank() vs Dense_rank() dense rank will keep numbers sequential w/o gap whereas rank will introduce gaps. In your example 3, B, Y twice may be assigned rank 3 but the next value would be assigned rank 5 if rank was used 4 if dense_rank() is used.
Link to Docs
Note each time the query runs the Dense_rank is assigned so #'s can change as data changes. If you want the numbers to be unique then you would need to store them. and then force Dense_Rank to start at a seed being the highest # in the stored results.
add rownum will add sequence number per row:
select col1, col2, col3, rownum as mygroup from table a;
There's a particular SQLIte command I want to run to perform a aggregate and concatenate operation.
I need to aggregate by "ID" column, then for each ID, concatenate unique 'Attribute' and also concatenate average of 'Value' for each unique corresponding 'Attribute':
I can do concatenate unqiue Attribute and aggregate by ID, but haven't got average of Value working.
Try to use subquery for getting AVG for combination of id+attribute and then use group_concat:
select t.id, Group_Concat(t.attribute) as concat_att, Group_Concat(t.avg) as concat_avg from
(
select test.id, test.attribute, AVG(test.value) as avg from test
group by test.id, test.attribute
) as t group by t.id;
See this example here: http://sqlfiddle.com/#!7/03fe4b/17
I want to calculate the sum of a calculated textbox i.e
Table1 has two columns colA bind with database [socre] and
ColB has an expression
[exp] i.e [score]/sum[score]*100
what i want is
TOTAL of colB sum[colB]
Please suggest me any suggestion.