I have problem with my homework - join shows no data - sql

It's me again - I have a new homework and I don't get any further.
The task is:
Find out the date of flight(DATE_FLY), flight number(FLY_NR), manufacturer(MANUF), type (TYP) and serial number (SER_NR) of all flights that fly from Frankfurt to Dallas between 10.11.2013 and 20.11.2013.
This is the departure_table:
DATE_FLY
FLY_NR
MANUF
TYP
PER_NR
SER_NR
TIME_START
06.07.13
LH-888
Boeing
B747
9fg-he-ztu8
10010071
11.23
08.10.13
LH-238
Airbus
A320
z3et-bwe7
10010072
22.06
13.11.13
LH-341
Boeing
B737
ba23-0012
10010001
10.23
14.11.13
LH-358
Boeing
B737
ba23-0012
10010001
8.17
13.11.13
LH-553
Boeing
B777
xv23-0889
10010002
16.53
15.11.13
LH-421
Boeing
B777
xv56-3142
10010002
14.45
17.11.13
LH-789
Airbus
A330
45-6789
10010003
8.11
14.11.13
LH-112
Boeing
B737
ba23-0034
10010001
8.14
17.11.13
LH-421
Boeing
B777
xv23-0889
10010002
16.26
18.11.13
LH-223
Airbus
A380
ab-45-6xf
10010004
9.45
19.11.13
LH-634
Airbus
A350
5478-awe3
10010005
20.25
18.02.14
LH-238
Airbus
A320
z3et-bwe7
10010072
23.06
And this is the fly_table that I need to join:
FLY_NR
START_FLY
DEST_FLY
TIME_FLY
KM
LH-341
Saarbruecken
Hamburg
1.2
490
LH-358
Saarbruecken
Leipzig
1.1
430
LH-553
Leipzig
Hamburg
.5
290
LH-112
Luxemburg
London
1.1
480
LH-421
Luxemburg
Ankara
2.5
2300
LH-789
Luxemburg
New-York
3.5
8300
LH-223
Frankfurt
Dallas
3.9
8600
LH-634
Frankfurt
Moskau
2.3
2020
LH-888
Frankfurt
Peking
9.5
7780
LH-238
Muenchen
Berlin
1.1
479
I wrote the following query but it showed me "no data found":
select
dp.date_fly, dp.fly_nr, dp.manuf, dp.typ, dp.ser_nr
from
departure_table dp, fly_table fl
where
dp.fly_nr = fl.fly_nr
and start_fly = 'Frankfurt'
and dest_fly = 'Dallas'
and dp.date_fly between 10.11.2013 and 20.11.2013;
I've tried a simple join but I still only got the message "no data found":
select
dp.date_fly, dp.fly_nr, dp.manuf, dp.typ, dp.ser_nr
from
departure_table dp, fly_table fl
where
dp.fly_nr = fl.fly_nr;
I don't know what am I doing wrong... I would be very thankful if somebody could help to solve this problem with the join function.
I need to get this result:
DATE_FLY
FLY_NR
MANUF
TYP
SER_NR
18-NOV-13
LH-223
Airbus
A380
10010004
Thank you!

The column FLY_DATE has a VARCHAR2 format
This is bad practice. If you have a date then store it as a date. If you have to store it as a string (really, don't do that) then store it using ISO8601 formatting YYYY-MM-DD which can be sorted alphabetically into date-order (and not DD-MM-YYYY which cannot be easily sorted).
Since you have it in a (mostly unusable) string format, you need to convert it to a DATE using the TO_DATE function and compare it to DATE literals:
select dp.date_fly,
dp.fly_nr,
dp.manuf,
dp.typ,
dp.ser_nr
from departure_table dp
INNER JOIN fly_table fl
ON dp.fly_nr = fl.fly_nr
where start_fly = 'Frankfurt'
and dest_fly = 'Dallas'
and TO_DATE(dp.date_fly, 'DD.MM.YYYY') between DATE '2013-11-10' and DATE '2013-11-20';
Which, for the sample data:
CREATE TABLE departure_table (DATE_FLY, FLY_NR, MANUF, TYP, PER_NR, SER_NR, TIME_START) AS
SELECT '06.07.2013', 'LH-888', 'Boeing', 'B747', '9fg-he-ztu8', 10010071, 11.23 FROM DUAL UNION ALL
SELECT '08.10.2013', 'LH-238', 'Airbus', 'A320', 'z3et-bwe7', 10010072, 22.06 FROM DUAL UNION ALL
SELECT '13.11.2013', 'LH-341', 'Boeing', 'B737', 'ba23-0012', 10010001, 10.23 FROM DUAL UNION ALL
SELECT '14.11.2013', 'LH-358', 'Boeing', 'B737', 'ba23-0012', 10010001, 8.17 FROM DUAL UNION ALL
SELECT '13.11.2013', 'LH-553', 'Boeing', 'B777', 'xv23-0889', 10010002, 16.53 FROM DUAL UNION ALL
SELECT '15.11.2013', 'LH-421', 'Boeing', 'B777', 'xv56-3142', 10010002, 14.45 FROM DUAL UNION ALL
SELECT '17.11.2013', 'LH-789', 'Airbus', 'A330', '45-6789', 10010003, 8.11 FROM DUAL UNION ALL
SELECT '14.11.2013', 'LH-112', 'Boeing', 'B737', 'ba23-0034', 10010001, 8.14 FROM DUAL UNION ALL
SELECT '17.11.2013', 'LH-421', 'Boeing', 'B777', 'xv23-0889', 10010002, 16.26 FROM DUAL UNION ALL
SELECT '18.11.2013', 'LH-223', 'Airbus', 'A380', 'ab-45-6xf', 10010004, 9.45 FROM DUAL UNION ALL
SELECT '19.11.2013', 'LH-634', 'Airbus', 'A350', '5478-awe3', 10010005, 20.25 FROM DUAL UNION ALL
SELECT '18.02.2014', 'LH-238', 'Airbus', 'A320', 'z3et-bwe7', 10010072, 23.06 FROM DUAL;
CREATE TABLE fly_table (FLY_NR, START_FLY, DEST_FLY, TIME_FLY, KM) AS
SELECT 'LH-341', 'Saarbruecken', 'Hamburg', 1.2, 490 FROM DUAL UNION ALL
SELECT 'LH-358', 'Saarbruecken', 'Leipzig', 1.1, 430 FROM DUAL UNION ALL
SELECT 'LH-553', 'Leipzig', 'Hamburg', .5, 290 FROM DUAL UNION ALL
SELECT 'LH-112', 'Luxemburg', 'London', 1.1, 480 FROM DUAL UNION ALL
SELECT 'LH-421', 'Luxemburg', 'Ankara', 2.5, 2300 FROM DUAL UNION ALL
SELECT 'LH-789', 'Luxemburg', 'New-York', 3.5, 8300 FROM DUAL UNION ALL
SELECT 'LH-223', 'Frankfurt', 'Dallas', 3.9, 8600 FROM DUAL UNION ALL
SELECT 'LH-634', 'Frankfurt', 'Moskau', 2.3, 2020 FROM DUAL UNION ALL
SELECT 'LH-888', 'Frankfurt', 'Peking', 9.5, 7780 FROM DUAL UNION ALL
SELECT 'LH-238', 'Muenchen', 'Berlin', 1.1, 479 FROM DUAL;
Outputs:
DATE_FLY
FLY_NR
MANUF
TYP
SER_NR
18.11.2013
LH-223
Airbus
A380
10010004
fiddle

Related

dynamic sum in oracle sql

I want last column 'Output' as stated in table. here the logic is :
for row 1 : sum of LBKUM & weekly_qty
for row 2 : sum of output of row 1 & weekly_qty of row 2
for row 3 : sum of output of row 2 & weekly_qty of row 3
for row 4 : sum of output of row 3 & weekly_qty of row 4
and so on.
Unlike other running total, I want LBKUM only once (in first row calculation) and in the next steps, output of previous row should be considered as shown in the description.
I hope this much would be sufficient.
Thanks in advance.
LOGSYS
MATNR
PLANT
LBKUM
QTY_WITHDRAWL
PLUS_QTY
WEEK
WEEKLY_QTY
Output
SAP
123456789
1234
1408
387
484
06
97
1505
SAP
123456789
1234
1408
1238
2080
07
842
2347
SAP
123456789
1234
1408
1826
1600
08
-226
2121
SAP
123456789
1234
1408
1786
1920
09
134
2255
SAP
123456789
1234
1408
1445
1120
10
-325
1930
SAP
123456789
1234
1408
1224
800
11
-424
1506
SAP
123456789
1234
1408
1299
1280
12
-19
1487
You want something like:
SELECT week,
FIRST_VALUE(lbkum) OVER (ORDER BY week)
+ SUM(weekly_qty) OVER (ORDER BY week) AS running_total
FROM table_name
Which, for the sample data:
CREATE TABLE table_name (LOGSYS, MATNR, PLANT, LBKUM, QTY_WITHDRAWL, PLUS_QTY, WEEK, WEEKLY_QTY, Output) AS
SELECT 'SAP', 123456789, 1234, 1408, 387, 484, 06, 97, 1505 FROM DUAL UNION ALL
SELECT 'SAP', 123456789, 1234, 1408, 1238, 2080, 07, 842, 2347 FROM DUAL UNION ALL
SELECT 'SAP', 123456789, 1234, 1408, 1826, 1600, 08, -226, 2121 FROM DUAL UNION ALL
SELECT 'SAP', 123456789, 1234, 1408, 1786, 1920, 09, 134, 2255 FROM DUAL UNION ALL
SELECT 'SAP', 123456789, 1234, 1408, 1445, 1120, 10, -325, 1930 FROM DUAL UNION ALL
SELECT 'SAP', 123456789, 1234, 1408, 1224, 800, 11, -424, 1506 FROM DUAL UNION ALL
SELECT 'SAP', 123456789, 1234, 1408, 1299, 1280, 12, -19, 1487 FROM DUAL;
Outputs:
WEEK
RUNNING_TOTAL
6
1505
7
2347
8
2121
9
2255
10
1930
11
1506
12
1487
fiddle

How can select only values that similar in several columns but different in several others?

I have two tables from different databases, and I need to create a report, where there is need to see discrepancy in data:
Table A:
DATE
FLIGHT
AC
DEST
ATD
TDN
14.01.2022
150
AIRB
JFK
02:45
1:35
15.01.2022
152
BOEING
MIA
02:45
1:38
15.01.2022
145
AIRB
SEA
01:25
01:05
Table B:
DATE
FLIGHT
AC
DEST
ATD
TDN
14.01.2022
150
AIRB
JFK
02:45
1:35
15.01.2022
152
BOEING
MIA
02:39
1:38
15.01.2022
145
AIRB
SEA
01:28
01:15
The result should be only rows different in last two columns:
DATE
FLIGHT
AC
DEST
ATD_B
TDN_B
ATD_A
TDN_A
15.01.2022
152
BOEING
MIA
02:39
1:38
02:45
01:38
15.01.2022
145
AIRB
SEA
01:28
01:15
01:25
01:05
Now we can see where discrepancy is.
I have tried
select * from table_a
minus
select * from table_b
But it seems not the right approach
You can - for your sample data - JOIN both tables on the four identic columns and then set a condition that the combination of the two other columns must differ:
SELECT
a.a_date, a.flight, a.ac, a.dest,
b.atd AS atd_b, b.tdn AS tdn_b,
a.atd AS atd_a, a.tdn AS tdn_a
FROM
a JOIN b
ON
a.a_date = b.b_date
AND a.flight = b.flight
AND a.ac = b.ac
AND a.dest = b.dest
WHERE NOT (a.atd = b.atd AND a.tdn = b.tdn);
Sidenote: The condition in the WHERE clause can of course also be added in the join clause and the where clause can be removed.
But this will do only as long as the data is in the same form as your example.
Let's assume the are two further different entries in both table A and B for flight 145. Then I guess you would expect two rows for that flight, but in real life, it will be four.
You can replicate this behaviour here:
db<>fiddle
That's why people are asking you which rows should be put together. If you want to get two rows only for that case, you need to tell us how. If it's really intended to see all four rows, this query will work.
Just join the tables by columns and conditions from your Where clause mentioned in comments:
WITH
A (FLIGHT_DATE, FLIGHT, AC, DEST, ATD, TDN) AS
(
Select '14-JAN-22', 150, 'AIRB', 'JFK', '02:45', '1:35' From Dual Union All
Select '15-JAN-22', 152, 'BOEING', 'MIA', '02:45', '1:38' From Dual Union All
Select '15-JAN-22', 145, 'AIRB', 'SEA', '02:45', '1:05' From Dual
),
B (FLIGHT_DATE, FLIGHT, AC, DEST, ATD, TDN) AS
(
Select '14-JAN-22', 150, 'AIRB', 'JFK', '02:45', '1:35' From Dual Union All
Select '15-JAN-22', 152, 'BOEING', 'MIA', '02:39', '1:38' From Dual Union All
Select '15-JAN-22', 145, 'AIRB', 'SEA', '01:28', '1:05' From Dual
)
Select
A.FLIGHT_DATE, A.FLIGHT, A.AC, A.DEST, B.ATD "ATD_B", B.TDN "TDN_B", A.ATD "ATD_A", A.TDN "TDN_A"
From
A
Inner Join
B ON(
(A.FLIGHT_DATE = B.FLIGHT_DATE And A.FLIGHT = B.FLIGHT And A.AC = B.AC And A.DEST = B.DEST)
AND
(A.ATD <> B.ATD OR A.TDN <> B.TDN)
)
/* R e s u l t :
FLIGHT_DATE FLIGHT AC DEST ATD_B TDN_B ATD_A TDN_A
----------- ---------- ------ ---- ----- ----- ----- -----
15-JAN-22 152 BOEING MIA 02:39 1:38 02:45 1:38
15-JAN-22 145 AIRB SEA 01:28 1:05 02:45 1:05
*/
Regards...

SQL Server 2008 How do I sum rows that match in criteria

This is what my table looks like:
RefNum
Year
CorrespVNum
Proceeds
BaseCost
12345
2019
54321
12345.69
10000.00
12345
2019
54321
500.69
6000.00
12345
2019
65432
12345.69
10000.00
12345
2019
65432
500.69
6000.00
23456
2020
33344
50000.00
15000.00
34567
2021
11155
521.00
1000.00
34567
2021
11155
17.00
800.00
34567
2021
11155
85.00
100.00
I want the result to look like this:
RefNum
Year
CorrespVNum
TotalProceeds
TotalBaseCost
12345
2019
54321
12846.38
16000.00
12345
2019
65432
12846.38
16000.00
23456
2020
33344
50000.00
15000.00
34567
2021
11155
623.00
1900.00
So the matching criteria are the Refnum, Year and CorrespVnum. I have been messing around with a CTE query and I can't seem to get it to work. It works if I only have 2 matching rows but if there are more than 2 then it doesn't work.
Does anyone have any idea how to do this?
select RefNum
,Year
,CorrespVNum
,sum(Proceeds) as TotalProceeds
,sum(BaseCost) as TotalBaseCost
from t
group by RefNum, Year, CorrespVNum
RefNum
Year
CorrespVNum
TotalProceeds
TotalBaseCost
12345
2019
54321
12845
16000
12345
2019
65432
12845
16000
23456
2020
33344
50000
15000
34567
2021
11155
623
1900
Fiddle
you can try this, keep it simple:
with table_1 (Refnum,Year,CorrespVNum,Proceeds,BaseCost)
as
(
Select '12345', '2019', '54321',12345.69,10000
Union all Select '12345', '2019', '54321',500.69,6000
Union all Select '12345', '2019', '65432',12345.69,10000
Union all Select '12345', '2019', '65432',500.69,6000
Union all Select '34567', '2021', '33344',500000,15000
Union all Select '34567', '2021', '11155',521,1000
Union all Select '34567', '2021', '11155',17,800
Union all Select '34567', '2021', '11155',85,100
)
Select
RefNum,
Year,
CorrespVNum,
Sum(BaseCost) as TotalCaseCost
from
table_1
group by
RefNum,
Year,
CorrespVNum

Round to .5 or 1.0 in Oracle SQL

I'm looking to round values like
2.39 -> 2.40
4.66 -> 4.70
2.11 -> 2.15
2.10 -> 2.10
2.50 -> 2.50
5.89 -> 5.90
How can I manage this in SQL?
Thanks
problem solved :
SELECT FLOOR ( (1.11 + 0.04) * 20) / 20 FROM DUAL;
test:
WITH t (my_number)
AS (SELECT 3.1001 FROM DUAL
UNION ALL
SELECT 2.39 FROM DUAL
UNION ALL
SELECT 4.66 FROM DUAL
UNION ALL
SELECT 2.11 FROM DUAL
UNION ALL
SELECT 2.10 FROM DUAL
UNION ALL
SELECT 2.50 FROM DUAL
UNION ALL
SELECT 5.89 FROM DUAL)
SELECT my_number, FLOOR ( (my_number + 0.04) * 20) / 20 round_on_number
FROM t;
3.1001 3.1
2.39 2.4
4.66 4.7
2.11 2.15
2.1 2.1
2.5 2.5
5.89 5.9

Oracle: Need number values converted to text AND preserved

If a decimal value is 0.40 I'd like to TO_CHAR(0.40) and get '0.40' back. Likewise, 50 should be to_char'd to '50'. As simple as what goes in, comes out, but as a CHAR, in all cases. So far I've tried the following:
select to_char(a, '99D90') test1,
to_char(a, '90D90') test2,
to_char(a, 'FM90D99') test3,
rtrim(to_char(a, 'FM90D99'), to_char(0, 'D')) test4
from (
select 50 a from dual
union all select 50.57 from dual
union all select 5.57 from dual
union all select 0.35 from dual
union all select 0.4 from dual
union all select 0.80 from dual
union all select .88 from dual
)
order by a;
returns:
TEST1 TEST2 TEST3 TEST4
------ ------ ------ ------
.35 0.35 0.35 0.35
.40 0.40 0.4 0.4
.80 0.80 0.8 0.8
.88 0.88 0.88 0.88
5.57 5.57 5.57 5.57
50.00 50.00 50. 50
50.57 50.57 50.57 50.57
As you can see, what may work in one case doesn't in another. Do I have to test for whole numbers, decimal numbers, then leading and trailing zeros and apply a different formatter for each? Is there just a simple cast-like construct? (tried cast too btw, similar issues).
Solved
Oracle does not store or display formatting (even on non-persisted values as shown below). A Formatter must be applied for anything other than that.
0.4, .4 and 0.4000 are all the same. It is just the way you want to display the number.
As I understand, you want to preserve the rows as string, and not just display them. Then you could handle the conditions using a CASE expression.
TRUNC(NUMBER) truncates the decimal portion and gives you a whole number. This will be the driving condition.
So, when TRUNC(NUMBER) = 0, it means it has only decimal part, and we need to append a zero in the beginning, else just convert it into text.
For example,
SQL> WITH DATA AS(
2 select 50 a from dual
3 union all select 50.57 from dual
4 union all select 5.57 from dual
5 union all select 0.35 from dual
6 union all select 0.4 from dual
7 UNION ALL SELECT 0.80 FROM dual
8 UNION ALL SELECT .88 FROM dual
9 )
10 SELECT
11 CASE
12 WHEN TRUNC(A) = 0
13 THEN ltrim(to_char(a, '0D99'), ' ')
14 ELSE TO_CHAR(a)
15 END text
16 FROM DATA;
TEXT
----------------------------------------
50
50.57
5.57
0.35
0.40
0.80
0.88
7 rows selected.
SQL>