Build a query in Oracle 11g with rownum in output - oracle

I have a scenario and build a Oracle query for it.
Table Columns: Plan_No, Invoice_No,Order_Dt. One plan will have more than one invoice.
I want get this output:
Plan_No Invoice_No Order_Dt Row_No
A1 1001 23-May-17 1
A1 1002 10-Apr-17 2
A1 1003 12-Jan-17 3
A1 1004 11-Nov-16 4
B1 1001 10-May-17 1
B1 2008 10-Feb-17 2
B1 3308 12-Dec-16 3
C1 5007 23-May-17 1
C1 5585 10-Apr-17 2
C1 52545 12-Jan-17 3
C1 5228 11-Nov-16 4
C1 21488 2-Jan-16 5
C1 51546 16-Apr-15 6

I think you can do something like this:
SELECT
ROW_NUMBER() OVER(PARTITION BY plan_no ORDER BY Invoice_No) AS row_nbr,
*
FROM
table

Related

Oracle Apex - Format number in millions

Is there a way to use Currency Format (or some other standard formatter) to format numbers like this in Oracle Apex:
1,000,000 => 1.00M
1,234,567 => 1.23M
1,234,567,890 => 1234.57M
Not declaratively, as far as I can tell. But, you can do it yourself (perhaps).
col is original value
c1 is that number (col) divided by a million
c2 rounds the result to the 2nd decimal (which is OK, but - decimals are missing (see 1)
c3 uses to_char function
final_result is c3 concatenated with an M
Note that col, c1 and c2 are numbers, while c3 and final_result are strings.
SQL> with test (col) as
2 (select 1000000 from dual union all
3 select 1234567 from dual union all
4 select 1234567890 from dual
5 )
6 select col,
7 col/1e6 c1,
8 round(col/1e6, 2) c2,
9 to_char(col/1e6, 'fm99990D00') c3,
10 --
11 to_char(col/1e6, 'fm99990D00') || 'M' final_result
12 from test;
COL C1 C2 C3 FINAL_RESULT
---------- ---------- ---------- --------- ---------------
1000000 1 1 1,00 1,00M
1234567 1,234567 1,23 1,23 1,23M
1234567890 1234,56789 1234,57 1234,57 1234,57M
SQL>

Oracle - Transpose (Pivot)

I have a table like this:
ID
C1
C2
C3
1
1
0
1
2
0
1
1
3
1
0
1
and I want to get to this:
ID
Category
Category_Value
1
C1
1
1
C2
0
1
C3
1
2
C1
0
2
C2
1
2
C3
0
3
C1
1
3
C2
0
3
C3
1
How can I do it? (I will add that I need It dynamic, because today I have 40 columns next week I can have 50 columns)
Thank you!
Another dynamic option is to Use UNPIVOT -
SELECT * FROM DATA
UNPIVOT (
Category_Value FOR Category IN (C1 AS 'C1', C2 AS 'C2', C3 AS 'C3')
);
Still you have to specify the column names as a list. If you want a fully dynamic query, You should try doing this at the presentation layer instead of database layer.
Demo.
One straightforward approach uses a union query:
SELECT ID, 'C1' AS Category, C1 AS Category_Value FROM yourTable
UNION ALL
SELECT ID, 'C2', C2 FROM yourTable
UNION ALL
SELECT ID, 'C3', C3 FROM yourTable
ORDER BY ID, Category;

How to show whole datas from table A and table B according to table C?

Table A
id
Name
1
A1
2
A2
3
A3
4
A4
5
A5
Table B
id
id_table_A
id_table_C
Name
1
1
1
Test-1
2
2
1
Test-2
3
1
2
Test-3
4
3
2
Test-4
5
3
1
Test-5
6
5
2
Test-6
7
2
2
Test-7
Table C
id
Name
1
C1
2
C2
3
C3
My Question
I want to select all datas from table A and also table B, according to id in table C in Laravel (Query Builder or Eloquent doesn't matter). So, it would showing like these:
C1 would showing:
id_table_A
Name
A1
Test-1
A2
Test-2
A3
Test-5
A4
NULL
A5
NULL
Or when I choose C2, it would showing like:
id_table_A
Name
A1
Test-3
A2
Test-7
A3
Test-4
A4
NULL
A5
Test-6
And C3 will showing like:
id_table_A
Name
A1
NULL
A2
NULL
A3
NULL
A4
NULL
A5
NULL
NB: Sorry I don't know how to simplify my question
You can use a many to many relation
A::class
class A extends Model
{
public function Cs()
{
return $this->belongsToMany(C:class, 'b', 'id_table_a', 'id_table_c')->withPivot('id','name');
}
}
Then use this relation to get what you need
$as = A::with('Cs')->get();

How can I query to get the rows according to the certain column's value in oracle? [duplicate]

This question already has an answer here:
how to duplicate my sql results? [duplicate]
(1 answer)
Closed 2 years ago.
Table A is:
--------------
C1 C2
--------------
A 3
B 2
--------------
select * from
(
select 'A' as C1, 3 as C2 from dual
union all
select 'B' as C1, 2 as C2 from dual
)
I want to get the following result view with one query statement:
--------------
C1 N1
--------------
A 1
A 2
A 3
B 1
B 2
--------------
I need to generate rows as many as C2 value
Is this possible?
Thank you.
We can handle this via the use of a calendar/sequence table. Consider:
WITH nums AS (
SELECT 1 AS val FROM dual UNION ALL
SELECT 2 FROM dual UNION ALL
SELECT 3 FROM dual
)
SELECT
a.C1,
n.val AS N1
FROM TableA a
INNER JOIN nums n
ON n.val <= a.C2
ORDER BY
a.C1,
n.val;
Demo
Note that in practice, you might use a dedicated table containing a sequence of numbers to cover all possible values in your table. Or, you might use an Oracle sequence.
Alternatively:
SQL> with test as
2 (select 'A' as C1, 3 as C2 from dual
3 union all
4 select 'B' as C1, 2 as C2 from dual
5 )
6 select c1, column_value n1
7 from test cross join table(cast(multiset(select level from dual
8 connect by level <= c2
9 ) as sys.odcinumberlist))
10 order by c1, column_value;
C N1
- ----------
A 1
A 2
A 3
B 1
B 2
SQL>

sum of column based on distinct value of other column in Oracle

Table A
A1 A2
1 7
2 8
1 9
Table B
A1 B2
1 2
2 3
i want something like this
select A.A1,sum(case when distinct A.A1 then B2),sum(A.A2) from
A,B
where A.A1=B.A1(+)
group by A.A1
After joining my table will be
A1 A2 B2
1 7 2
2 8 3
1 9 2
Resulting Table
A1 A2 B2
1 7+9 2(only once)
2 8 3
how to get sum of B2 when distinct A1 after joining the tables as stated above.
Thanks in advance
Use JOIN and GROUP BY.
Query
SELECT t1.A1, SUM(t1.A2) AS A1, SUM(t2.B2) AS B2
FROM TableA t1
JOIN TableB t2
ON t1.A1 = t2.A1
GROUP BY t1.A1;
Since table_b.a1 is unique, the best way to do this would be to work out the sum of table_a.a2 first to reduce the number of rows you're joining against, and then join to table_b. Then you don't need to worry about summing the distinct table_b.b2 values, which you would otherwise have to do.
WITH table_a AS (SELECT 1 a1, 7 a2 FROM dual UNION ALL
SELECT 2 a1, 8 a2 FROM dual UNION ALL
SELECT 1 a1, 9 a2 FROM dual),
table_b AS (SELECT 1 a1, 2 b2 FROM dual UNION ALL
SELECT 2 a1, 3 b2 FROM dual)
-- end of mimicking your two tables with sample_data in them;
-- see the sql below:
SELECT ta.a1,
ta.a2,
tb.b2
FROM (SELECT a1, SUM(a2) a2
FROM table_a
GROUP BY a1) ta
INNER JOIN table_b tb ON ta.a1 = tb.a1;
A1 A2 B2
---------- ---------- ----------
1 16 2
2 8 3
If you absolutely must join the two tables first (I don't recommend; this is making more work for the database to do), then you could do something like:
WITH table_a AS (SELECT 1 a1, 7 a2 FROM dual UNION ALL
SELECT 2 a1, 8 a2 FROM dual UNION ALL
SELECT 1 a1, 9 a2 FROM dual),
table_b AS (SELECT 1 a1, 2 b2 FROM dual UNION ALL
SELECT 2 a1, 3 b2 FROM dual)
SELECT ta.a1,
SUM(ta.a2) a2,
MAX(tb.b2) b2
FROM table_a ta
INNER JOIN table_b tb ON ta.a1 = tb.a1
GROUP BY ta.a1;
A1 A2 B2
---------- ---------- ----------
1 16 2
2 8 3
Since there can only be one distinct value for table_b.b2 per table_a.a1, we can just pick one of the values to use via MAX (we could have used MIN or SUM(distinct tb.b2) instead, fyi).

Resources