How to sum the values of a column based on minimum of two values in another column? - subquery

I am using Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production.
There is a task date, shift and volume column in table.
image.png
I want to sum the volume from the task_date 10 PM to next day 6 AM and display the total for each night shift with the previous day date(i.e the date on which the shift starts at 10 PM )
This needs to be done for a period of date ranges which will be input as part of this SQL query.
for example: Total picked volume for one night shift from 13-07-2022 22:00 to 14-07-2022 02:00 will be 110 for the date 13-jul-2022 and so on.
Please find the create/insert scripts for the sample case. The Shift needs to be created in the query itself.
create table duumy_t
(
task_end_date date,
picked_volume number
);
insert into duumy_t values ('12-jul-2022 01:00:00',20);
insert into duumy_t values ('12-jul-2022 02:00:00',20);
insert into duumy_t values ('12-jul-2022 22:00:00',30);
insert into duumy_t values ('12-jul-2022 23:00:00',40);
insert into duumy_t values ('12-jul-2022 23:00:00',200);
insert into duumy_t values ('13-jul-2022 01:00:00',400);
insert into duumy_t values ('13-jul-2022 01:00:00',20);
insert into duumy_t values ('13-jul-2022 02:00:00',20);
insert into duumy_t values ('13-jul-2022 22:00:00',30);
insert into duumy_t values ('13-jul-2022 23:00:00',40);
insert into duumy_t values ('14-jul-2022 01:00:00',20);
insert into duumy_t values ('14-jul-2022 02:00:00',20);
insert into duumy_t values ('14-jul-2022 22:00:00',20);
insert into duumy_t values ('14-jul-2022 23:00:00',20);
insert into duumy_t values ('14-jul-2022 14:30:00',20);
insert into duumy_t values ('14-jul-2022 10:30:00',20);
commit;
Thanks

Related

Excel formula in Power Query

I would like to use the following excel (table) function in Power Query:
{=IF([#[Month_N]]=MIN(IF([Pers_ID]=[#[Pers_ID]];[Month_N]));TRUE;FALSE)}
It gives me for a distinct Pers_ID with the lowest Month_N number a TRUE and otherwise a FALSE.
Is there a way to do this in Power Query?
Reason: I would like to automate different steps in Excel via power query so I can start analyzing my data right away.
Assuming your table is added to Power Query as Table1:
Duplicate the Table1 query, renaming this new query as Table2
Add a step to the Table2 query in which you use the Group By feature, grouping on Pers_ID, with New column name=MinMonth, Operation=Minand Column=Month_N
Back in the Table1 query, Merge Queries (not as New) on Table 1 and Table2, choosing the Pers_ID column from both, Left Outer join.
Expand the Table2 column after this merge, selecting the MinMonth column only
Add a simple Calculated Column to check if the entry in the MinMonth column is equal to that in the Month_N column
Delete unwanted columns as required

Filter out / hide OR replace excel dax measure 0 values

I have the following dax measure in the value area of a pivot table to return a concatenated list of unique values from a table:
=CONCATENATEX(
Values(
Table1[Column2]
),
Table1[Column2],", "
)
There are instances where 0 is returned as the value because no corresponding values exists for that row in the source data.
Is it possible to either replace with "-" or preferrably hide or filter out these rows entirely.
Any ideas?

Two-level sorting in Excel with one as Number and second as date

So I have a sheet with almost 50 columns and I have to sort them according to only two columns, say, ID and DATE, where ID is a number.
What I want is the data should be sorted first by ID(ascending) and than by DATE (descending, newer date first).
Problem is, whatever I am trying the data is sorted by Excel for DATE column in ascending order , i.e, earlier date is coming first not in descending.
Can anyone suggest me more ideas?
Solution 1: Check value type
Check the type of the date column. It has to be a number to be sorted correctly.
The safest version-independent check:
select an unused column in your table (in the following assumung that your the date coloumn is 'B', the data rows starting in row 2)
put the formula "=TYPE(B2)" in that cell.
If the result is 1, your Date value is a number (at least in that row)
If the result is 2, your Date value is a string that might behave unexpectedly when you sort.
Solution II: Assure the values being numbers
(Caution: Befeor starting, make sure that you havbe backed up your data)
Put the number 1 into an empty cell somwhere on your sheet
Copy the cell (important! not only the formula)
select all fields in your data column
Hit the right Mouse key, select 'insert contents'
Select the options 'values' and 'multiply'
If the 'multiply' option is not available immediately within the 'insert contents'-submenu when you hit the right mouse button (depends on the excel version) select the 'Insert Contents...' sub-sub-Menu to get the complete insert values-dalog.
Hit 'OK'
If (some of) your date values are turned into 5-digit figures, they were in deed strings. Don't panic! Change the number format of the entire date coloumn to the date format of your liking.
Generally:
In the excel sort dialog, you can always select the sorting order. If the data are OK, the result is also.

how to frame a insert query with values in A cell

I have a spreadsheet with values
A
1000
2000
3000
4000
i want to frame a query like
insert into table_name values(a1);
insert into table_name values(a2);
insert into table_name values(a3);
how can i do this
you are probably looking for the function concatenate()
=CONCATENATE("insert into table table_name values(",A2,");")
here A2 is the Cell in which your value is contained.
Step 1> Goto column beside the one with values
Step 2> Apply the above formula
Step 3> Copy paste the formula for all the cells in the new column. This will take care of generating the final values.

Select a subset of a table for further processing

In Microsoft Excel, how can I compute a range (portion of a column), based on the values in another column of the same table, returning the result in an Array form for further processing by other functions?
In SQL, what I mean is "SELECT field1 FROM table WHERE field2=value".
The selected results will be fed (twice) to FREQUENCY(), to compute the number of distinct entries in "field1". That is: given an existing table like this:
Box Date
1 07/01/12
13 07/01/12
13 07/01/12
27 07/18/12
13 07/18/12
55 07/18/12
I want to produce a resulting table like this:
Boxes Date
2 07/01/12
3 07/18/12
Note that "13" is only counted once in the first date ("distinct"), but it's still counted again in the second date.
I already have an expression that does the right thing for the whole of the table,
=SUM(N(FREQUENCY(Box,Box)>0))
where "Box" is a named range of the first table, consisting of the whole Box column. (Using the same range/array/list as the data and the bins for FREQUENCY is a stupefyingly subtle trick actually contained in the Excel help but -- alas! -- by no means adequately explained.)
But I want (several) subsets, one for each date. I want to expand my "SUM(N(FREQUENCY…" expression to act only on the rows of the first table whose Date column matches the Date column of the row being computed. That is, again resorting to SQL,
SELECT count(DISTINCT t1.Box), t2.Date
FROM `t1` JOIN `t2` ON (Date)
GROUP BY Date
I can even build a pivot table of the interesting values (which gets me counts in its cells), then use a parallel, date-indexed column of
=COUNTIF(…)
to reduce each row of counts down to a single count of uniques for that date. But this requires me to update the pivot table to notice new data in the base table, and then to drag-expand the column of answers to include the new date (or suffer ugly value error markers). So something more automatic, less fussily manual, would be sweet.
I guess not available when you asked, but Excel 2013 has Distinct Count as an option in a PivotTable:

Resources