SQL: select datetime values prior to that date based on it's value - sql

I want to select rows for a field MRD which is declared as date where it is prior for that date only.
So
(case when sum (transPoints) > 4 and MRD is that same date then 4
So if a row has a date of today, I want the case when to be triggered when the transaction points are bigger than 4 against all columns with the same date.
As you can imagine the date field will be different against many rows.

Based on what I can understand from your question, it seems that the GROUP BY clause may be what you're looking for. If your date column is in the correct format then you may have to use something like:
SELECT CAST(DateColumn as DATE)
FROM YourTable
GROUP BY CAST(DateColumn as DATE)

Related

How to look-up dates and find closest match

I have this problem using SQL where I want to find the closest matched date between two columns. Imagine this is my data-set:
'LY Date' is just last years date of 'Date' column I want to put into the new column 'Closest Date' which 'Date' that the 'LY Date', for each row, is closest to.
Example: the 22-02-2022 has last years date of 21-02-2021. This date is closest to the 15-02-2021 from 'Date' column so I put it in 'Closest Date'
I’ve referred to the columns as ‘DATE.DATE’ and ‘LYDATE.DATE’.
You can start from the LYDATE and obtain the top 1 record from DATE when DATE is sorted descending and only contains items less than ‘LYDATE’. You can then flip the DATE table and obtain the top 1 after filtering out those items less than LYDATE. This will give you the closest date that’s less than what we are looking for and the closest date that’s greater than what we are looking for.
I then calculate the date difference between those two limits to see which has smaller difference, and return the appropriate limit value as ClosestDate.
SELECT
CASE WHEN DATEDIFF(ns,lowerlimit.date, LYDate.date) < DATEDIFF(ns, upperlimit.date, LYDATE.date) THEN lowerlimit.date ELSE upperlimit.date END as ClosestDate
FROM LYDate
OUTER APPLY (SELECT TOP 1 DATE FROM DATE WHERE DATE.DATE < LYDATE.DATE ORDER BY DATE.DATE DESC) lowerlimit
OUTER APPLY (SELECT TOP 1 DATE FROM DATE WHERE DATE.DATE > LYDATE.DATE ORDER BY DATE.DATE ASC) upperlimit

Sql count and and/or condition in single statement

I am trying to build where clause condition on table having columns “Id”, itemNumber” which can be either 1 or 2 for any row and “date”.
My goal is to write where clause such that i only get “Id’s” where “itemNumber” is 2, and then if count is greater than some value it should filter whole rows to date between today and today+1, otherwise today and today+2.
I tried,
Select Id
from table
where itemNumber=2 And ((count(itemNumber)>2 and date between ‘today’ and ‘today+1’) OR (count(itemNumber)<=2 and date between ‘today’ and ‘today+2’))
I got error saying you need to have sql “having”. Am i doing it wrong?
Try it like this:
SELECT id
FROM t
WHERE itemNumber = 2
GROUP BY id
HAVING (COUNT(itemNumber) > 2 AND date BETWEEN 'today' and 'today+1'))
OR (COUNT(itemNumber) <= 2 AND date BETWEEN 'today' and 'today+2'))
Think of HAVING as a WHERE clause after you have grouped your data, which you have to do if you want to count something by group (or id).

SQL minimum date value after today's date Now()

I am writing a query and am having trouble filtering data as I would like. In the table, there is a date field and an ItemCode field. I would like to return one record per ItemCode with the earliest date that is after today.
If today is 6/6/2017 and my data looks like:
ItemCode Date
1 6/1/2017
1 6/7/2017
1 6/10/2017
2 6/2/2017
2 6/8/2017
2 6/15/2017
I would want the result to be
ItemCode Date
1 6/7/2017
2 6/8/2017
My query so far is:
SELECT PO_PurchaseOrderDetail.ItemCode, Min(PO_PurchaseOrderDetail.RequiredDate) AS NextPO
FROM PO_PurchaseOrderDetail
GROUP BY PO_PurchaseOrderDetail.ItemCode
HAVING (((Min(PO_PurchaseOrderDetail.RequiredDate))>=Now()));
The problem is that the Min function fires first and grabs the earliest dates per ItemCode, which are before today. Then the >=Now() is evaluated and because the min dates are before today, the query returns nothing.
I've tried putting the >=Now() inside the min function in the HAVING part of the query but it does not change the result.
My structure is wrong and I would appreciate any advice. Thanks!
I would approach like this for standard SQL, Access approach may vary
select PO_PurchaseOrderDetail.ItemCode,
min(PO_PurchaseOrderDetail.RequiredDate) as NextPO
from PO_PurchaseOrderDetail
where PO_PurchaseOrderDetail.RequiredDate >= Now()
group by PO_PurchaseOrderDetail.ItemCode;
Put the date condition in the where clause (not the having clause):
select ItemCode, min(Date) as NextPO
from PO_PurchaseOrderDetail
where Date > '6/6/2017'
group by ItemCode

TSQL How to make where clause match all 'IN' multiple values

I am using 4 dates in my where clause using IN. For example
...where date in ('2017-01-01', '2017-01-02', '2017-01-03','2017-01-04')
My query will return a result if even one date matches but I want my where clause to match ALL the dates. I'm sure there has to be an easy solution for this.
You want to match your data for a specific column value. Group by that column and take only those groups having all 4 dates
select col
from your_table
where date in ('2017-01-01', '2017-01-02', '2017-01-03','2017-01-04')
group by col
having count(distinct date) = 4

View data by date after Format 'mmyy'

I'm trying to answer questions like, how many POs per month do we have? Or, how many lines are there in every PO by month, etc. The original PO dates are all formatted #1/1/2013#. So my first step was to Format each PO record date into 'mmyy' so I could group and COUNT them.
This worked well but, now I cannot view the data by date... For example, I cannot ask 'How many POs after December did we get?' I think this is because SQL does not recognize mm/yy as a comparable date.
Any ideas how I could restructure this?
There are 2 queries I wrote. This is the query to format the dates. This is also the query I was trying to add the date filter to (ex: >#3/14#)
SELECT qryALL_PO.POLN, Format([PO CREATE DATE],"mm/yy") AS [Date]
FROM qryALL_PO
GROUP BY qryALL_PO.POLN, Format([PO CREATE DATE],"mm/yy");
My group and counting query is:
SELECT qryALL_PO.POLN, Sum(qryALL_PO.[LINE QUANTITY]) AS SUM_QTY_PO
FROM qryALL_PO
GROUP BY qryALL_PO.POLN;
You can still count and group dates, as long as you have a way to determine the part of the date you are looking for.
In Access you can use year and month for example to get the year and month part of the date:
select year(mydate)
, month(mydate)
, count(*)
from tableX
group
by year(mydate)
, month(mydate)
You can format it 'YYYY-MM' , and then use '>' for 'after' clause