SQL for each group find column order by another column - sql

Here's the data:
studentid,state,score,date,modno,transactionno
1,IL,10,20170101,0,0
1,VA,20,20170102,1,1
1,FL,30,20170103,2,2
2,VA,40,20170101,0,0
2,IL,50,20170102,1,1
2,IN,60,20170103,2,2
Here's how the output should be:
studentid,state,score,date
1,IL,20,20170101
2,VA,60,20170102
So basically we want to group by studentid.
Find the first state based on the lowest modno and transactionno.
Find the first score based on the highest modno and transactionno.
select studentid,
(select top 1 state from student s1 where s.studentid = s1.studentid order by modno, transactionno) as state,
(select top 1 score from student s1 where s.studentid = s1.studentid order by modno desc, transactionno desc) as score,
(select top 1 date from student s1 where s.studentid = s1.studentid order by modno, transactionno) as date
from student s
group by studentid
Above is my query in SQL 2008. Are there other ways to write this query to get better performance? It really shows down when working with large data sets and with pulling out more than two columns per group.

I think I would approach this with conditional aggregation:
select studentid,
max(case when seqnum_asc = 1 then state end) as state,
max(case when seqnum_desc = 1 then score end) as score,
max(case when seqnum_asc = 1 then date end) as date
from (select s.*,
row_number() over (partition by studentid order by modno, transactionno) as seqnum_asc,
row_number() over (partition by studentid order by modno desc, transactionno desc) as seqnum_desc
from student s
) s
group by studentid;

Guess your output is not correct or we havn't unserdtood Date part in your output.
Try this other sample data and let us know,
declare #t table(studentid int,states varchar(20), score int
,dates date,modno int,transactionno int)
insert into #t VALUES
(1,'IL',10,'20170101',0,0)
,(1,'VA',20,'20170102',1,1)
,(1,'FL',30,'20170103',2,2)
,(2,'VA',40,'20170101',0,0)
,(2,'IL',50,'20170102',1,1)
,(2,'IN',60,'20170103',2,2)
;with CTE as
(
select studentid from #t
group by studentid
)
SELECT t.studentid,ca.states,ca1.score,ca.dates
from CTE t
cross apply(select top 1 t1.states,t1.dates from #t t1
where t.studentid=t1.studentid
order by modno,transactionno )ca
cross apply(select top 1 t1.score from #t t1
where t.studentid=t1.studentid
order by modno desc,transactionno desc )ca1
--cross apply(select top 1 t1.dates from #t t1
--where t.studentid=t1.studentid
--order by modno ,transactionno )ca2

Related

SQL select row with max value or distinct value and sum all

I have the following data that is returned to me. I need to get a distinct or max sum of all the commission by taxid for a single repnbr. The 'qtrlycommrep' column is the value I'm trying to get to, but not able to. For repnbr c590, I need to get the 854.66 commission amount, which is the max for each taxid.
What am I doing wrong?
Any help would be much appreciated!
Here's what I've tried so far. Using the Row_number
select distinct
sub.Repnbr
, (sub.QtrLYComm) as qtrlycommrep
from (
select distinct repnbr, QtrLYComm
, rn = row_number() over(partition by repnbr order by QtrLYComm desc)
from #qtrly
) sub
where sub.rn = 1
Cross Apply
select distinct
#qtrly.repnbr
, x.QtrLYComm as qtrlycommrep
from #qtrly
cross apply (
select top 1
*
from #qtrly as i
where i.repnbr = Repnbr
order by i.qtrlycomm desc
) as x;
inner join
select
#qtrly.repnbr, #qtrly.qtrlycomm as qtrlycommrep
from #qtrly
inner join (
select maxvalue = max(qtrlycomm), repnbr
from #qtrly
group by repnbr
) as m
on #qtrly.repnbr = m.repnbr
and #qtrly.qtrlycomm = m.maxvalue;
order by row_number
select top 1 with ties
#qtrly.repnbr, #qtrly.qtrlycomm as qtrlycommrep
from #qtrly
order by
row_number() over(partition by repnbr
order by qtrlycomm desc)
You want one value per tax id. You need to include that. For instance:
select q.Repnbr, sum(q.QtrLYComm) as qtrlycommrep
from (select q.*,
row_number() over(partition by repnbr, taxid order by QtrLYComm desc) as seqnum
from #qtrly q
) q
where seqnum = 1
group by q.Repnbr;
However, I would be inclined to use two levels of aggregation:
select q.Repnbr, sum(q.QtrLYComm) as qtrlycommrep
from (select distinct repnbr, taxid, QtrLYComm
from #qtrly q
) q
group by q.Repnbr;

Sum column but for each row

How to do like
and I want to have result like
and what I already try is like this
select no, student, sum(point) from student group by no, student
But the result is not like what I expected.
You appear to want a "running sum" e.g.
select
no
, student
, sum(point) over(partition by student order by no) run_sum
from student
and it seems that the extra point might a subtraction of the 2nd last running sum from the final like this.
WITH cte
AS (
SELECT
no
, student
, SUM(point) OVER (PARTITION BY student ORDER BY no) run_sum
, ROW_NUMBER() OVER (PARTITION BY student ORDER BY no DESC) rn
FROM student)
SELECT
t.*
, coalesce(t.rum_sum,0) - coalesce(ex.run_sum,0) AS extra_point
FROM cte t
LEFT JOIN (
SELECT
*
FROM cte
WHERE rn = 2) ex ON t.student = ex.student
AND t.rn = 1
AND ex.rn = 2
;
but without more guidance on the required logic for extra_point that is just a guess.

How to improve Using RANK() top 2 results into unique columns

Here is the SQL 9(i) code written to show results 1 and 2 in their own columns. Is there a more efficient way to write this?
select
sc1.COIL as COIL1
, sc1.DEFECT as DEFECT1
, sc2.DEFECT as DEFECT2
FROM
(select
COIL, DEFECT
, RANK() OVER(PARTITION BY COIL ORDER BY WEIGHT DESC) RNK
from NOVELIS.F406, NOVELIS.F408 where F406_DEFECT_CODE = F408_REJECT_CODE
GROUP BY COIL, DEFECT
)sc1
, (select
COIL, DEFECT
, RANK() OVER(PARTITION BY COIL ORDER BY WEIGHT DESC) RNK
from NOVELIS.F406, NOVELIS.F408 where F406_DEFECT_CODE = F408_REJECT_CODE
GROUP BY COIL, DEFECT
)sc2
WHERE
sc1.RNK = 1
and sc2.RNK = 2
and sc1.COIL = sc2.COIL
You can use conditional aggregation and use 1 derived table
SELECT sc1.COIL AS COIL1,
MAX(CASE WHEN RNK = 1 THEN sc1.DEFECT END) AS DEFECT1,
MAX(CASE WHEN RNK = 2 THEN sc1.DEFECT END) AS DEFECT2
FROM
(
SELECT COIL,
DEFECT,
RANK() OVER(PARTITION BY COIL ORDER BY WEIGHT DESC) RNK
FROM NOVELIS.F406
--practice using joins
INNER JOIN NOVELIS.F408 ON F406_DEFECT_CODE = F408_REJECT_CODE
--not sure you need the group by here
) sc1
GROUP BY sc1.COIL
To avoid reading the tables twice, just read them once and pivot out the results:-
select
coil,
max (case when rnk=1 then defect else null end) defect1,
max (case when rnk=2 then defect else null end) defect2
FROM (
select
COIL, DEFECT,
RANK() OVER(PARTITION BY COIL ORDER BY WEIGHT DESC) RNK
from NOVELIS.F406
inner join NOVELIS.F408 on F406_DEFECT_CODE = F408_REJECT_CODE
) sc
WHERE
sc.RNK <= 2
group by coil

Sql query with join and group by and

I have two table in sql. First one the patient list, second one is their report. all patient's reports are in the report, just with id we can join them. Each patient has some reports (Maybe all the fields of a record is not filled). Now I want to make a report that get the last report of each patient but if some field are empty in the last record of that patient I should fill it with last filled record of that patients records. I have date in the table of reports.
I want to do it for all patients. Here I will add a pic for one patient as an example
In the example above, I want just highlighted ones for this patient in the report.
I have write this query but it give even when a filed in the last record is null while it has data in previous records.
SELECT patient.bartar_id,patient.bartar_enteringthesystem,patient.bartar_proviencename,
patient.bartar_cityname,patient.bartar_coloplastrepname,patient.bartar_consultorname,
patient.bartar_provienceofsurgeryname,patient.bartar_cityofsurgeryname,
patient.bartar_surgeryhospitalname,patient.bartar_doctor,patient.bartar_patientstatusname,
patient.bartar_ostomytypename, patient.bartar_ostomytimename,
r.bartar_date,r.bartar_delay,r.bartar_nextcall,r.new_newcaller,
r.bartar_brandname,r.bartar_pastename,r.bartar_bagname,r.bartar_accname,
r.bartar_pastepermonth,r.bartar_bagepermonth,r.bartar_insuranceinfo,
patient.bartar_deathhealeddate,patient.bartar_dateofseurgery
FROM [Bartar_MSCRM].[dbo].[Filteredbartar_newpaitient] as patient
JOIN (SELECT r.*, row_number() over (partition by r.bartar_patientname
order by r.bartar_date desc) as seqnum
FROM [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] as r
where r.bartar_delay is not null
) r
ON r.bartar_patientname = patient.bartar_newpaitientid and seqnum = 1
ORDER BY patient.bartar_id DESC ;
patient Table
Report Table
Join
Final Report What I want
this is a sample,
in your case you have to get the value of each column in a subquery (either in the join statement, or in the main select statement
example:
inner join (
select distinct bartar_patientname
,(select top 1 bartar_pastePerMonth from [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] c2 where c2.bartar_patientname = cte.bartar_patientname and c2.bartar_pastePerMonth is not null order by c2.bartar_date desc) as bartar_date
,(select top 1 bartar_acc from [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] c2 where c2.bartar_patientname = cte.bartar_patientname and c2.bartar_acc is not null order by c2.bartar_date desc) as bartar_acc
,(select top 1 bartar_insuranceinfo from [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] c2 where c2.bartar_patientname = cte.bartar_patientname and c2.bartar_insuranceinfo is not null order by c2.bartar_date desc) as bartar_insuranceinfo
,(select top 1 bartar_brand from [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] c2 where c2.bartar_patientname = cte.bartar_patientname and c2.bartar_brand is not null order by c2.bartar_date desc) as bartar_brand
from [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] cte
) r
Again, this is a sample of the solution.
Your script is fine as it looks, so I'll just place that on a temporary table for now, and do a per sequence query and filter it by "OR" afterwards.
Please try the script below.
SELECT patient.bartar_id,patient.bartar_enteringthesystem,patient.bartar_proviencename,
patient.bartar_cityname,patient.bartar_coloplastrepname,patient.bartar_consultorname,
patient.bartar_provienceofsurgeryname,patient.bartar_cityofsurgeryname,
patient.bartar_surgeryhospitalname,patient.bartar_doctor,patient.bartar_patientstatusname,
patient.bartar_ostomytypename, patient.bartar_ostomytimename,
r.bartar_date,r.bartar_delay,r.bartar_nextcall,r.new_newcaller,
r.bartar_brandname,r.bartar_pastename,r.bartar_bagname,r.bartar_accname,
r.bartar_pastepermonth,r.bartar_bagepermonth,r.bartar_insuranceinfo,
patient.bartar_deathhealeddate,patient.bartar_dateofseurgery
, ROW_NUMBER() OVER (PARTITION BY r.bartar_newpaitientid, r.bartar_pastepermonth ORDER BY r.bartar_date DESC) AS bartarpaste_sequence
, ROW_NUMBER() OVER (PARTITION BY r.bartar_newpaitientid, r.bartar_acc ORDER BY r.bartar_date DESC) AS bartaracc_sequence
, ROW_NUMBER() OVER (PARTITION BY r.bartar_newpaitientid, r.bartar_insuranceinfo ORDER BY r.bartar_date DESC) AS bartarins_sequence
, ROW_NUMBER() OVER (PARTITION BY r.bartar_newpaitientid, r.bartar_brandname ORDER BY r.bartar_date DESC) AS bartarbrd_sequence
INTO #tmpPatientReport
FROM [Bartar_MSCRM].[dbo].[Filteredbartar_newpaitient] as patient
JOIN (SELECT r.*, row_number() over (partition by r.bartar_patientname
order by r.bartar_date desc) as seqnum
FROM [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] as r
where r.bartar_delay is not null
) r
ON r.bartar_patientname = patient.bartar_newpaitientid and seqnum = 1
ORDER BY patient.bartar_id DESC;
SELECT
*
FROM #tmpPatientReport
WHERE
bartarpaste_sequence = 1
OR bartaracc_sequence = 1
OR bartarins_sequence = 1
OR bartarbrd_sequence = 1
just do join for each column
JOIN (SELECT r.colx, row_number() over (partition by r.bartar_patientname
order by r.bartar_date desc) as seqnum
FROM [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] as r
where r.bartar_delay is not null and r.colx in not null
) rx
ON rx.bartar_patientname = patient.bartar_newpaitientid and seqnum = 1

Oracle SQL Query Assistance

Given a table:
ID NUMBER
OBJECTID NUMBER
CATEGORYID NUMBER
SCORE NUMBER
SCOREDATE DATE
Is it possible to efficiently retrieve the last score (based on SCOREDATE) in each distinct category for a given object in one query?
Try:
select v.* from (
select category_id,
score,
scoredate,
row_number() over (partition by category_id order by scoredate desc) rn
from MyTable) v
where rn=1
What you want falls into the [greatest-n-per-group] tag. One way to achieve the result:
SELECT
t.CategoryId
, t.Score
FROM
( SELECT
CategoryId
, MAX(ScoreDate) AS LastScoreDate
FROM
TableX
WHERE
ObjectId = #ObjectId
GROUP BY
CategoryId
) AS grp
JOIN
TableX AS t
ON grp.Category = t.CategoryId
AND grp.LastScoreDate = t.ScoreDate
WHERE
t.ObjectId = #ObjectId