Sort data-Oracle on two columns with some condition - sorting

I want to sort the data by date and then by sequence number. I have done this sorting but when I check the result I get some unordered data for the sequence number and want to fix that. Let's understand by example-
If you see the attachment-
I get seq_num "24231" after "24229" which is true by date order. But I need seq_num also in order after date sort if those are not.
Requirement-
Seq_num is rolling basis- that is 24220-24235 for every day. It preserves the sequence. So if my sequence ended "24224" on last day the next day's sequence is "24225". So I can't do the sort by seq_num at first. Sorting by date is first priority.
If you check the image I have seq_num in a different order for the "24229-24231-24230-24232"
I need to get this as "24229-24230-24231-24232".
enter image description here
My sample code-
SELECT /*+parallel 32*/SOURCEID,
Sourcename,
Inputfilename,
Substr(Inputfilename,Instr(Inputfilename,'_',-1)+1,5) Seq_Num,
Substr(Inputfilename,1,16) Element_Id,
To_Date(Substr(Inputfilename,Instr(Inputfilename,'_',+1,6)+1,14),'YYYYMMDD HH24:MI:SS') Dt
From Mm_Sequence_Check_Test
Order By 6,4;

Related

Google Sheets Formula - Get Total from filtered dates per row (undefined number of columns)

I have this data in Google Sheets where in I need to get the total of the filtered data columns per row. The date columns are not fixed (may increase over time, I already know how to handle this undefined number of columns). What my current challenge encountered is how can I efficiently get a summary of totals per user based on filtered date columns.
My data is like this:
My expected result is like this:
My current idea is this:
Here is a sample spreadsheet for reference:
https://docs.google.com/spreadsheets/d/1_dByPabStGQvh94TabKxwFeUyVaRFnkBCRf4ioTY5jM/edit?usp=sharing
This is a method to unpivot the data so you can work with it
=ARRAYFORMULA(
QUERY(
IFERROR(
SPLIT(
FLATTEN(
IF(ISBLANK(A2:A),,A2:A&"|"&B1:G1&"|"&B2:G)),
"|")),
"select Col1, Sum(Col3)
where
Col2 >= "&DATE(2022,1,1)&" and
Col2 <= "&DATE(2022,1,15)&"
group by Col1
label
Col1 'Person',
Sum(Col3) 'Total'"))
Basically, its creating an output of User1|44557|8 -- it then FLATTENs it all and splits by the pipe, which gives you three clean columns.
Run that through a QUERY to SUM by the person between the dates and you get what you're after. If you wanted to use cell references for dates, simply replace the dates with the cell references.
To expand the table, change B1:G1 and B2:G2 to match the width of the range.

Generate order number based on a column value with reference to other columns

This is question in Oracle views.I have a table with Emp_id,Start_Period and Key. Sample data is given in Descending order of start period with 201909 on top. Need to generate a column named Key_order. (Finally I am planning to create a view with all 4 columns.)
With the sample data as shown. In the sorted list with Start_period what ever comes in first position with number 1 and then on, when the Key changes order has to increment by one.
That is in row 1 and 2 key is same and order is 1. In row 3 SCD changed to ABC so order has to increment by 1 so order value is 2. 4th position key changes and order becomes 3.
See in 7th and 8th position value is same so order remains 6 for both. I am trying to do this inside a view. Tried RANK() but it is sorting column Key and giving order based on that.
Please help.Sample Data
Set a one in each line that has a different key than the line before. Use LAG for this. Then build a running total of these ones with SUM OVER.
select
emp_id, start_period, key,
sum(chg) over (partition by emp_id order by start_period desc) as key_order
from
(
select
emp_id, start_period, key,
case when key = lag(key) over (partition by emp_id order by start_period desc)
then 0 else 1 end as chg
from mytable
)
order by emp_id, start_period desc;

How to order by field mixed with string and number in Oracle?

These are the field (crane_no) values to be sorted
QC11QC10QC9
I tried the following query:
select * from table order by crane_no DESC
but query results does not give in an order because the field is mixed with staring and number (Example:QC12).
I get following results for above query:
QC9QC11QC10
I want the results to be in order (QC9, QC10, QC11). Thanks
If the data isn't huge, I'd use a regex order by clause:
select
cran_no
from your_table
order by
regexp_substr(cran_no, '^\D*') nulls first,
to_number(regexp_substr(cran_no, '\d+'))
This looks for the numbers in the string, so rows like 'QCC20', 'DCDS90' are ordered properly; it also takes care of nulls.
One approach is to extract the numeric portion of the crane_no columns using SUBSTR(), cast to an integer, and order descending by this value.
SELECT *
FROM yourTable
ORDER BY CAST(SUBSTR(crane_no, 3) AS INT) DESC
Note that I assume in my answer that every entry in crane_no is prefixed with the fixed width QC. If not, then we would have to do more work to identify the numerical component.
select ...
order by to_number( substr( crane_no,3 )) desc

Coldfusion query of queries count by date

I'm trying to get an count based on two dates and I'm not sure how it should look in a query. I have two date fields; I want to get a count based on those dates.
<cfquery>
SELECT COUNT(*)
FROM Table1
Where month of date1 is one month less than month of date2
</cfquery>
Assuming Table1 is your original query, you can accomplish your goal as follows.
Step 1 - Use QueryAddColumn twice to add two empty columns.
Step 2 - Loop through your query and populate these two columns with numbers. One will represent date1 and the other will represent date2. It's not quite as simple as putting in the month numbers because you have to account for the year as well.
Step 3 - Write your Q of Q with a filter resembling this:
where NewColumn1 - NewColumn2 = 1

SQL Query in Oracle showing less records than those satisfying criterion

I need to check whether a user inputted Month-Year combination falls in between from_dt and to_dt column of the table.My Query is As follows:
SELECT emp_code,
CASE WHEN TO_CHAR(TO_DATE(from_dt),'MM') = TO_CHAR(TO_DATE((1||'/'||:for_mon||'/'|| :for_year),'DD/MM/YYYY'),'MM')
THEN LAST_DAY(TO_DATE((1||'/'||:for_mon||'/'|| :for_year),'DD/MM/YYYY')) - TO_DATE(FROM_DT) +1
WHEN TO_CHAR(TO_DATE(from_dt),'DD/MM/YYYY')<=
to_char(TO_DATE((1||'/'||:for_mon||'/'||
:for_year),'DD/MM/YYYY'),'DD/MM/YYYY')
AND TO_CHAR(TO_DATE(to_dt),'DD/MM/YYYY')>=to_char(LAST_DAY(TO_DATE((1||'/'||:for_mon||'/'||
:for_year),'DD/MM/YYYY')),'DD/MM/YYYY')
Then LAST_DAY(TO_DATE((1||'/'||:for_mon||'/'|| :for_year),'DD/MM/YYYY'))
-TO_DATE((1||'/'||:for_mon||'/'|| :for_year),'DD/MM/YYYY')+1
ELSE 0
END AS Leave
FROM TestTable
But this query shows only one employee's leave count for the given month-year combo while other employees' leave count also should be shown as their from_dt and to_dt also falls in the same month-year.
Moreover its is showing no exceptions.
I've tried setting values of NLS_COMP and NLS _SORT too, but with no gain.
When comparing strings, you use lexicographically comparison. That is, you are using the dictionary order. Depending your date string format, the lexicography order might or might not be the same as the "natural" date order:
TO_CHAR(TO_DATE(from_dt),'DD/MM/YYYY')
<=to_char(TO_DATE((1||'/'||:for_mon||'/'||:for_year),'DD/MM/YYYY'),'DD/MM/YYYY')
For example, the following table is lexicographically ordered using the DD/MM/YYYY format:
TO_CHAR(DATA,'DD/MM/YYYY')
01/12/2014
14/10/2013
24/11/2014
As you can see, such format does not preserve "natural" date order, and so is not suitable for date comparison. On the other hand, the YYYY/MM/DD is:
TO_CHAR(DATA,'YYYY/MM/DD')
2013/10/14
2014/11/24
2014/12/01
Either switch to that date format, or better, get rid of those date to string comparison and use plain date arithmetics (take a look at EXTRACT(MONTH FROM ...) for example)

Resources