Sort by Date range asc - sorting

I need some quick and simple help with a query.
=query('Active Workstreams [update me] 10.2'!B6:L306, "SELECT B,C,D,I,J,L where C > date '"&TEXT(A8,"yyyy-mm-dd")&"' and C <= date'"&TEXT(B8,"yyyy-mm-dd")&"'",1)
What and where do I need to add to the order by C asc?
I have tried a few different versions but keep getting errors.

Try this
=query('Active Workstreams [update me] 10.2'!B6:L306,
"SELECT B,C,D,I,J,L where C > date '"&TEXT(A8,"yyyy-mm-dd")&"' and C <= date'"&TEXT(B8,"yyyy-mm-dd")&"' order by C asc ",1)

the order of argument is as follows:
select
where
group by
pivot
order by
limit
offset
label
format
options
while you can skip any of them (yes even select) the order must be preserved

Related

Small detail when using this function in Google Sheets

Small detail when using =INDEX($A$8:$A$11;MATCH(MAX(SUMIF($B$8:$B$11;$B$8:$B$11));SUMIF($B$8:$B$11;$B$8:$B$11);0))) If the values in column B are all different it returns the correct date value, but if two identical values in column B coincide in different dates then it returns the date of the first value; it does not return the correct date and it keeps the first one that has the repeated value.
Any idea?
p.s This question can be added to this post
Even more easier way:
On E2 Try this =TRANSPOSE(INDEX(QUERY(A1:B," select A, sum(B) group by A Order By sum(B) Desc "),2))
and format the date and currency accordingly.
You can do that easily and differently to get:
1 - Make a helper table to get unique dates, You can use two ways
a) - Use SUMIF Function to get the sum of Expenditure in each unique date Like so =IF(D2="",,SUMIF($A$2:$A,D2,$B$2:$B)) and drag it down.
b) - By using QUERY Function =QUERY(A1:B11," select A, sum(B) group by A Order By sum(B) Desc ")
2 - to get SUM BY DATE OF HIGHEST EXPENDITURE: =MAX(E2:E)
3 - to get DATE BY HIGHEST EXPENDITURE: =INDEX($D$2:$D,MATCH($H$3,$E$2:$E,0),1)
Make a copy of this sheet "make it yours."
Hope that answerd your question.

I'm needing to produce a set of missing records in oracle

I have a source table and a branch table.
I need to update the sequence by 1 starting with the max value and populate a branch number not already being used. My results should be Seqno starting at 1409 with a new branch number not in the source table already.
Here's what I have so far. I get all values 1409, but they need to be 1410, 1411,... and so on
SELECT S.SOURCEID
, MAX(S.SEQNO) + 1 AS NEWSEQ
, B.BRANCH_NO
FROM SOURCE S, BRANCH B
WHERE S.SOURCEID = '607'
AND B.BRANCH <> S.BRANCH
GROUP BY S.SOURCEID,B.BRANCH,S.SEQNO
Don't use MAX(seqno) + 1 to generate the values (you will end up with duplicates if you run the query on a parallel system). Instead, create a sequence:
CREATE SEQUENCE source__seqno START WITH 1409;
Then you can use:
INSERT INTO source (sourceid, seqno, branch)
SELECT S.SOURCEID
, source__seqno.NEXTVAL
, B.BRANCH
FROM SOURCE S
INNER JOIN BRANCH B
ON (B.BRANCH <> S.BRANCH)
WHERE S.SOURCEID = '607'
db<>fiddle here
You need to use ROWNUM instead of 1.
SELECT S.SOURCEID
,(SELECT MAX(SEQNO) FROM SOURCE) + ROWNUM AS NEWSEQ
,B.BRANCH_NO
FROM SOURCE S
JOIN BRANCH B ON B.BRANCH <> SB.BRANCH
WHERE S.SOURCEID = '607'
GROUP BY S.SOURCEID,B.BRANCH
Using only the MAX condition will give the max from the query not from the whole table. So I have used a sub-query for that. Also, I have changed your join to modern syntax.
P.S. - Not sure why you have used <> condition instead of the equality condition. Normally we use equality condition for joins.

How to make ORDER BY case-insensitive?

I'm working with ABAP and OpenSQL and I think I'm running version 7.5, but I'm not really sure about this.
I try to use ORDER BY in my SELECT. My problem is that upper case letters will first shown and than lower case like this:
A B C D E F... a b c d e f - but of course I want it like this: A a B b C c D d E e F f ...
I've tried to to it with ORDER BY UPPER( column2 ) and ORDER BY LOWER( column2 ), but I always get following error (same with lower):
Unknown column name "UPPER( column2 )". until runtime, you cannot
specify a field list.
Here is my code:
SELECT * FROM <database table>
WHERE column1 = #inputParameter
ORDER BY column2
INTO CORRESPONDING FIELDS OF TABLE #export_structure
Or you could just sort the results afterwards on the ABAP layer using:
SORT export_structure BY column2 AS TEXT.
The addition AS TEXT does the sorting according to the alphabetic sorting rules for the current locale. Unfortunately it's unavailable in the ORDER BY clause of a SELECT.
This solution should also work fine on very old releases.
I dont think the Order by supports functions, even though some dynamic features are available. Docu says it is only limited to the column syntax.
Well as of 7.51 at least.
SAP 7.51 Select order by docu
When your system is on Release 7.51 or later, then you can use a WITH ... SELECT.
WITH
+tmp AS (
SELECT name_first,
UPPER( name_first ) AS name_first_upper
FROM adrp
WHERE persnumber = #inputParameter )
SELECT name_first FROM +tmp
ORDER BY name_first_upper
INTO TABLE #DATA(lt_data).
The WITH-Keyword allows you to define one or more "temporary" SELECTs and then perform a SELECT query on the result-sets of those SELECTs.

Hadoop Hive MAX gives multiple results

I am trying to get a maximum value from a count selecting 2 label srcip and max, but everytime I include srcip I have to use group by srcip at the end and gives me result as the max wasnt even there.
When I write the query like this it gives me the correct max value but I want to select srcip as well.
Select max(count1) as maximum
from (SELECT srcip,count(srcip) as count1 from data group by srcip)t;
But when I do include srcip in the select I get result as there was no max function
Select srcip,max(count1) as maximum
from (SELECT srcip,count(srcip) as count1 from data group by srcip)t
group by srcip;
I would expect from this a single result but I get multiple.
Anyone has any ideas?
You may do ORDER BY count DESC with LIMIT 1 to get the scrip with MAX of count.
SELECT srcip, count(srcip) as count1
from data group by srcip
ORDER BY count1 DESC LIMIT 1
let's consider you have a data like this.
Table
Let's see what happens when you run following query, what happens to data.
Query
SELECT srcip,count(srcip) as count1 from data group by srcip
Output: table1
Now let's see what happens you run your outer query on above table .
Select srcip,max(count1) as maximum from table1 group by srcip
Same Output
Reason being your query says to select srcip and maximum of count from each group of srcip. And we have 3 groups, so 3 rows.
The query below returns exact one row having the max count and the associated scrip. This is the query based on the expected result; you would rather look more into sql and earlier comments, then progress to hive analytical queries.
Some people could argue that there is better way to optimize this query for your expected result but this should give you a motivation to look more into Hive analytical queries.
select scrip, count1 as maximum from (select srcip, count(scrip) over (PARTITION by scrip) as count1, row_number() over (ORDER by scrip desc) as row_num from data) q1 having row_num = 1;

Using rownum in subquery

In an algorithm the users passes a query, for instance:
SELECT o_orderdate, o_orderpriority FROM h_orders WHERE rownum <= 5
The query returns the following:
1996-01-02 5-LOW
1996-12-01 1-URGENT
1993-10-14 5-LOW
1995-10-11 5-LOW
1994-07-30 5-LOW
The algorithm needs the count for the select attributes (o_orderdate, o_orderpriority in the above example) and therefore it rewrites the query to:
SELECT o_orderdate, count(o_orderdate) FROM
(SELECT o_orderdate, o_orderpriority FROM h_orders WHERE rownum <= 5)
GROUP BY o_orderdate
This query returns the following:
1992-01-01 5
However the intended result is:
1996-12-01 1
1995-10-11 1
1994-07-30 1
1996-01-02 1
1993-10-14 1
Any idea how I could rewrite the parsing stage or how the user could pass a syntactically different query to receive the above results?
The rows returned by the inner query are essentially non-deterministic, as they depend on the order in which the optimiser identifies rows as part of the required data set. A change in execution plan due to modified predicates might change the order in which the rows come back, and new rows added to the table can also change which rows are included.
If you always want n rows then either use distinct(o_orderdate) in the innerquery, which will render the GROUP BY useless.
Or you can add another outer select with rownum to get n of the grouped rows, like this:
select o_orderdate, counter from
(
SELECT o_orderdate, count(o_orderdate) as counter FROM
(SELECT o_orderdate, o_orderpriority FROM h_orders)
GROUP BY o_orderdate
)
WHERE rownum <= 5
Although the results will most likely be useless as they will be undeterministic (as mentioned by David Aldridge).
As your outer query makes no use of "o_orderpriority", why not just get rid of the subquery and simply query like this:
SELECT o_orderdate, count(o_orderdate) AS order_count
FROM h_orders
WHERE rownum <= 5
GROUP BY o_orderdate

Resources