SQL Select min(date) group by with corresponding row - sql

There are some subsets of a parent set with release date. And I want the earliest release date with the subset's no, but in a group base.
I try this:
SELECT Sub_No,Parent_No,MIN(DATE)
FROM mytable
WHERE other_CODE IS NULL
GROUP BY Sub_No,Parent_No
ORDER BY Parent_No DESC
but I get this: but I only want the most earliest released one as the 2015-02-12's Sub_No and Parent_No.
Any help will be highly appreciated!
Parent_No (No column name) Sub_No
07 2015-02-12 90
07 2015-11-03 88
07 2017-02-06 59

You can use the NOT EXISTS as follows:
SELECT Sub_No,Parent_No,DATE
FROM mytable t
where other_CODE IS NULL
and not exists (select 1 from mytable tt
where tt.Parent_No = t.Parent_No and tt.date < t.date
and tt.other_CODE is null)
If your database supports analytical function then you can use it as follows:
select Sub_No,Parent_No, DATE from
(SELECT Sub_No,Parent_No, DATE,
row_number() over (partition by parent_no order by date) as rn
FROM mytable
WHERE other_CODE IS NULL) t
where rn = 1

Only if Parent_No and DATE are unique key of mytable, then you can do the trick like this:
SELECT * FROM mytable
INNER JOIN
(
SELECT Parent_No,MIN(DATE) as MinDate
FROM mytable
WHERE other_CODE IS NULL
GROUP BY Parent_No
) AS Filter
on mytable.Parent_No = Filter.Parent_No and mytable.DATE = Filter.MinDate

Related

SQL - One Table with Two Date Columns. Count and Join

I have a table (vOKPI_Tickets) that has the following columns:
|CreationDate | CompletionDate|
I'd like to get a count on each of those columns, and group them by date. It should look something like this when complete:
| Date | Count-Created | Count-Completed |
I can get each of the counts individually, by doing something like this:
SELECT COUNT(TicketId)
FROM vOKPI_Tickets
GROUP BY CreationDate
and
SELECT COUNT(TicketId)
FROM vOKPI_Tickets
GROUP BY CreationDate
How can I combine the output into one table? I should also note that this will become a View.
Thanks in advance
Simple generic approach:
select
coalesce(crte.creationdate, cmpl.CompletionDate) as theDate,
crte.cnt as created,
cmpl.cnt as completed
from
(select creationdate, count (*) as cnt from vOKPI_Tickets where creationdate is not null group by creationdate) crte
full join
(select CompletionDate, count (*) as cnt from vOKPI_Tickets where CompletionDate is not null group by CompletionDate) cmpl
on crte.creationdate = cmpl.CompletionDate
You can unpivot and aggregate. A general method is:
select dte, sum(created), sum(completed)
from ((select creationdate as dte, 1 as created, 0 as completed
from vOKPI_Tickets
) union all
(select completed as dte, 0 created, 1 as completed
from vOKPI_Tickets
)
) t
group by dte;
In SQL Server, you can use cross apply for this:
select d.dt, sum(d.is_completed) count_created, sum(d.is_completed) count_completed
from vokpi_tickets t
cross apply (values (creationdate, 1, 0), (completion_date, 0, 1)) as d(dt, is_created, is_completed)
where d.dt is not null
group by d.dt

Pivot the dates based on event/status values

I have data like below for a SQL query and want to convert that as below where 106 is event start and 110 is event end date.
If the sequence of records is always 106/110 for each (orders_sk_seq, order_product_sk_seq) tuple, then you can just use lead():
select *
from (
select
orders_sk_seq,
order_product_sk,
create_datetime start_date,
status_code,
lead(create_datetime) over(
partition by orders_sk_seq, order_product_sk_seq order by create_datetime
) end_date
from mytable
) t
where status_code = 106
order by start_date
WITH Order_CTE AS
(
SELECT order_Product_sk_seq,status_code,create_datetime
,ROW_NUMBER() OVER (PARTITION BY order_Product_sk_seq,status_code ORDER BY create_datetime) AS SEQUENCE
FROM atclose.order_status
where status_code IN (106,110)
)
SELECT
b1.order_Product_sk_seq
,b1.create_datetime
,b2.create_datetime
FROM Order_CTE b1
JOIN (
SELECT
order_Product_sk_seq
,create_datetime
,ROW_NUMBER() OVER (PARTITION BY order_Product_sk_seq ORDER BY create_datetime) AS SEQUENCE
FROM atclose.order_status
WHERE status_code = 110) b2
ON b1.order_Product_sk_seq = b2.order_Product_sk_seq
AND b1.Sequence = b2.Sequence
WHERE b1.status_code = 106;

How to get stockBalance of every end of the month

I have Four columns like
Date Customer InvoiceNo StockBalance
11/29/2017 A IN000414 5000
11/30/2017 B IN000415 4000
12/27/2017 A IN000416 3500
12/30/2017 B IN000417 2000
I want to get Stockbalance of every end of month, I need the output as
11/30/2017 B IN000415 4000
12/30/2017 B IN000417 2000
how could i get this could anybody guide to me?
You can use row_number() function :
select t.*
from (select *, row_number() over (partition by year(date), month(date) order by date desc) seq
from table
) t
where seq = 1;
EDIT : You want apply :
select t.*
from table t cross apply
( select top (1) t1.*
from table t1
where t1.Customer = t.Customer and
EOMONTH(t1.Dat) = t.Dat
order by t1.Dat desc
) t1;
Use row_number(), but be sure you include the year and month in the calculation:
select t.*
from (select t.*,
row_number() over (partition by year(date), month(date)
order by date desc
) as seqnum
from t
) t
where seqnum = 1;

How to find latest status of the day in SQL Server

I have a SQL Server question that I'm trying to figure out at work:
There is a table with a status field which can contain a status called "Participate." I am only trying to find records if the latest status of the day is "Participate" and only if the status changed on the same day from another status to "Participate."
I don't want any records where the status was already "Participate." It must have changed to that status on the same day. You can tell when the status was changed by the datetime field ChangedOn.
In the sample below I would only want to bring back ID 1880 since the status of "Participated" has the latest timestamp. I would not bring back ID 1700 since the last record is "Other," and I would not bring back ID 1600 since "Participated" is the only status of that day.
ChangedOn Status ID
02/01/17 15:23 Terminated 1880
02/01/17 17:24 Participated 1880
02/01/17 09:00 Other 1880
01/31/17 01:00 Terminated 1700
01/31/17 02:00 Participated 1700
01/31/17 03:00 Other 1700
01/31/17 02:00 Participated 1600
I was thinking of using a Window function, but I'm not sure how to get started on this. It's been a few months since I've written a query like this so I'm a bit out of practice.
Thanks!
You can use window functions for this:
select t.*
from (select t.*,
row_number() over (partition by cast(ChangedOn as date)
order by ChangedOn desc
) as seqnum,
sum(case when status <> 'Participate' then 1 else 0 end) over (partition by cast(ChangedOn as date)) as num_nonparticipate
from t
) t
where (seqnum = 1 and ChangedOn = 'Participate') and
num_nonparticipate > 0;
Can you check this?
WITH sample_table(ChangedOn,Status,ID)AS(
SELECT CONVERT(DATETIME,'02/01/2017 15:23'),'Terminated',1880 UNION ALL
SELECT '02/01/2017 17:24','Participated',1880 UNION ALL
SELECT '02/01/2017 09:00','Other',1880 UNION ALL
SELECT '01/31/2017 01:00','Terminated',1700 UNION ALL
SELECT '01/31/2017 02:00','Participated',1700 UNION ALL
SELECT '01/31/2017 03:00','Other',1700 UNION ALL
SELECT '01/31/2017 02:00','Participated',1600
)
SELECT ID FROM (
SELECT *
,ROW_NUMBER()OVER(PARTITION BY ID,CONVERT(VARCHAR,ChangedOn,112) ORDER BY ChangedOn) AS rn
,COUNT(0)OVER(PARTITION BY ID,CONVERT(VARCHAR,ChangedOn,112)) AS cnt
,CASE WHEN Status<>'Participated' THEN 1 ELSE 0 END AS ss
,SUM(CASE WHEN Status!='Participated' THEN 1 ELSE 0 END)OVER(PARTITION BY ID,CONVERT(VARCHAR,ChangedOn,112)) AS OtherStatusCnt
FROM sample_table
) AS t WHERE t.rn=t.cnt AND t.Status='Participated' AND t.OtherStatusCnt>0
--Return:
1880
try this with other sample data,
declare #t table(ChangedOn datetime,Status varchar(50),ID int)
insert into #t VALUES
('02/01/17 15:23', 'Terminated' ,1880)
,('02/01/17 17:24', 'Participated' ,1880)
,('02/01/17 09:00', 'Other' ,1880)
,('01/31/17 01:00', 'Terminated' ,1700)
,('01/31/17 02:00', 'Participated' ,1700)
,('01/31/17 03:00', 'Other' ,1700)
,('01/31/17 02:00', 'Participated' ,1600)
;
WITH CTE
AS (
SELECT *
,row_number() OVER (
PARTITION BY id
,cast(ChangedOn AS DATE) ORDER BY ChangedOn DESC
) AS seqnum
FROM #t
)
SELECT *
FROM cte c
WHERE seqnum = 1
AND STATUS = 'Participated'
AND EXISTS (
SELECT id
FROM cte c1
WHERE seqnum > 1
AND c.id = c1.id
)
2nd query,this is better
here CTE is same
SELECT *
FROM cte c
WHERE seqnum = 1
AND STATUS = 'Participated'
AND EXISTS (
SELECT id
FROM cte c1
WHERE STATUS != 'Participated'
AND c.id = c1.id
)

writing a sql query in MySQL with subquery on the same table

I have a table svn1:
id | date | startdate
23 2002-12-04 2000-11-11
23 2004-08-19 2005-09-10
23 2002-09-09 2004-08-23
select id,startdate from svn1 where startdate>=(select max(date) from svn1 where id=svn1.id);
Now the problem is how do I let know the subquery to match id with the id in the outer query. Obviously id=svn1.id wont work. Thanks!
If you have the time to read more:
This really is a simplified version of asking what I really am trying to do here. my actual query is something like this
select
id, count(distinct archdetails.compname)
from
svn1,svn3,archdetails
where
svn1.name='ant'
and svn3.name='ant'
and archdetails.name='ant'
and type='Bug'
and svn1.revno=svn3.revno
and svn3.compname=archdetails.compname
and
(
(startdate>=sdate and startdate<=edate)
or
(
sdate<=(select max(date) from svn1 where type='Bug' and id=svn1.id)
and
edate>=(select max(date) from svn1 where type='Bug' and id=svn1.id)
)
or
(
sdate>=startdate
and
edate<=(select max(date) from svn1 where type='Bug' and id=svn1.id)
)
)
group by id LIMIT 0,40;
As you notice select max(date) from svn1 where type='Bug' and id=svn1.id has to be calculated many times.
Can I just calculate this once and store it using AS and then use that variable later. Main problem is to correct id=svn1.id so as to correctly equate it to the id in the outer table.
I'm not sure you can eliminate the repetition of the subquery, but the subquery can reference the main query if you use a table alias, as in the following:
select id,
count(distinct archdetails.compname)
from svn1 s1,
svn3 s3,
archdetails a
where s1.name='ant' and
s3.name='ant' and
a.name='ant' and
type='Bug' and
s1.revno=s3.revno and
s3.compname = a.compname and
( (startdate >= sdate and startdate<=edate) or
(sdate <= (select max(date)
from svn1
where type='Bug' and
id=s1.id and
edate>=(select max(date)
from svn1
where type='Bug' and
id=s1.id)) or
(sdate >= startdate and edate<=(select max(date)
from svn1
where type='Bug' and
id=s1.id)) )
group by id LIMIT 0,40;
Share and enjoy.
You should be able to left join to a sub-select so you only run the query once. Then you can do a join condition to pull out the maximum for the ID on each record as shown below:
SELECT id,
COUNT(DISTINCT archdetails.compname)
FROM svn1,
svn3,
archdetails
LEFT JOIN (
SELECT id, MAX(date) AS MaximumDate
FROM svn1
WHERE TYPE = 'Bug'
GROUP BY id
) AS MaxDate ON MaxDate.id = svn1.id
WHERE svn1.name = 'ant'
AND svn3.name = 'ant'
AND archdetails.name = 'ant'
AND TYPE = 'Bug'
AND svn1.revno = svn3.revno
AND svn3.compname = archdetails.compname
AND (
(startdate >= sdate AND startdate <= edate)
OR (
sdate <= MaxDate.MaximumDate
AND edate >= MaxDate.MaximumDate
)
OR (
sdate >= startdate
AND edate <= MaxDate.MaximumDate
)
)
GROUP BY
id LIMIT 0,
40;
Try using alias, something like this should work:
select s.id,s.startdate from svn1.s where s.startdate>=(select max(date) from svn1.s2 where s.id=s2.id);