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

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

Related

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: select datetime values prior to that date based on it's value

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)

IN Clause issue

I have one SQL output table like this
ITEM,LOC,PERIOD,QUANTITY
101,US,07/22/2015,500
101,US,07/02/2015,0
102,LON,07/22/2015,0
102,LON,07/02/2015,1000
But I want the output table as follows,
ITEM LOC 07/22/2015 07/02/2015
101 US 500 0
102 LON 0 1000
Please find the code which I have used below,
select * from
(
select item, loc, period, quantity
from example
)
pivot
(
sum (quantity) for period in ('22/JUL/2015','02/JUL/2015'));
If it is for 2 dates, then no issue in mentionning the 'IN' clause
If it is 1000 dates like weekly, monthly and daily. Then how ?
Below command is not working in 'IN' clause.
SELECT PERIOD FROM EXAMPLE WHERE PERIOD < TO_DATE(22/JUL/2015);
Can you please help me to solve this issue ?
Thanks for your time.
Your issue may be incompatible data types. If the period column on your table is DATE type, you are trying to compare strings/VARCHAR with DATE type.
If period column is a DATE try changing your IN to
SELECT period FROM example WHERE period < DATE '2015-07-22';
or
SELECT period FROM example WHERE period < TO_DATE('22/JUL/2015', 'DD/MON/YYYY');

SQL - selecting the first record found before a given date

Say I have a table:
ID DATE
1 2/1/12
2 3/1/12
3 1/1/12
4 4/1/12
How would I go about selecting the first date found when decrementing from a given date.
Example: Find the last entry before 4/1/12, by date. Return entry at SQL ID 2.
If this was added:
ID DATE
5 3/2/12
Than the above example would return the entry at SQL ID 5.
How would I represent what I need in SQL?
Select top 1 ID, DATE
from table
where DATE < '4/1/12'
order by DATE DESC
other ideas: (in addition to Gratzy's)
select the MAX date where the date is less than the target date.
use a LAG function.

How to write a sql query to sum total for each day?

Please help me to write the following sql. I have a table like this, with an amount column and date column
amount date
-----------------------
100 - 2010-02-05
200 - 2010-02-05
50 - 2010-02-06
10 - 2010-02-06
10 2010-02-07
what I want is to write a sql to get total for each day. Ultimately my query should return something like this
amount date
-----------------
300 - 2010-02-05
60 - 2010-02-06
10 - 2010-02-07
Please note that now it has group by dates and amounts are summed for that date.
RESOLVED -
This was my bad, even though I mention the date column as date here, my actual date column in postgres table was 'timestamp'. Once I changed it to get the date only, everything got working
my working sql is like this "select sum(amount), date(date)
from bills
group by date(date)
"
thanks everyone (and I will accept the 1st answer as the correct answer since I can accept only one answer)
thanks again
sameera
Pretty basic group by statement.
SELECT SUM(table.amount), table.date
FROM table
GROUP BY table.date
http://www.w3schools.com/sql/sql_groupby.asp
Look into GROUPING by date, here: http://www.w3schools.com/sql/sql_groupby.asp
and look into SUM(), here: http://www.w3schools.com/sql/sql_func_sum.asp
You will need to use both of them.
Try this:
SELECT SUM(Amount), Date FROM Table
GROUP BY Date
SELECT SUM(amount) as amount_sum, date FROM table GROUP BY date;