Im looking for a query that only returns new items for the month. ie if this item was in last month exclude.
My table looks something like this
Date fruit buyer
2014-04-01 Apple someone1
2014-04-01 Banana someone2
2014-05-01 Apple someone2
2014-05-01 Banana someone2
2014-05-01 Pear someone1
So in the case above I want a query that if run for 2014-05-01 would return only apple and pear because either the buyer or the fruit is distinct. but not banana because that exact record was in last month.
I have tried to use a not exists query on itself ie.
select Date, Fruit, buyer
from table
where date = 2014-05-01 and not exists(
select Date, Fruit, buyer
from table
where date = 2014-04-01)
But this seems to run for a long term and not return the correct results.
I also am aware that one solution would be to normalize buyer out but at the current time is not an option. (is for the long term)
This is on MSSQL.
Thanks in advance
You need to try something like this, my apologies, for previous one, but idea is the same, you can visit this link for more
http://technet.microsoft.com/en-us/library/bb264565(v=sql.90).aspx
SELECT EmployeeID, FirstName, LastName, HireDate, City
FROM Employees
WHERE (HireDate >= '1-june-1992') AND (HireDate <= '15-december-1993')
I solved this by
select Date, Fruit, buyer
from table e
where date = 2014-05-01
and not exists(
select Date, Fruit, buyer
from table d
where date = 2014-04-01
and e.buyer = d.buyer
and e.fuit = d.fruit)
u need to exclude the buyer from your last month
SELECT DATE,FRUIT, BUYER FROM TABLE
WHERE FRUIT IN (
(
Select Fruit
from
(
SELECT FRUIT,BUYER FROM TABLE WHERE DATE = '2014-05-01'
EXCEPT
SELECT FRUIT,BUYER FROM TABLE WHERE DATE = '2014-04-01'
) T1
)
)
AND DATE = '2014-05-01'
I use inner join to create the query try below query
Data:
xDate fruit buyer
----------------------------
2014-04-01 Apple someone1
2014-04-01 Banana someone2
2014-05-01 Apple someone2
2014-05-01 Banana someone2
Query:
select
convert(nvarchar(10),a.xdate,110),a.fruit, a.buyer
from
@temp a
inner join
(
select
b.fruit, b.buyer,b.fruit + ' ' + b.buyer as 'xField', count(*) as total
from @temp b
group by b.fruit, b.buyer, b.fruit + ' ' + b.buyer
having
count(*) = 1
) c on a.fruit = c.fruit and a.buyer = c.buyer
where
a.xDate < dateadd(day,1,'20140630')
--convert(nvarchar(6),a.xdate,112) = '201405'
Result:
fruit buyer
----------------------------
05-01-2014 Apple someone2
05-01-2014 Pear someone1
Note: If you need the Year in the where clause add
DATEPART(Year, xdate) = 2014
DATEPART(Month,xdate) = 5
UPDATED:
if you do not want to use datepart func, you can use below where clause:
(format string 'YYYYMM')
convert(nvarchar(6),a.xdate,112) = '201405'
if we added this 2 records in the data:
2014-03-05 Apple someone2
2014-03-10 Apple someone1
Only pear will appear in the result data
05-01-2014 Pear someone1
2nd UPDATED
To prevent NON-SARGable Query, the where clause is change to:
a.xDate < dateadd(day,1,'20140630')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.