I have SQL to get 5 rows... how do I get the max value from this fetch. for example I want 1990.75.
here are the results of the fetch
1990.25
1990.50
1990.00
1900.00
1990.75
Or if there is a better way? I need to get the last 5 records which are already sorted by date DESC and time DESC in the table (the 5 may change to another number)
DECLARE #CurrentSetNumber int = 0;
DECLARE #NumRowsInSet int = 5;
SELECT [Stock_High]
FROM [dbo].[HistData]
Where BarSize = '5 mins'
Order by RecordID
OFFSET #NumRowsInSet * #CurrentSetNumber ROWS
FETCH NEXT #NumRowsInSet ROWS ONLY;
SET #CurrentSetNumber = #CurrentSetNumber + 1;
TIA
The 5 rows/values that you have after sorting, stores those 5 values into a table variable or temp table and then get max of values from temp table.
Related
I am stuck with a the following problem in SAS. I have a dataset of this format:
The dataet consists of 500ids with different number of observations per ID. I'm trying to randomly select 5id's and at the same time retain all of their observations. I built a random generator in the first place saving a vector with 10 numbers in the interval [1,500]. However it became clumpsy when I tried to use this vector in order to select the ids correspoding to the vector with the random numbers. To be more clear, I want my net result to be a dataset which includes all observations correspoding to ID 1,10,43, 22, 67, or any other sequence of 5 numbers.
Any tip will be more than appreciated!
From your question, I assume you already have your 10 random numbers. If they are saved in a table/dataset, you can run a left join between them and your original dataset, by id. This will pull out all the original observations with the same id.
Let's say that your ramdonly selected numbers are saved in a table called "random_ids". Then, you can do:
proc sql;
create table want as
select distinct
t1.id,
t2.*
from random_ids as t1
left join have as t2 on t1.id = t2.id;
quit;
If your random numbers are not saved in a dataset, you may simply copy them to a where statement, like:
proc sql;
create table want as
select distinct
*
from have
where id in (1 10 43 22 67) /*here you put the ids you want*/
quit;
Best,
Proc SURVEYSELECT is your friend.
data have;
call streaminit(123);
do _n_ = 1 to 500;
id = rand('integer', 1e6);
do seq = 1 to rand('integer', 35);
output;
end;
end;
run;
proc surveyselect noprint data=have sampsize=5 out=want;
cluster id;
run;
proc sql noprint;
select count(distinct id) into :id_count trimmed from want;
%put NOTE: &=id_count;
If you don't have the procedure as part of your SAS license, you can do sample selection per k/n algorithm. NOTE: Earliest archived post for k/n is May 1996 SAS-L message which has code based on a 1995 SAS Observations magazine article.
proc sql noprint;
select count(distinct id) into :N trimmed from have;
proc sort data=have;
by id;
data want_kn;
retain N &N k 5;
if _n_ = 1 then call streaminit(123);
keep = rand('uniform') < k / N;
if keep then k = k - 1;
do until (last.id);
set have;
by id;
if keep then output;
end;
if k = 0 then stop;
N = N - 1;
drop k N keep;
run;
proc sql noprint;
select count(distinct id) into :id_count trimmed from want_kn;
%put NOTE: &=id_count;
My query returns something like this:
I want to save each value into array. I know how to save each column of a row using SELECT INTO, but I don't know how to save rows of a table with just one column.
I would like to get this:
my_array(1) = 11111
my_array(2) = 22222
my_array(3) = 33333
....
Length of an array is 6. I know that my query will not return more than 6 rows.
If query returns less than 6 rows is it possible to put NULL into array element where there is no rows for it?
As suggested in a comment you may use BULK COLLECT
select your_col BULK COLLECT
INTO your_collection from my_array where some_condition = 'something';
Regarding your question
if query returns less than 6 rows is it possible to put NULL into
array element where there is no rows for it
You have not said why it's required but a BULK COLLECT will create as many elements in the array as the number of rows present in the query result. In case you need NULL elements, you may use count and extend collection methods to check and allocate null elements until the count is 6.
DECLARE
TYPE myarrtype is table of integer;
my_array myarrtype;
BEGIN
select level bulk collect into my_array from dual
connect by level <= 4; --generates 4 integers 1-4 and loads into array.
dbms_output.put_line('OLD COUNT '||my_array.count);
if my_array.count <= 6 then
my_array.extend(6 - my_array.count); --This appends required number of
--NULL elements
dbms_output.put_line('NEW COUNT '||my_array.count);
end if;
END;
/
Output
1 rows affected
dbms_output:
OLD COUNT 4
NEW COUNT 6
Demo
I have a SSRS report in which there are 3 columns each contain 3 different subreports in a table. Requirement is 1st subreport column should return 27 rows, 2nd : 25 rows and 3rd:26 rows. Is it possible in SSRS ? If yes How ?
You can do this.. using row_number and Mod.
I'm simply generating a list of numbers from 1 - 100 below.. lets assume that this is your dataset. Create a new column using row_number and partition it by mod 25 (27 or 26 as you require) against this dataset. Now you have a unique value every X number of rows..
declare #start int = 1
declare #end int = 100
;with mycte as (
select distinct n = number
from master..[spt_values]
where number between #start and #end
)
Select
*
,ROW_NUMBER() OVER (PARTITION BY (mycte.n % 25) ORDER BY (n) )rn
from mycte
order by 1,2
Now in SSRS, against each subreport add this column, add a parent group, grouping by this newly generated row number (RN in this case). Remove any columns that SSRS adds after grouping, but keep the grouping..
Set the group property to pagebreak in between each instance of the groups.. Done!
let's assume I have a table tab1 in my Oracle DB 12.1, which has a column record_id (type NUMBER) and many other columns, among them a column named exchg_id.
This record_id is always empty when a batch of new rows gets inserted into the table. What I need to do is to populate the record_id with values 1..N for all rows that satisfy a condition ...WHERE EXCHG_ID = 'something' and number of such rows is N. Of course I know how to do this procedurally (in a for-loop), but I'd like to know if there's an faster way using a single UPDATE statement. I imagine something like this:
UPDATE tab1 SET record_id = {1..N} WHERE exchg_id = 'something';
Many thanks for your help!
UPDATE: the order of the rows is not important, I need no specific ordering. I just need unique record_id's 1..N for any given exchg_id.
You could use rownum to set record_id to 1 to N :
UPDATE tab1 SET record_id = rownum WHERE exchg_id = 'something';
If you have some offset, say 10, then use rownum + 10
I have a query which fetch record from one db and insert it into another db across servers using script.
the query is like :-
select id, date, iscomplete
from sourcedb.test where id = '1'
and date = '2011-03-15' and iscomplete = 1;
this query returns me some records, which i want only when there are certain number of records.
for ex:- there are 10 records for '2011-03-15' then I want to fetch those 10 records only when is complete is 1 for all the 10 records.
I dont want to hardcode it as records can increase in near future from 10 to 20.
Is there any way I can modify my original query and check if iscomplete = 1 for all the records for that day then fetch the records else return nothing.
I need to add one more condition to this query that is if there are 10 records and half of them are completed i.e. isComplete = 1 and half of them are isComplete <> 1 in this case I dont want any output from the query untill and unless all the record has isComplete = 1.
Regards,
Manasi
Just make the next check
select id, date, iscomplete
from sourcedb.test
where id = '1'
and date = '2011-03-15'
and not exists (select 1 from sourcedb.test where id = '1'
and date = '2011-03-15' and isComplete <> 1);