Get Products By Minimum Duration Between StartDate and EndDate? - sql

I am having some problem to get distinct products between start-data and end-date and minimum duration. My table structure is,
ID SKU Desc1 Desc2 Price PriceFrom PriceTo
-------------------------------------------------------
1 xxxx xxxx xxxx 12 1/1/2014 1/1/2015
1 xxx xxxx xxxx 12 1/1/2014 2/1/2014
1 xxx xxxx xxxx 12 9/1/2014 10/1/2014
Let's say today's date is 09/04/2014. So we have 2 options record 1 and 3(because 2 is outside the range of today's date) but I choose 3 because the duration of 3rd record is less than 1st record?

You can do this by using order by and top:
select top 1 t.*
from table t
where cast(getdate() as date) >= PriceFrom and cast(getdate() as date) <= PriceTo
order by datediff(day, PriceFrom, PriceTo) asc;
update:
SELECT
MIN(DATEDIFF(DAY, t.PriceFrom, t.PriceTo)),
t.ID,
t.Name,
t.ModelNumber,
t.Description,
t.Price,
t.NewPrice,
t.SKU
FROM Products t
WHERE GETDATE() BETWEEN PriceFrom AND PriceTo
GROUP BY t.ID,
t.Name,
t.ModelNumber,
t.Description,
t.Price,
t.NewPrice,
t.SKU

Related

Aggregate a subtotal column based on two dates of that same row

Situation:
I have 5 columns
id
subtotal (price of item)
order_date (purchase date)
updated_at (if refunded or any other status change)
status
Objective:
I need the order date as column 1
I need to get the subtotal for each day regardless if of the status as column 2
I need the subtotal amount for refunds for the third column.
Example:
If a purchase is made on May 1st and refunded on May 3rd. The output should look like this
+-------+----------+--------+
| date | subtotal | refund |
+-------+----------+--------+
| 05-01 | 10.00 | 0.00 |
| 05-02 | 00.00 | 0.00 |
| 05-03 | 00.00 | 10.00 |
+-------+----------+--------+
while the row will look like that
+-----+----------+------------+------------+----------+
| id | subtotal | order_date | updated_at | status |
+-----+----------+------------+------------+----------+
| 123 | 10 | 2019-05-01 | 2019-05-03 | refunded |
+-----+----------+------------+------------+----------+
Query:
Currently what I have looks like this:
Note: Timezone discrepancy therefore bring back the dates by 8 hours.
;with cte as (
select id as orderid
, CAST(dateadd(hour,-8,order_date) as date) as order_date
, CAST(dateadd(hour,-8,updated_at) as date) as updated_at
, subtotal
, status
from orders
)
select
b.dates
, sum(a.subtotal_price) as subtotal
, -- not sure how to aggregate it to get the refunds
from Orders as o
inner join cte as a on orders.id=cte.orderid
inner join (select * from cte where status = ('refund')) as b on o.id=cte.orderid
where dates between '2019-05-01' and '2019-05-31'
group by dates
And do I need to join it twice? Hopefully not since my table is huge.
This looks like a job for a Calendar Table. Bit of a stab in the dark, but:
--Overly simplistic Calendar table
CREATE TABLE dbo.Calendar (CalendarDate date);
WITH N AS(
SELECT N
FROM (VALUES(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL))N(N)),
Tally AS(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1 AS I
FROM N N1, N N2, N N3, N N4, N N5) --Many years of data
INSERT INTO dbo.Calendar
SELECT DATEADD(DAY, T.I, 0)
FROM Tally T;
GO
SELECT C.CalendarDate AS [date],
CASE C.CalendarDate WHEN V.order_date THEN subtotal ELSE 0 END AS subtotal,
CASE WHEN C.CalendarDate = V.updated_at AND V.[status] = 'refunded' THEN subtotal ELSE 0.00 END AS subtotal
FROM (VALUES(123,10.00,CONVERT(date,'20190501'),CONVERT(date,'20190503'),'refunded'))V(id,subtotal,order_date,updated_at,status)
JOIN dbo.Calendar C ON V.order_date <= C.CalendarDate AND V.updated_at >= C.CalendarDate;
GO
DROP TABLE dbo.Calendar;
Consider joining on a recursive CTE of sequential dates:
WITH dates AS (
SELECT CONVERT(datetime, '2019-01-01') AS rec_date
UNION ALL
SELECT DATEADD(d, 1, CONVERT(datetime, rec_date))
FROM dates
WHERE rec_date < '2019-12-31'
),
cte AS (
SELECT id AS orderid
, CAST(dateadd(hour,-8,order_date) AS date) as order_date
, CAST(dateadd(hour,-8,updated_at) AS date) as updated_at
, subtotal
, status
FROM orders
)
SELECT rec_date AS date,
CASE
WHEN c.order_date = d.rec_date THEN subtotal
ELSE 0
END AS subtotal,
CASE
WHEN c.updated_at = d.rec_date THEN subtotal
ELSE 0
END AS refund
FROM cte c
JOIN dates d ON d.rec_date BETWEEN c.order_date AND c.updated_at
WHERE c.status = 'refund'
option (maxrecursion 0)
GO
Rextester demo

Automatically assign values from a day before when today's data is not present

I have a table that contains columns such as Price_Date, Catagory, Size, Grade, Country, and Price. The table sometimes do not contain data for Sundays or holidays (like christmas, thanksgivings, etc).
What I am trying to achieve here is when the table do not contain data for certain date, I want it to automaticaly populate data from the pervious day.
For example, the table do not contain 01/06/2019 data. It does not have the date at all. In this case, I want to automatically assign 01/06/2019 date which was missing and populate it with 01/05/2019 data.
Price_Date Catagory Size Grade Country Price
--------------------------------------------------------------------------------
2019-01-05 0 32 1 2 24.25
2019-01-05 0 36 1 2 24.25
2019-01-05 0 40 1 2 24.25
2019-01-05 0 48 1 2 24.25
2019-01-05 0 60 1 2 23.25
2019-01-05 0 70 1 2 21.25
2019-01-05 0 84 1 2 17.25
Here is the SQL query that I came up with.
And sorry if I am making this post in a wrong section.
WITH MyRowSet
AS
(
select distinct
d.date_key
,p.Size_Value
,Catagory_Value
,cast (Price_Date as datetime) as prev_effex
,ROW_NUMBER() OVER (PARTITION BY date_key,Size_Value,Catagory_Value order by date_key,cast (Price_Date as datetime) desc) AS RowNum
from
FSPPRICE P
CROSS APPLY Dim_Time d
where
d.Date_KEY <(GETDATE()) and
(D.Date_KEY > (select min(cast (Price_Date as datetime)) as min_date from FSPPRICE))
and
cast (Price_Date as datetime) <> D.Date_Key and cast (Price_Date as datetime) < D.Date_Key
group by d.date_key,Price_Date,Size_Value,Catagory_Value
)
SELECT
r.Date_KEY AS effectiveon
,P.Catagory_Value
,cast(P.Size_Value as varchar) as Size_Value
,P.Grade_Value
,P.Country
,P.Price
,P.Active_Code
FROM MyRowSet AS R INNER JOIN
FSPPRICE AS P
ON r.prev_effex = P.Price_Date and r.Catagory_Value=p.Catagory_Value and r.Size_Value=p.Size_Value WHERE (rownum < 2)
declare #dt date = GETDATE();--any given date
select #dt as price_date, category, size, grade, grade, country, price
from
#t
where price_date in (select MAX(price_date) price_date from #t where price_date <= #dt)

SQL - Find if column dates include at least partially a date range

I need to create a report and I am struggling with the SQL script.
The table I want to query is a company_status_history table which has entries like the following (the ones that I can't figure out)
Table company_status_history
Columns:
| id | company_id | status_id | effective_date |
Data:
| 1 | 10 | 1 | 2016-12-30 00:00:00.000 |
| 2 | 10 | 5 | 2017-02-04 00:00:00.000 |
| 3 | 11 | 5 | 2017-06-05 00:00:00.000 |
| 4 | 11 | 1 | 2018-04-30 00:00:00.000 |
I want to answer to the question "Get all companies that have been at least for some point in status 1 inside the time period 01/01/2017 - 31/12/2017"
Above are the cases that I don't know how to handle since I need to add some logic of type :
"If this row is status 1 and it's date is before the date range check the next row if it has a date inside the date range."
"If this row is status 1 and it's date is after the date range check the row before if it has a date inside the date range."
I think this can be handled as a gaps and islands problem. Consider the following input data: (same as sample data of OP plus two additional rows)
id company_id status_id effective_date
-------------------------------------------
1 10 1 2016-12-15
2 10 1 2016-12-30
3 10 5 2017-02-04
4 10 4 2017-02-08
5 11 5 2017-06-05
6 11 1 2018-04-30
You can use the following query:
SELECT t.id, t.company_id, t.status_id, t.effective_date, x.cnt
FROM company_status_history AS t
OUTER APPLY
(
SELECT COUNT(*) AS cnt
FROM company_status_history AS c
WHERE c.status_id = 1
AND c.company_id = t.company_id
AND c.effective_date < t.effective_date
) AS x
ORDER BY company_id, effective_date
to get:
id company_id status_id effective_date grp
-----------------------------------------------
1 10 1 2016-12-15 0
2 10 1 2016-12-30 1
3 10 5 2017-02-04 2
4 10 4 2017-02-08 2
5 11 5 2017-06-05 0
6 11 1 2018-04-30 0
Now you can identify status = 1 islands using:
;WITH CTE AS
(
SELECT t.id, t.company_id, t.status_id, t.effective_date, x.cnt
FROM company_status_history AS t
OUTER APPLY
(
SELECT COUNT(*) AS cnt
FROM company_status_history AS c
WHERE c.status_id = 1
AND c.company_id = t.company_id
AND c.effective_date < t.effective_date
) AS x
)
SELECT id, company_id, status_id, effective_date,
ROW_NUMBER() OVER (PARTITION BY company_id ORDER BY effective_date) -
cnt AS grp
FROM CTE
Output:
id company_id status_id effective_date grp
-----------------------------------------------
1 10 1 2016-12-15 1
2 10 1 2016-12-30 1
3 10 5 2017-02-04 1
4 10 4 2017-02-08 2
5 11 5 2017-06-05 1
6 11 1 2018-04-30 2
Calculated field grp will help us identify those islands:
;WITH CTE AS
(
SELECT t.id, t.company_id, t.status_id, t.effective_date, x.cnt
FROM company_status_history AS t
OUTER APPLY
(
SELECT COUNT(*) AS cnt
FROM company_status_history AS c
WHERE c.status_id = 1
AND c.company_id = t.company_id
AND c.effective_date < t.effective_date
) AS x
), CTE2 AS
(
SELECT id, company_id, status_id, effective_date,
ROW_NUMBER() OVER (PARTITION BY company_id ORDER BY effective_date) -
cnt AS grp
FROM CTE
)
SELECT company_id,
MIN(effective_date) AS start_date,
CASE
WHEN COUNT(*) > 1 THEN DATEADD(DAY, -1, MAX(effective_date))
ELSE MIN(effective_date)
END AS end_date
FROM CTE2
GROUP BY company_id, grp
HAVING COUNT(CASE WHEN status_id = 1 THEN 1 END) > 0
Output:
company_id start_date end_date
-----------------------------------
10 2016-12-15 2017-02-03
11 2018-04-30 2018-04-30
All you want know is those records from above that overlap with the specified interval.
Demo here with somewhat more complicated use case.
Maybe this is what you are looking for? For these kind of questions, you need to join two instance of your table, in this case I am just joining with next record by Id, which probably is not totally correct. To do it better, you can create a new Id using a windowed function like row_number, ordering the table by your requirement criteria
If this row is status 1 and it's date is before the date range check
the next row if it has a date inside the date range
declare #range_st date = '2017-01-01'
declare #range_en date = '2017-12-31'
select
case
when csh1.status_id=1 and csh1.effective_date<#range_st
then
case
when csh2.effective_date between #range_st and #range_en then true
else false
end
else NULL
end
from company_status_history csh1
left join company_status_history csh2
on csh1.id=csh2.id+1
Implementing second criteria:
"If this row is status 1 and it's date is after the date range check
the row before if it has a date inside the date range."
declare #range_st date = '2017-01-01'
declare #range_en date = '2017-12-31'
select
case
when csh1.status_id=1 and csh1.effective_date<#range_st
then
case
when csh2.effective_date between #range_st and #range_en then true
else false
end
when csh1.status_id=1 and csh1.effective_date>#range_en
then
case
when csh3.effective_date between #range_st and #range_en then true
else false
end
else null -- ¿?
end
from company_status_history csh1
left join company_status_history csh2
on csh1.id=csh2.id+1
left join company_status_history csh3
on csh1.id=csh3.id-1
I would suggest the use of a cte and the window functions ROW_NUMBER. With this you can find the desired records. An example:
DECLARE #t TABLE(
id INT
,company_id INT
,status_id INT
,effective_date DATETIME
)
INSERT INTO #t VALUES
(1, 10, 1, '2016-12-30 00:00:00.000')
,(2, 10, 5, '2017-02-04 00:00:00.000')
,(3, 11, 5, '2017-06-05 00:00:00.000')
,(4, 11, 1, '2018-04-30 00:00:00.000')
DECLARE #StartDate DATETIME = '2017-01-01';
DECLARE #EndDate DATETIME = '2017-12-31';
WITH cte AS(
SELECT *
,ROW_NUMBER() OVER (PARTITION BY company_id ORDER BY effective_date) AS rn
FROM #t
),
cteLeadLag AS(
SELECT c.*, ISNULL(c2.effective_date, c.effective_date) LagEffective, ISNULL(c3.effective_date, c.effective_date)LeadEffective
FROM cte c
LEFT JOIN cte c2 ON c2.company_id = c.company_id AND c2.rn = c.rn-1
LEFT JOIN cte c3 ON c3.company_id = c.company_id AND c3.rn = c.rn+1
)
SELECT 'Included' AS RangeStatus, *
FROM cteLeadLag
WHERE status_id = 1
AND effective_date BETWEEN #StartDate AND #EndDate
UNION ALL
SELECT 'Following' AS RangeStatus, *
FROM cteLeadLag
WHERE status_id = 1
AND effective_date > #EndDate
AND LagEffective BETWEEN #StartDate AND #EndDate
UNION ALL
SELECT 'Trailing' AS RangeStatus, *
FROM cteLeadLag
WHERE status_id = 1
AND effective_date < #EndDate
AND LeadEffective BETWEEN #StartDate AND #EndDate
I first select all records with their leading and lagging Dates and then I perform your checks on the inclusion in the desired timespan.
Try with this, self-explanatory. Responds to this part of your question:
I want to answer to the question "Get all companies that have been at
least for some point in status 1 inside the time period 01/01/2017 -
31/12/2017"
Case that you want to find those id's that have been in any moment in status 1 and have records in the period requested:
SELECT *
FROM company_status_history
WHERE id IN
( SELECT Id
FROM company_status_history
WHERE status_id=1 )
AND effective_date BETWEEN '2017-01-01' AND '2017-12-31'
Case that you want to find id's in status 1 and inside the period:
SELECT *
FROM company_status_history
WHERE status_id=1
AND effective_date BETWEEN '2017-01-01' AND '2017-12-31'

SQL To sum up amount for multiple rows at same column

I need to sum up the amount for different products but same month for same serviceid. Here is the table:
ServiceID PRODUCT AMT DATE
1 prod1 20 1/1/2013
1 prod2 40 1/1/2013
1 prod1 30 2/1/2013
1 prod2 50 2/1/2013
I need to add prod1+prod2 for 1/1/2013, prod1+prod2 for 2/1/2013
This is the result that I want:
ServiceID PRODUCT AMT DATE
1 prod1 60 1/1/2013
1 prod2 80 2/1/2013
select serviceID, product, sum(amt), date
from table
where date >= 1/1/2013
and date <= 2/1/2013
group by 1, 2, 4
The group by doesn't get the result that I want.
In reality I can't specify product because it has more than what I post here.
select serviceID, 'prod1+prod2', sum(amt), date
from table
where date >= 1/1/2013
and date <= 2/1/2013
group by 1, 4
you can use DATEADD, DATEDIFF to get monthly aggregation
SELECT serviceID,
STUFF( ( SELECT DISTINCT '&' + PRODUCT
FROM Table1 T1
WHERE T1.ServiceID = T.ServiceID
FOR XML PATH(''), TYPE).value('.','nvarchar(max)') , 1,1,'') as Products,
sum(amt) as totalAmount,
DATEADD(MONTH, DATEDIFF(MONTH,0,[DATE]), 0) as month
FROM Table1 T
GROUP BY serviceId, DATEADD(MONTH, DATEDIFF(MONTH,0,[DATE]), 0)
As latest comment, adding total column using cross apply
SELECT T1.ServiceID, PRODUCT, AMT, [DATE], C.Total
FROM Table1 T1
CROSS APPLY
(
SELECT serviceID,
sum(amt) as Total,
DATEADD(MONTH, DATEDIFF(MONTH,0,[DATE]), 0) as month
FROM Table1 T
GROUP BY serviceId, DATEADD(MONTH, DATEDIFF(MONTH,0,[DATE]), 0)
) C
where T1.ServiceID = C.serviceId
AND T1.[DATE] = C.month

Running Total on date column

I have the following data in my table:
id invoice_id date ammount
1 1 2012-01-01 100.00
20 1 2012-01-31 50.00
470 1 2012-01-15 300.00
Now, I need to calculate running total for an invoice in some period. So, the output for this data sample should look like this:
id invoice_id date ammount running_total
1 1 2012-01-01 100.00 100.00
470 1 2012-01-15 300.00 400.00
20 1 2012-01-31 50.00 450.00
I tried with this samples http://www.sqlusa.com/bestpractices/runningtotal/ and several others, but the problem is that I could have entries like id 20, date 2012-01-31 and id 120, date 2012-01-01, and then I couldn't use NO = ROW_NUMBER(over by date)... in first select and then ID < NO in second select for calculating running total.
DECLARE #DateStart DATE='2012-01-01';
WITH cte
AS (SELECT id = Row_number() OVER(ORDER BY [date]),
DATE,
myid = id,
invoice_id,
orderdate = CONVERT(DATE, DATE),
ammount
FROM [Table_2]
WHERE DATE >= #DateStart)
SELECT myid,
invoice_id,
DATE,
ammount,
runningtotal = (SELECT SUM(ammount)
FROM cte
WHERE id <= a.id)
FROM cte AS a
ORDER BY id