MAX value of column with corresponding columns - sql-server-2000

I am using an old SQL Server 2000.
Here is some sample data:
ROOMDATE rate bus_id quantity
2018-09-21 00:00:00.000 129 346686 2
2018-09-21 00:00:00.000 162 354247 36
2018-09-21 00:00:00.000 159 382897 150
2018-09-21 00:00:00.000 120 556111 25
2018-09-22 00:00:00.000 129 346686 8
2018-09-22 00:00:00.000 162 354247 86
2018-09-22 00:00:00.000 159 382897 150
2018-09-22 00:00:00.000 120 556111 25
2018-09-23 00:00:00.000 129 346686 23
2018-09-23 00:00:00.000 162 354247 146
2018-09-23 00:00:00.000 159 382897 9
2018-09-23 00:00:00.000 94 570135 23
Essentially what I am wanting is the MAX quantity of each day with it's corresponding rate and bus_id.
For example, I would want the following rows from my sample data above:
ROOMDATE rate bus_id quantity
2018-09-21 00:00:00.000 159 382897 150
2018-09-22 00:00:00.000 159 382897 150
2018-09-23 00:00:00.000 162 354247 146

From what I have read, SQL Server 2000 did not support ROW_NUMBER. But we can phrase your query using a subquery which finds the max quantity for each day:
SELECT t1.*
FROM yourTable t1
INNER JOIN
(
SELECT
CONVERT(char(10), ROOMDATE, 120) AS ROOMDATE,
MAX(quantity) AS max_quantity
FROM yourTable
GROUP BY CONVERT(char(10), ROOMDATE, 120)
) t2
ON CONVERT(char(10), t1.ROOMDATE, 120) = t2.ROOMDATE AND
t1.quantity = t2.max_quantity
ORDER BY
t1.ROOMDATE;
Demo

Related

Get the max month from a query that returns several years

I have a table with dates, one date per month (some months will be missing but that is expected) but several years are return. I need to get the latest month only. So if I have data for say months 8, 7, 6, etc. in 2020 then return those startDate. And for months 10, 11, and 12 it should return the StartDate from 2019 or wherever it finds it that is the latest. id and courseLength are part of the table but irrelevant for this task. StartDate is of type date.
This is the top 15 rows of the table
id StartDate courseLength
153 2020-08-31 63
153 2020-07-31 35
153 2020-06-30 60
153 2020-05-31 17
153 2020-03-31 51
153 2020-01-31 59
153 2019-12-31 30
153 2019-10-31 51
153 2019-08-31 59
153 2019-06-30 54
153 2019-05-31 17
153 2019-03-31 56
153 2019-01-31 55
153 2018-12-31 27
153 2018-10-31 54
And this is what I am expecting
id StartDate courseLength
153 2020-08-31 63
153 2020-07-31 35
153 2020-06-30 60
153 2020-05-31 17
153 2020-03-31 51
153 2020-01-31 59
153 2019-12-31 30
153 2019-10-31 51
153 2018-11-30 65
153 2018-09-31 53
153 2019-05-31 17
153 2018-04-30 13
You can use window functions:
select *
from (
select t.*,
row_number() over(partition by id, month(startdate) order by startdate desc) rn
from mytable t
) t
where rn = 1
try with this
SELECT
R.id, R.StartDate, R.courseLength
FROM (
SELECT
id, StartDate, courseLength, RANK() OVER(PARTITION BY MONTH(StartDate) ORDER BY StartDate DESC) as rank
FROM
#t
) R
WHERE
R.rank = 1
or you can use this :
select * from table
join in (
select
max(date) maxdate
, id
from table
group by
month(date) , id
) max
on max.id = table.id
and max.maxdate = table.date

Difference between consecutive rows but different column

I need to find the difference between two consecutive rows but different column. I have written a query as follows
select ticketid,createddate,expirydate ,TIMESTAMP_DIFF(
timestamp_seconds(expirytimestamp),
timestamp_seconds(
lag(createdtimestamp) over (partition by ticketid order by ticketid)
),
Minute
) AS result from table
Expected result
TicketID createdtimestamp Expirytimestamp created date Expirydate Expected result in minutes
121 1574170201 1574328843 2019-12-01 1:25:05 2019-12-01 7:24:53 NULL
121 1574170202 1574372109 2019-12-01 2:55:02 2019-12-01 8:54:50 300
121 1574256733 1574415375 2019-12-01 4:24:59 2019-12-01 10:24:47 240
121 1574285577 1574458641 2019-12-01 5:54:56 2019-12-01 11:54:44 300
121 1574328843 1574501907 2019-12-01 7:24:53 2019-12-01 13:24:41 240
121 1574372109 1574545173 2019-12-01 8:54:50 2019-12-01 14:54:38 300
121 1574415375 1574588439 2019-12-01 10:24:47 2019-12-01 16:24:35 240
Could any one suggest. It's not giving the desired result.
Data Not found this Query
Here is the actual result I got
TicketID createdtimestamp Expirytimestamp created date Expirydate Expected result in minutes
121 1574170200 1574170680 2019-11-19 13:30:00 2019-11-19 13:38:00 null
121 1574170201 1574173801 2019-11-19 13:30:01 2019-11-19 14:30:01 60
121 1574170201 1574173801 2019-11-19 13:30:01 2019-11-19 14:30:01 60
121 1574170201 1574173801 2019-11-19 13:30:01 2019-11-19 14:30:01 60
121 1574170202 1574170512 2019-11-19 13:30:02 2019-11-19 13:35:12 5
121 1574170202 1574170512 2019-11-19 13:30:02 2019-11-19 13:35:12 5
121 1574170202 1574170902 2019-11-19 13:30:02 2019-11-19 13:41:42 11
121 1574256733 1574257433 2019-11-20 13:32:13 2019-11-20 13:43:53 1453
121 1574343249 1574343949 2019-11-21 13:34:09 2019-11-21 13:45:49 1453
121 1574429680 1574430380 2019-11-22 13:34:40 2019-11-22 13:46:20 1452
121 1574516458 1574517158 2019-11-23 13:40:58 2019-11-23 13:52:38 1457
This is putting things in an arbitrary order:
lag(createdtimestamp) over (partition by ticketid order by ticketid)
I think you want:
lag(createdtimestamp) over (partition by ticketid order by createdtimestamp)
Note the difference in the order by.

How to use next date column value to calculate delta for current column

I have a temp table
BusinessDate SSQ_CompScore
2011-01-05 00:00:00.000 41
2011-01-06 00:00:00.000 6
2011-01-07 00:00:00.000 1
2011-01-10 00:00:00.000 8
2011-01-11 00:00:00.000 48
2011-01-12 00:00:00.000 50
2011-01-13 00:00:00.000 59
I need to calculate delta for each current date.
I have prepared a solution but it doesn't work where date as not consecutive.
Can you please help?
select t1.businessdate, t1.ssq_compscore, (t2.ssq_compscore - t1.ssq_compscore) as delta
from #temp t1
left join #temp t2 on t1.businessdate = DATEADD(dd,1,t2.businessdate)
where t1.businessdate >='20180814'
Result set should be as
BusinessDate SSQ_CompScore Delta
2011-01-05 00:00:00.000 41 NULL
2011-01-06 00:00:00.000 6 35
2011-01-07 00:00:00.000 1 5
2011-01-10 00:00:00.000 8 7
2011-01-11 00:00:00.000 48 40
2011-01-12 00:00:00.000 50 2
2011-01-13 00:00:00.000 59 9
Not sure if this is the most efficient way but it works as far as I see
SELECT businessdate, SSQ_CompScore ,
SSQ_CompScore - (SELECT SSQ_CompScore
FROM temp
WHERE businessdate < t1.businessdate
ORDER BY businessdate DESC
LIMIT 1) as delta
FROM temp t1
ORDER BY businessdate ASC

SQL Top 1 query on multiple columns

I have a script which returns the following table. If I put the script in a subquery and give it a pseudonym, what script would generate the top row by EVENT_DATE for each CARE_ID? This has to be compatible with SQL2000. Thank you.
CARE_ID EVENT_ID EVENT_TYPE EVENT_DATE
3 18 B 13/07/2010 00:00
78 11 C 27/07/2009 00:00
78 9 T 28/07/2009 00:00
151 49 T 21/03/2010 00:00
217 102 C 30/03/2010 00:00
355 111 C 16/07/2010 00:00
355 56 T 17/07/2010 00:00
364 774 C 23/08/2012 00:00
369 117 C 28/07/2010 00:00
631 74 T 15/01/2010 00:00
631 148 C 02/02/2010 00:00
1066 91 T 15/11/2010 00:00
2123 280 T 10/07/2011 00:00
2265 448 C 31/05/2011 00:00
2512 183 B 04/02/2014 00:00
2691 906 C 12/01/2014 00:00
2694 307 T 15/06/2011 00:00
2694 544 C 02/07/2011 00:00
2892 85 B 19/12/2011 00:00
2892 641 C 13/02/2012 00:00
3038 660 C 09/08/2011 00:00
3162 407 T 15/04/2012 00:00
3178 780 C 01/09/2012 00:00
3311 175 B 27/01/2014 00:00
3344 869 C 01/10/2013 00:00
3426 474 T 13/07/2013 00:00
3606 479 T 03/01/2014 00:00
3770 917 C 11/01/2014 00:00
This is somewhat inefficient, but I see no better way to do it in SQL Server 2000:
select
t1.care_id,
t1.event_id,
t1.event_type,
t1.event_date
from TheTable t1
join TheTable t2
on t1.care_id = t2.care_id
and t1.event_date >= t2.event_date
group by
t1.care_id,
t1.event_id,
t1.event_type,
t1.event_date
having count(*) = 1
The query currently returns the most recent record per care_id. If you need the oldest, just change the >= to <=.
SQLFiddle: http://www.sqlfiddle.com/#!3/98536/6
A potential issue with the query above is that if you have two records with the same (latest) event_date, it will return none. Let me know if such cases are possible in your data set.
Try this, assume the earliest date is top row
select x.care_id,min(x.event_date) as FirstDate
from <table> x
group by x.care_id
To get all information, you need a bit more
select x.care_id,a.event_id,a.event_type,x.firstDate as Event_date
from <table> a
join (select b.care_id,min(b.event_date) as FirstDate
from <table> b
group by b.care_id ) x
on a.care_id=x.care_id and a.event_date=x.firstDate
Just type in on the fly, but should get you what you need.
Caveat, if care_id have identical event dates, you might get some duplicate rows.

Would like to return a fake row if there is no match to my pair (for a year)

I would like to clean up some data returned from a query. This query :
select seriesId,
startDate,
reportingCountyId,
countyId,
countyName,
pocId,
pocValue
from someTable
where seriesId = 147
and pocid = 2
and countyId in (2033,2040)
order by startDate
usually returns 2 county matches for all years:
seriesId startDate reportingCountyId countyId countyName pocId pocValue
147 2004-01-01 00:00:00.000 6910 2040 CountyOne 2 828
147 2005-01-01 00:00:00.000 2998 2033 CountyTwo 2 4514
147 2005-01-01 00:00:00.000 3000 2040 CountyOne 2 2446
147 2006-01-01 00:00:00.000 3018 2033 CountyTwo 2 5675
147 2006-01-01 00:00:00.000 4754 2040 CountyOne 2 2265
147 2007-01-01 00:00:00.000 3894 2033 CountyTwo 2 6250
147 2007-01-01 00:00:00.000 3895 2040 CountyOne 2 2127
147 2008-01-01 00:00:00.000 4842 2033 CountyTwo 2 5696
147 2008-01-01 00:00:00.000 4846 2040 CountyOne 2 2013
147 2009-01-01 00:00:00.000 6786 2033 CountyTwo 2 2578
147 2009-01-01 00:00:00.000 6817 2040 CountyTwo 2 1933
147 2010-01-01 00:00:00.000 6871 2040 CountyOne 2 1799
147 2010-01-01 00:00:00.000 6872 2033 CountyTwo 2 4223
147 2011-01-01 00:00:00.000 8314 2033 CountyTwo 2 3596
147 2011-01-01 00:00:00.000 8315 2040 CountyOne 2 1559
But note please that the first entry has only CountyOne for 2004. I would like to return a fake row for CountyTwo for a graph I am doing. It would be sufficient to fill it like CountyOne only with pocValue = 0.
thanks!!!!!!!!
Try this (if you need blank row for that countryid)
; with CTE AS
(SELECT 2033 As CountryID UNION SELECT 2040),
CTE2 AS
(
seriesId, startDate, reportingCountyId,
countyId, countyName, pocId, pocValue
from someTable where
seriesId = 147 and pocid = 2 and countyId in (2033,2040)
order by startDate
)
SELECT x1.CountyId, x2.*, IsNull(pocValue,0) NewpocValue FROM CTE x
LEFT OUTER JOIN CTE2 x2 ON x1.CountyId = x2.reportingCountyId