Selecting rows based on Priority -Sql - sql

I have a case there I want to select row based Status cloumn Priorty
First Priorty should be Customer_Status with Status 'Deleted'
Second priority should be Family_Status with value 'Deleted'
[ Select top 1 with Family_Status ='Deleted']
If all Family_Status are "open' then select top 1 Family_Status ='Open'
Case : 1
CustomerID FamilyId Name Customer_Status Family_Status
------- -------- --------- --------------- -----------
1000 101 Vk Open Deleted
1000 102 vk Open Open
1000 103 vk Open Open
In this case i need result as
CustomerID FamilyId Name Customer_Status Family_Status
------- -------- --------- --------------- -----------
1000 101 Vk Open Open
Case2
CustomerID FamilyId Name Customer_Status Family_Status
------- --------- --------------- ------------
1000 101 Vk Open Open
1000 102 vk Deleted Open
1000 103 vk Open Open
In this case i need result as
CustomerID FamilyId Name Customer_Status Family_Status
------- --------- --------------- ------------
1000 102 vk Deleted Open
Case : 3
CustomerID FamilyId Name Customer_Status Family_Status
------- --------- --------------- ------------
1000 101 Vk Deleted Open
1000 102 vk Deleted Open
1000 103 vk Deleted Open
Output :
CustomerID FamilyId Name Customer_Status Family_Status
------- --------- --------------- ------------
1000 101 Vk Deleted Open
Case :4
CustomerID FamilyId Name Customer_Status Family_Status
------- --------- --------------- ------------
1000 101 Vk Open Deleted
1000 102 vk Open Deleted
1000 103 vk Open Deleted
In this case i need result as
CustomerID FamilyId Name Customer_Status Family_Status
------- --------- --------------- ---------
1000 101 Vk Open Deleted
Can anyone help on this query

Just order the result set:
SELECT TOP (1) *
FROM MyTable
ORDER BY Customer_Status, Family_Status DESC

Related

get average of subtotal and wet with respect to distinct purchaseorderid and product category in DAX

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]
)

delete multiple rows from different tables on oracle

I have a two table.One of them is student, the other one is salary.
Student table
id | name | code | status
1 | steven | 123 | 100
2 | joe | 678 | 200
3 | paul | 758 | 100
Salary table
id | code | status | currency
1 | 123 | 100 | euro
2 | 678 | 200 | dolar
3 | 758 | 520 | yuan
I want to delete row1 from Student table and row 1 and 2 from Salary table because code and status fields
are same.
I write that query
delete a,b Student as a , join Salary as b
on a.code= b.code and a.status = b.status
but it is not working.I want to delete rows with one query.Do you have any idea?
Would something like this do? PL/SQL, though, not SQL.
Initial data sets:
SQL> select * from student;
ID NAME CODE STATUS
---------- ------ ---------- ----------
1 steven 123 100
2 joe 678 200
3 paul 758 100
SQL> select * from salary;
ID CODE STATUS CURREN
---------- ---------- ---------- ------
1 123 100 euro
2 678 200 dollar
3 758 520 yuan
Remove common (CODE, STATUS) combinations:
SQL> begin
2 for cur_r in (select code, status from student
3 intersect
4 select code, status from salary
5 )
6 loop
7 delete from student where code = cur_r.code and status = cur_r.status;
8 delete from salary where code = cur_r.code and status = cur_r.status;
9 end loop;
10 end;
11 /
PL/SQL procedure successfully completed.
Result:
SQL> select * from student;
ID NAME CODE STATUS
---------- ------ ---------- ----------
3 paul 758 100
SQL> select * from salary;
ID CODE STATUS CURREN
---------- ---------- ---------- ------
3 758 520 yuan
SQL>
You can use two statements to do so easily:
delete student s where exists(
select * from student stu inner join salary sal on stu.code=sal.code and stu.status=sal.status and stu.id=s.id);
delete salary sal where not exists (select code from student stu where stu.code=sal.code);
First one to delete all the students having same code and status in both tables and second is to delete all the rows from salary table where code doesn't exist in student table.

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

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'

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 ;