How to report for all rows when joining? - sql

I'd like to report for all batch_runs that meet where batch_run > 200833 and batch_id=100
If a BATCH_RUN does not have any batch_id = 100, then report 0.
select batch_id,
batch_run,
count(*) over (partition by batch_id,
batch_run
order by batch_run) as total_lot_count,
sum(lot_size) over (partition by batch_id,
batch_run) as total_lot_size,
row_number() over (partition by batch_id
order by batch_run) as line_number
from batch_jobs
-- inner join batches on batch_jobs.batch_run = batches.batch_run
-- left join batches on batch_jobs.batch_run = batches.batch_run
where batch_run > 200833
and batch_id = 100
See Fidde
BATCHES
--------------- ----------
BatchSequence Batch_run
--------------- ----------
1 200833
2 200911
3 200922
4 200933
5 201011
6 201022
7 201033
BATCH_RUNS
------------- ---------- ---------
Batch_id Batch_run Lot_size
------------- ---------- ---------
100 200933 10
100 200933 20
100 200933 30
100 201022 400
100 201022 500
Desired result:
--------------- --------- ---------- ----- ---- -------
Batch_Run Batch_id Lot_count Total_Lots Line_No
--------------- --------- ---------- ----- ---- -------
200911 0 1
200922 100 3 60 2
200933 0 3
201011 0 4
201022 100 2 900 5
201033 0 6

It's too bad that your post has inconsistencies with your SQL Fiddle. It makes it all a bit confusing. But I think that this is what you were looking for. And as you'll see, apart from row_number, analytic functions are not really needed.
select b.batch_run,
bj.batch_id,
count(bj.batch_run) as total_lot_count,
coalesce(sum(bj.lot_size), 0) as total_lot_size,
row_number() over (order by b.batch_sequence) as Line_No
from batches b
left join batch_jobs bj
on bj.batch_run = b.batch_run
and bj.batch_id = 100
where b.batch_run > 200833
group by b.batch_sequence, b.batch_run, bj.batch_id
order by Line_No
SQLFiddle Demo

Related

analytical functions

good afternoon, a question, how can I optimize the code, I don't know, maybe using oracle analytical functions :
-- tabledeuda : this table contains 2 months 202212 and 202211
SELECT B.*,
NVL(B.DEUDAPRESTAMO_PAGPER,0)-NVL(A.DEUDAPRESTAMO_PAGPER,0) AS SALE_CT -- current month - previous month
FROM tabledeuda B
LEFT JOIN tabledeuda A ON (A.CODLLAVE = B.CODLLAVE
AND A.CODMES = TO_NUMBER(TO_CHAR(ADD_MONTHS(TO_DATE(B.CODMES,'YYYYMM'),-1),'YYYYMM'))
AND A.financial_company = B.financial_company
AND A.CODMONEY=B.CODMONEY)
WHERE NVL(B.DEUDAPRESTAMO_PAGPER,0)>NVL(A.DEUDAPRESTAMO_PAGPER,0)
AND B.CODMES = &CODMES; ---> &CODMES 202212
OUTPUT
Looks like a candidate for lag analytic function.
Sample data is rather poor so it is unclear what happens when there's more data, but - that's the general idea.
Sample data:
SQL> with test (codmes, customer, deudaprestamo_pagper) as
2 (select 202212, 'T1009', 200 from dual union all
3 select 202211, 'T1009', 150 from dual
4 )
Query:
5 select codmes, customer,
6 deudaprestamo_pagper,
7 deudaprestamo_pagper -
8 lag(deudaprestamo_pagper) over (partition by customer order by codmes) sale_ct
9 from test;
CODMES CUSTO DEUDAPRESTAMO_PAGPER SALE_CT
---------- ----- -------------------- ----------
202211 T1009 150
202212 T1009 200 50
SQL>
If you want to fetch only the last row (sorted by codmes), you could e.g.
6 with temp as
7 (select codmes, customer,
8 deudaprestamo_pagper,
9 deudaprestamo_pagper -
10 lag(deudaprestamo_pagper) over (partition by customer order by codmes) sale_ct,
11 --
12 row_number() over (partition by customer order by codmes desc) rn
13 from test
14 )
15 select codmes, customer, deudaprestamo_pagper, sale_ct
16 from temp
17 where rn = 1;
CODMES CUSTO DEUDAPRESTAMO_PAGPER SALE_CT
---------- ----- -------------------- ----------
202212 T1009 200 50
SQL>

Running recursive query in Vertica

I am trying to do the exact same thing as this question. But I am in Vertica, and I am finding no way to carry out the top answer, or the other answers. So basically I have tried connect by and sub query UNION ALL method, and I don't think Vertica supports it.
Is there any way I can replicate the solution in Vertica?
EDIT: Full Question
I am trying to calculate 30-day readmission chains, which is a sequence of readmissions within 30 days from its previous admission. The following data shows a simplified situation where we have events, rather than admission and discharges. Difference in days between events will identify it as a 30-day readmission, consecutive 30 day readmissions(Chain Len) will be a single chain of readmission (Count).
Sample Data
CREATE TABLE dbo.Events (
EventID INT IDENTITY(1,1) PRIMARY KEY,
EventDate DATE NOT NULL,
PersonID INT NOT NULL
);
GO
INSERT dbo.Events (EventDate, PersonID)
VALUES
('2014-01-01', 1), ('2014-01-05', 1), ('2014-02-02', 1), ('2014-03-30', 1), ('2014-04-04', 1),
('2014-01-11', 2), ('2014-02-02', 2),
('2014-01-03', 3), ('2014-03-03', 3);
GO
Sample Output
EventID EventDate PersonID CHAIN LEN Count
------- ---------- -------- --------- -----
1 2014-01-01 1 1 1
2 2014-01-05 1 2 1
3 2014-02-02 1 3 1
------- ---------- -------- --------- -----
4 2014-03-30 1 1 2
5 2014-04-04 1 2 2
------- ---------- -------- --------- -----
6 2014-01-11 2 1 1
7 2014-02-02 2 2 1
------- ---------- -------- --------- -----
8 2014-01-03 3 1 1
------- ---------- -------- --------- -----
9 2014-03-03 3 1 2
------- ---------- -------- --------- -----
Here is an Oracle solution; see if it works. You may need to make some changes for vertica, as each db dialect has its own quirks. Vertica does support analytic functions, which is the main ingredient.
The method used here is a very well known one, it is usually called "start-of-groups" method (for the "flags" created in the innermost subquery).
select eventid, eventdate, personid,
row_number() over
(partition by personid, ct order by eventdate) as chain_len,
ct
from (
select eventid, eventdate, personid,
count(flag) over
(partition by personid order by eventdate) + 1 as ct
from (
select eventid, eventdate, personid,
case when eventdate > lag(eventdate) over
(partition by personid order by eventdate) + 30
then 0 end as flag
from events
)
)
order by personid, eventdate -- if needed
;

How to join multiple rows by continue from and to id columns in oracle

I have a scenario where I need to find the start date and end date from multiple rows which are tied by continued_from and continued_to date fields in Oracle.
result should look like
ID STARTDATE ENDDATE
-- ---------- ----------
3 01/01/1000 12/31/9999
ID STARTDATE ENDDATE CONT_FROM_ID CONT_TO_ID
-- ---------- ---------- ------------ -----------
1 01/01/1000 10/10/1999 NULL 2
2 10/10/1999 11/11/2000 1 3
3 11/11/2000 12/31/9999 2 NULL
Oracle's hierarchical query syntax makes it easy to walk the tree from parent to child. The analytical lead() and lag() functions track the next and previous IDs.
select c23.id
, c23.startdate
, c23.enddate
, lag(c23.id) over (partition by p23.id order by c23.id) as cont_from_id
, lead(c23.id) over (partition by p23.id order by c23.id) as cont_to_id
from p23
join c23 on p23.startdate <= c23.startdate
and p23.enddate >= c23.enddate
order by c23.id
/
Here is a test using your sample data:
SQL> select c23.id
2 , c23.startdate
3 , c23.enddate
4 , lag(c23.id) over (partition by p23.id order by c23.id) as cont_from_id
5 , lead(c23.id) over (partition by p23.id order by c23.id) as cont_to_id
6 from p23
7 join c23 on p23.startdate <= c23.startdate
8 and p23.enddate >= c23.enddate
9 order by c23.id
10 /
ID STARTDATE ENDDATE CONT_FROM_ID CONT_TO_ID
---------- --------- --------- ------------ ----------
1 01-JAN-00 10-OCT-99 2
2 10-OCT-99 11-NOV-00 1 3
3 11-NOV-00 31-DEC-99 2
SQL>

get multiple column from both table by joining a table on the basis of other max table column

1) vendor table
--------------------------------------------
VENDid VENDname
--- -----
1 ABC
2 XYZ
3 WXY
2)purchase table
---------------------------------------------
VENDid Purchasedate
------ ------------
1 12-01-2012
1 10-11-2013
2 22-02-2014
2 11-04-2014
3 10-05-2014
3 11-06-2014
1 14-06-2014
output(list all rows of vendor table and only max(purchasedate) from purchase table)
---------------------------------------------
VENDid VENDname PurchaseDate
------- -------- -------------
1 ABC 14-06-2014
2 XYZ 11-04-2014
3 WXY 11-06-2014
i got some queries like to solve previous problem-
SELECT v.VendID, VendName, Max(PurchaseDate)
FROM vendor v
INNER JOIN purchase p
ON v.VendID = p.VendID
Group By v.VendID, VendName
select VENDid, VENDname,
(select top 1 purchaseDate from purchase p
where p.VENDid=v.VENDid order by purchaseDate desc) as 'Purchase date'
from Vendor v
Que. If i will add some more column in purchase table like -
2)purchase table
------------------------------------------
VENDid Purchasedate amount_paid
------ ------------ ------------
1 12-01-2012 10000
1 10-11-2013 20000
2 22-02-2014 15000
2 11-04-2014 30000
3 10-05-2014 80000
3 11-06-2014 17000
1 14-06-2014 28000
and i want amount_paid along with previous output like-
---------------------------------------------
VENDid VENDname PurchaseDate amount_paid
------- -------- ------------- -------------
1 ABC 14-06-2014 28000
2 XYZ 11-04-2014 30000
3 WXY 11-06-2014 17000
then what will be query..
You appear to be using SQL Server. If so, you can use cross apply:
select v.VENDid, v.VENDname, p.PurchaseDate, p.Amount_Paid
from Vendor v cross apply
(select top 1 p.*
from purchase p
where p.VENDid = v.VENDid
order by p.purchaseDate desc
) p ;

sum of cost spent on advertisments on specific magazine (month?, year?)

Here are the tables //thanks for fixing my format//
ADV_COST
--------
PAGE_SIZE
MAG_ID
COST
//SAMPLE DATA ADV_COST//
PAGE_SIZE MAG_ID COST
-------------------- ---------- ----------
1/25 PAGE 1 40
1/8 PAGE 1 60
1/6 PAGE 1 65
...
ADS
--------
AD_ID
ADV_ID
PAGE_SIZE
MAG_ID
START_DATE
PURCH_DATE
NUM_ISSUES
//SAMPLE DATA ADS//
AD_ID ADV_ID PAGE_SIZE MAG_ID START_DAT PURCH_DAT NUM_ISSUES
---------- ---------- ---------- ---------- --------- --------- ----------
1 5 1/4 PAGE 1 01-APR-11 01-MAR-11 4
...
Here's the question:
Whirlpool ADV_ID=6; HOUSES: MAG_ID=1;
"How much money did Whirlpool spend in advertising in HOUSES this month?, this year?"
help please, thanks!
This is what I tried.
SQL> SELECT SUM(COST)
2 FROM DVD_ADV_COST A, DVD_ADS B
3 WHERE A.MAG_ID = B.MAG_ID
4 AND B.ADV_ID = 6
5 AND B.MAG_ID = 1;
Try
SELECT SUM(B.COST * A.NUM_ISSUES) AS TOTAL_COST
FROM DVD_ADS A,
DVD_ADV_COST B
WHERE B.MAG_ID = A.MAG_ID
AND B.PAGE_SIZE = A.PAGE_SIZE
AND A.ADV_ID = 6
AND A.MAG_ID = 1
and then add your date range to the WHERE clause as well
If it's not returning any data, make sure that you actually have entries for ADV_ID = 6.... your example show an entry for 5