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'
Related
i don't know where to start, pretty new in power bi or Dax
here is sample data
PurchaseOrderId SubTotal Wet ProductCategory
------------------ ----------- ----------- ------------------
1021 804.9767 233.4432 Wine
------------------ ----------- ----------- ------------------
1022 228.4651 66.2548 Beer
------------------ ----------- ----------- ------------------
1022 228.4651 66.2548 RTD
------------------ ----------- ----------- ------------------
1022 228.4651 66.2548 Wine
------------------ ----------- ----------- ------------------
1023 2791.2558 809.4641 Wine
------------------ ----------- ----------- ------------------
1023 2791.2558 809.4641 Beer
------------------ ----------- ----------- ------------------
1023 2791.2558 809.4641 Non-alcoholic
------------------ ----------- ----------- ------------------
1023 2791.2558 809.4641 RTD
------------------ ----------- ----------- ------------------
1024 396 114.84 Wine
------------------ ----------- ----------- ------------------
1025 374.2325 108.5274 Wine
------------------ ----------- ----------- ------------------
1026 864.093 250.5869 Wine
------------------ ----------- ----------- ------------------
1027 127.9069 37.093 Wine
what i want is i want average order value i.e
(Subtotal+Wet)/Count(Distinct PurchaseOrderId).
For this table Sum of distinct Subtotal is 5586.93 and Sum of
distinct wet is 1620.20,
total number of distinct purchaseorderid i.e number of orders is 7 so my average order value is (5586.93+1620.20)/7 = 1029.59,
i also want average order value by category
i am doing everything in DAX
so how this can be achieved?
Thanks in advance
You need to use this measure: I called my table (Test00) You can use yours in the same way.
AverageOfSubTotal =
VAR TblSummary = ADDCOLUMNS(
SUMMARIZE (Test00, Test00[PurchaseOrderId], Test00[ProductCategory]),
"Total_SubTotal",CALCULATE(SUM(Test00[SubTotal]))
)
RETURN
AVERAGEX(TblSummary,[Total_SubTotal])
If we test it on a table visual:
Now let's test the visual table report performance on DAX Studio:
The results are quite good!! 5 ms query time (4ms FE, 1 ms SE). 4 SE(Storage Engine) queries.
Use this as a new Measure
Avg Subtotal =
AVERAGEX(
SUMMARIZE(
'Table',
'Table'[PurchaseOrderId],
'Table'[ProductCategory],
'Table'[SubTotal]
),
'Table'[SubTotal]
)
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
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 ;
I have a table consisting of customer purchase records having following columns
CUSTOMER TRANSDATE SALENUM STORE TRANSTYP PRICE
-------- --------- --------- ----- --------- ------
1 12-FEB-2013 777 O CASH 7.99
1 12-FEB-2013 777 O CASH 6.99
1 12-FEB-2013 778 O CARD 9.17
2 23-APR-2013 987 D CASH 7.65
1 15-MAY-2013 1098 T CARD 2.35
I want to aggregate over salenum i,e for each salenum, i want the total price, as well as the store, transtyp, transdate,customer values as these are same for a particular salenum
However if i use
select customer,transdate,salenum,store,transtyp,sum(price) from table1 group by salenum
Its obviously saying not a valid group by value. How to get the desired result?
SAMPLE RESULT:
CUSTOMER TRANSDATE SALENUM STORE TRANSTYP PRICE
-------- --------- --------- ----- --------- ------
1 12-FEB-2013 777 O CASH 15.98
1 12-FEB-2013 778 O CARD 9.17
All non-aggregated columns should be in the group by:
select customer,transdate,salenum,store,transtyp,sum(price)
from table1
group by customer,transdate,salenum,store,transtyp
SELECT CUSTOMER,TRANSDATE,SALENUM,STORE,TRANSTYP,SUM(PRICE)
FROM TABLE1
GROUP BY CUSTOMER,TRANSDATE,SALENUM,STORE,TRANSTYP
ORDER BY CUSTOMER
I using the order by clause because it will be in order and also in sequence.
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