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

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

Related

How to report for all rows when joining?

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

Elasticsearch Join columns from different index with condition

I have 2 different indexes in elasticsearch, indx1 and indx2 which i have indexed from an SQL Database using river plugin.
indx1
----------
id | Amt
1 2
2 3
3 2
1 9
2 4
----------
indx 2
----------
id | Name
1 Alex
2 Joe
3 MARY
----------
I want to create a new index now which calculate the average amount from indx1 and join everything in a single index.
So the final structure of the index should look like
indx_final
----------
id | Name | Avg Amt | Status
1 Alex 5.5 High
2 Joe 3.5 Med
3 Mary 2.0 Low
----------
The status is set according to average amount , if Avg amt > 4 , status = high, if avg amt >3, status = Med, If avg amt <2.5 ,status = Low.
Is this possible to do in elasticsearch only? If not possible i would have to do the calculation in SQL and then index the data again.
Any help would be appreciated. Thanks!

SQL statement, result from different row

I have a sales table that look like this
receipt saletype qtysold sellprice discount
31103 ----- I --------- 1 ------- 39 ------- 0
31103 ---- W -------- 1 -------- 0 ------- 14
31103 ----- I --------- 1 ------- 39 ------- 0
31103 ---- W -------- 1 -------- 0 ------- 14
The actual qtysold is 2 pcs from this receipt 31103.
I am trying to get the result from qtysold where salestype='I'
My result will be incorrect when I do this
Select qtysold, sellprice, discount
from salestable
where receipt = '31103'
You can easily return all rows with salestype = 'I' by including this filter in your where clause. To include multiple filters in a single where clause, you would need the 'and' keyword. The code would look something like this-
Select qtysold, sellprice, discount
from salestable
where receipt = '31103'
and salestype = 'I'

Calculate Discount amount Based on Quantity

I have a table
SELECT PRD_CODE,PRD_MAKE,PRD_NAME FROM PRD_MST
SELECT PDI_DISC_QTY,PDI_DISC_PRICE FROM PRD_DISC_INF
select customerid,Productid,quantity from ShoppingCartItem
Records
PRD_MST
PRD_CODE PRD_MAKE PRD_NAME
----------- ---------------- ----------
4 mobile moto e
5 cycle hero
PRD_DISC_INF
PDI_DISC_QTY PDI_DISC_PRICE
----------------- ------------------
2.00 2.10
1.00 2.31
ShoppingCartItem
customerid Productid quantity
----------- ----------- -----------
1 5 5
The Problem is that
if a customer buy 5 mobile and price of single unit is 200 then how do i calculate discount amount base on PDI_DISC_QTY in table 'PRD_DISC_INF'
Need help i am unable to get the logic
Try this DEMO
select PDI_DISC_QTY *
(cast(round(((select quantity from ShoppingCartItem)/PDI_DISC_QTY),0) as int))
from PRD_DISC_INF ;

Creating a view with sums from multiple tables (Oracle SQL)

I'm trying to create a view that gets the sums of a couple of different rows in various tables. (I'm not sure how to explain this properly)
Here is how my tables are set out:
Visitors:
VISITORID FNAME LNAME PHONE HOTELID
---------- --------------- --------------- --------------- ----------
23 Bella Morgan 0394110625 3
Bookings:
BOOKINGID HOTELID ROOMNO BOOKINGDATE BOOKINGDAYS BEDANDBREA VISITORID
---------- ---------- ---------- ------------------- ----------- ---------- ----------
28 3 509 28-04-2013 00:00:00 3 Yes 23
Rooms:
ROOMNO HOTELID ROOMTYPE PRICE
---------- ---------- ------------------------- ----------
509 3 Double 700
Services:
SERVICEID SERVICENAME COST HOTELID
---------- -------------------------------------------------- ---------- ----------
1-CLTH Cloth Cleaning 14.95 1
2-RMSV Room Service 9.95 2
Booking_services:
SERVICEID BOOKINGID
---------- ----------
2-RMSV 32
1-CLTH 32
I want to create a view called bills that gives me the total of room cost and cost of all services.
To get the room price, the sum is rooms.price*bookings.bookingdays.
For the services, it's the sum of all the rows in the services table that match the SERVICEID in booking_services for the matching bookingID.
Currently there are more rows in all of the tables than I've shown (so it doesn't take up too much space on here) and I have a query but it's only showing 2 of the visitors that i'd like the total for. I know it's because of line 5, but I'm not sure how I can get it to calculate that as well as those who do not have a row in booking_services.
Here is that query:
CREATE VIEW bills AS
SELECT v.fname, SUM((r.price*b.bookingdays)+s.cost) AS total
FROM visitors v, rooms r, bookings b, services s, booking_services bs
WHERE v.visitorid = b.visitorid
AND
s.serviceid in(select bs.serviceid from booking_services where bs.bookingid = b.bookingid)
AND
b.roomno = r.roomno
GROUP BY v.fname;
Any help to get what I'm after (if this makes any sense) would be appreciated.
Here is the SQLFiddel Demo
You can try below query for your view:
Select v.fname, sum((r.price*b.bookingdays)+nvl(bso.cost,0))
From visitors v
Join bookings b
on v.visitorid = b.visitorid
Join rooms r
on b.roomno = r.roomno
left outer join (select bs.BOOKINGID,sum(cost) as cost
from booking_services bs
Join services s
on s.SERVICEID = bs.SERVICEID
group by bs.BOOKINGID) bso
on bso.BOOKINGID = b.BOOKINGID
GROUP BY v.fname;