I want to turn this:
Query:
select A.DispatchNote, B.MStockCode, B.NComment
from MdnMaster A
left join MdnDetail B on A.DispatchNote = B.DispatchNote
A.DispatchNote
B.MStockCode
B.NComment
258579/0001
RFK2-8520-150-25
258579/0001
FREIGHT
258579/0001
1 Parcel UPS Ground 2/28/2020
258579/0001
Trk#: 1Z8R9V900342021397 -- 0.9 lb
258585/0001
CAW2-1832-25L
258585/0001
FREIGHT
258585/0001
6 Parcels UPS Ground 2/28/2020
258585/0001
Trk#: 1Z8R9V900342126962 -- 15 lb
258585/0001
Trk#: 1Z8R9V900342515176 -- 15 lb
258585/0001
Trk#: 1Z8R9V900340703781 -- 15 lb
258585/0001
Trk#: 1Z8R9V900340988792 -- 15 lb
258585/0001
Trk#: 1Z8R9V900340706204 -- 15 lb
258585/0001
Trk#: 1Z8R9V900342832014 -- 15 lb
into this:
Query:
select
A.DispatchNote,
MAX(B.MStockCode) as StockCode,
MAX(B.NComment) as Comment
from MdnMaster A
left join MdnDetail B on A.DispatchNote = B.DispatchNote
and (B.NComment LIKE 'Trk%' OR B.MStockCode is not null)
group by A.DispatchNote
A.DispatchNote
B.MStockCode
B.NComment
258579/0001
RFK2-8520-150-25
Trk#: 1Z8R9V900342021397 -- 0.9 lb
258585/0001
CAW2-1832-25L
Trk#: 1Z8R9V900342126962 -- 15 lb
But I'm not sure how. When I run my query I still get rowsthat have random info in the NComment Column (like Blank NComments or things other than Trk#). I think I'm closer, but I'm still missing something.
Basically I want to clear the empty rows in MStockCode (or condense the rows I guess) and only pull the first visible tracking number that shows up for each dispatchnote when I run the query unsorted).
Related
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 - Calculate sum of md_delta for previous 24 hrs for every record.
Here is an example of what I am shooting for
Basically, I am trying to create a query that returns the base table unaltered, such that for each row in left table it returns the sum of MD_DELTA over the previous 24 hrs for each unique ID.
I'm thinking I would likely need to use a correlated subquery but its does not seem to be working correctly.
Here is an code example:
SELECT
PG."INFO_ID" AS ID
,PG."RANK"
,PG."TIMEPOINT"
,PG."MD_DELTA"
,G."TOTAL_24HR_DELTA"
FROM CTE_PGROUPS AS PG
LEFT JOIN(
/*
CORRELATED SUB-QUERY TO GET THE SUM OF THE DEPTH_PROGRESS FROM THEPREVIOUS
24-HOUR TIME PERIOD FOR EACH
*/
SELECT
G."TIMEPOINT"
,SUM(G."MD_DELTA") AS TOTAL_24HR_DELTA
FROM CTE_PGROUPS AS G
WHERE G."TIMEPOINT" >= DATEADD('DAY',-1, G."TIMEPOINT")
GROUP BY G."TIMEPOINT"
) AS G ON PG."TIMEPOINT" = G."TIMEPOINT"
Using this CTE for data:
WITH data AS (
SELECT
*
FROM VALUES
(306, '2022-03-21 01:00:00'::timestamp, 0.5),
(306, '2022-03-21 08:00:00'::timestamp, 0.5),
(306, '2022-03-21 16:00:00'::timestamp, 0.5),
(306, '2022-03-21 21:00:00'::timestamp, 0.5),
(306, '2022-03-22 02:00:00'::timestamp, 0.5),
(306, '2022-03-22 06:00:00'::timestamp, 0.5),
(306, '2022-03-22 12:00:00'::timestamp, 0.5),
(306, '2022-03-22 18:00:00'::timestamp, 0.5),
(306, '2022-03-22 22:00:00'::timestamp, 0.5)
v(id, timepoint, depth_progress)
)
this SQL gives:
SELECT d.id,
d.timepoint,
sum(d2.depth_progress)
FROM data AS d
JOIN data AS d2
ON d.id = d2.id
AND d2.timepoint between dateadd(day,-1,d.timepoint) and d.timepoint
GROUP BY 1,2
ORDER BY 1,2;
gives:
ID
TIMEPOINT
SUM(D2.DEPTH_PROGRESS)
306
2022-03-21 01:00:00.000
0.5
306
2022-03-21 08:00:00.000
1
306
2022-03-21 16:00:00.000
1.5
306
2022-03-21 21:00:00.000
2
306
2022-03-22 02:00:00.000
2
306
2022-03-22 06:00:00.000
2.5
306
2022-03-22 12:00:00.000
2.5
306
2022-03-22 18:00:00.000
2.5
306
2022-03-22 22:00:00.000
2.5
and if your table as "missive" I would pre-condition that data like:
WITH massive_pre_condition as (
select *,
dateadd(day,-1,timepoint) as minus_one_day,
timepoint::date as d1,
minus_one_day::date as d2
FROM data
)
SELECT d.id,
d.timepoint,
sum(d2.depth_progress)
FROM massive_pre_condition AS d
JOIN massive_pre_condition AS d2
ON d.id = d2.id
AND d2.d1 IN (d.d1, d.d2)
AND d2.timepoint between d.minus_one_day and d.timepoint
GROUP BY 1,2
ORDER BY 1,2;
I have three tables and expecting the result as below but i do not know how to correct my sql statement.
select history.company,history.ghacct,rpt_revenue.revenue,rpt_revenue.other, isnull(guest.stay,0) as stay, isnull(guest.nights,0) as nights
from history
left join(select company,count(*) as stay,sum(nights) as nights from guest group by company) guest on guest.company=history.company
left join (select ghacct,sum(revenue) as revenue, sum(other) as other
from rpt_revenue group by ghacct) rpt_revenue on rpt_revenue.ghacct=history.ghacct
where history.type='c' group by history.company, history.ghacct,rpt_revenue.revenue, rpt_revenue.other,guest.stay,guest.nights order by history.company asc;
history
ghacct company type
33 JOINT LTD 10010205687 c
3B GLOBAL 10010350619 c
3E FASHION 10010244145 c
3P INT'L 10010112089 c
guest
company stay nights
33 JOINT LTD 01/01/2009 1
33 JOINT LTD 01/06/2009 1
3B GLOBAL 10/02/2019 2
3E FASHION 09/25/2008 6
3P INT'L 08/26/2009 3
3P INT'L 04/26/2010 9
rpt_revenue
ghacct revenue other
10010205687 20 10
10010205687 10 10
10010350619 30 2
10010244145 15 3
10010112089 16 8
10010112089 4 2
Result
company ghacct revenue other stay nights
33 JOINT LTD 10010205687 NULL NULL 2 2
3B GLOBAL 10010350619 NULL NULL 1 2
3E FASHION 10010244145 NULL NULL 1 6
3P INT'L 10010112089 NULL NULL 2 12
Expected result
company ghacct revenue other stay nights
33 JOINT LTD 10010205687 30 20 2 2
3B GLOBAL 10010350619 30 2 1 2
3E FASHION 10010244145 15 3 1 6
3P INT'L 10010112089 20 10 2 12
I think the main problem with your current query lies in the GROUP BY clause, which should really only be aggregating by company and account. In addition, you might want to use ISNULL for the revenue and other amount, as you are already doing so for stay and nights.
SELECT
h.company,
h.ghacct,
ISNULL(rr.revenue, 0) AS revenue,
ISNULL(rr.other, 0) AS other,
ISNULL(g.stay, 0) AS stay,
ISNULL(g.nights, 0) AS nights
FROM history h
LEFT JOIN
(
SELECT company, COUNT(*) AS stay, SUM(nights) AS nights
FROM guest
GROUP BY company
) g
ON g.company = h.company
LEFT JOIN
(
SELECT ghacct, SUM(revenue) AS revenue, SUM(other) AS other
FROM rpt_revenue
GROUP BY ghacct
) rr
ON rr.ghacct = h.ghacct
WHERE
h.type = 'c'
GROUP BY
h.company,
h.ghacct
ORDER BY
h.company;
I have the following table output-
What I have:
Account No Description Seg1 Seg2 Seg3 Budget PeriodBalance
000-1120-00 Cash 000 1120 00 $1,000.00 $2,000.00
000-1130-00 Asset 000 1130 00 $1,500.00 $3,000.00
What I would like to have:
Account No Description Seg1 Seg2 Seg3 Budget PeriodBalance
000-1120-01 Cash 000 1120 01 $700.00 $1,300.00
000-1120-02 Cash 000 1120 02 $900.00 $1,700.00
000-1130-00 Asset 000 1130 00 $1,500.00 $3,000.00
Here, if the Seg2 is equal to 1120 then I would like to split it into 2 accounts suffixing the account No with 01 and 02 in place of 00 in segment 3. Once I have that, I would like to obtain the budget and the periodbalance for the new accounts. My original query is the following (Dynamics GP database Fabrikam):
Select
rtrim(c.Actnumbr_1) +'-'+ rtrim(c.Actnumbr_2)+ '-'+ rtrim(c.Actnumbr_3) as ACTNUMST,
c.ACTINDX,
c.ACTDESCR,
c.ACTNUMBR_1,
c.ACTNUMBR_2,
c.ACTNUMBR_3,
sum(a.PERDBLNC) as Period_Balance,
b.BUDGETAMT,
a.year1,
'' as docdate,
''as Vendname,'' as PONUMBER,
0 as PO_Amount,
0 as 'Not_Received_Amount',
0 as ENCMBAMT
from GL00100 c
left outer join GL11110 a on c.ACTINDX = a.ACTINDX
Left outer join
(Select
actindx,
sum(budgetamt) as budgetamt
from GL00201
where BUDGETID = 'budget2017'
and periodid <= 12 group by actindx) b
on c.ACTINDX = b.ACTINDX
where
a.PERIODID <= 12 and
a.year1 = '2017' and a.actindx < '18'
group by
c.ACTDESCR, c.ACTNUMBR_1, c.ACTNUMBR_2, c.ACTNUMBR_3, a.year1,c.ACTINDX,b.BUDGETAMT
Any help would be highly appreciated.
Question
I want to display a combination of DATA from different tables. In this case all combinations of food and drink, which have not been ordered so far.
My database tables
Customer_order
Orderno(PK) DateOrd DateReq Address
O00001 03-Apr-11 07-Apr-11 Union St
O00002 05-Apr-11 01-May-11 St. Andrew St.
O00003 12-Apr-11 27-Apr-11 Garthdee
O00004 12-Apr-11 17-Apr-11 Union St.
Dish
Dishid(PK) DishName Vegetarian Price
D0001 Pasta bake yes 6.00
D0002 Fish pie No 9.00
D0003 Steak and chips No 14.00
D0004 Stuffed peppers yes 11.50
D0005 Ham and rice No 7.25
D0006 Lamb curry No 8.50
Drink
Drinkid(PK) DrinkName DrinkType Price
DR0001 Water soft 1.00
DR0002 Coffee hot 1.70
DR0003 Wine alcoholic 3.00
DR0004 Beer alcoholic 2.30
DR0005 tea hot 1.50
Food_order
Food_orderno(PK) Orderno(FK) Dishid(FK) NPortions
FO00001 O00001 D0003 6
FO00002 O00001 D0001 4
FO00003 O00001 D0004 3
FO00004 O00002 D0001 10
FO00005 O00002 D0002 10
FO00006 O00003 D0002 35
FO00007 O00004 D0002 23
Drink_order
Drinkorderid(PK) Orderid(FK) Drinkid(FK) N_units
DO00001 O00001 DR0001 13
DO00002 O00001 DR0002 13
DO00003 O00001 DR0004 13
DO00004 O00002 DR0001 20
DO00005 O00002 DR0003 20
DO00006 O00002 DR0004 15
DO00007 O00003 DR0002 35
DO00008 O00004 DR0001 23
DO00009 O00004 DR0003 15
DO00010 O00004 DR0004 15
http://imgur.com/KziEoLo
Queries tried so far
select distinct d.dish_name, dr.drink_name
from Dish d, Drink dr, Food_order fo, Drink_order dro, Customer_order co
where co.orderno = fo.orderno
AND co.orderno = dro.orderno
order by dish_name, drink_name;
select d.dish_name, dr.drink_name
from Dish d, Drink dr, Food_order fo, Drink_order dro, Customer_order co
where co.orderno = fo.orderno
AND co.orderno = dro.orderno
group by d.dish_name, dr.drink_name;
select distinct d.dish_name, dr.drink_name
from Dish d, Drink dr, Food_order fo, Drink_order dro, Customer_order co
where d.dishid = fo.DISHID
AND dr.DRINKID = dro.DRINKID
AND fo.ORDERNO = dro.ORDERNO
group by d.dish_name, dr.drink_name;
select distinct d.dish_name, dr.drink_name
from dish d, drink dr, food_order fo, drink_order do
where fo.ORDERNO = do.ORDERNO
group by d.dish_name, dr.drink_name;
That seems to return, what you are asking for:
SELECT
Dish.DishName,
Dish.Dishid,
Drink.DrinkName,
Drink.Drinkid
FROM Dish
JOIN
(SELECT
Dishid
, Drinkid
FROM Dish
CROSS JOIN Drink
MINUS
(SELECT
Dishid
, Drinkid
FROM Food_order
CROSS JOIN Drink_order
WHERE Food_order.Orderno = Drink_order.Orderid
)
) NotOrderedTogether
ON Dish.Dishid = NotOrderedTogether.Dishid
JOIN Drink
ON NotOrderedTogether.Drinkid = Drink.Drinkid
ORDER BY Dishname, Drinkname
;
See it in action: SQL Fiddle.
As pointed out in the comments, the result set is going to explode, if you add large(r) numbers of food and beverages. In that case, it would probably make sense to apply the MINUS first to dishes and drinks respectively, and to apply the CROSS JOIN to the least possible number of records.
Please comment, if and as this requires adjustment / further detail.