我正在尝试在SQL Server 2005数据库中找到没有任何订单的客户名称,或者要求报价一年。我正在尝试这个,但它不起作用,因为它只向我显示超过一年的文件,但如果在本年度同一客户的其他文件是礼物,也会显示它们。
SELECT DISTINCT
ANAGRAFICACF.CODCONTO,
DSCCONTO1,
'€ '+LEFT(TOTDOCUMENTO,LEN(TOTDOCUMENTO)-2),
CONVERT(VARCHAR(11),DATADOC,6),
TESTEDOCUMENTI.TIPODOC,
ANAGRAFICACF.PARTITAIVA,
ANAGRAFICACF.CODFISCALE,
ANAGRAFICARISERVATICF.NOTE1,
TESTEDOCUMENTI.DATADOC,
TESTEDOCUMENTI.CODAGENTE1,
ANAGRAFICAAGENTI.DSCAGENTE
FROM
dbo.TESTEDOCUMENTI
INNER JOIN
dbo.ANAGRAFICACF ON CODCLIFOR = CODCONTO
INNER JOIN
dbo.RIGHEDOCUMENTI ON PROGRESSIVO = IDTESTA AND TOTNETTORIGA <> '0'
INNER JOIN
dbo.ANAGRAFICARISERVATICF ON CODCLIFOR = ANAGRAFICARISERVATICF.CODCONTO
LEFT JOIN
dbo.ANAGRAFICAAGENTI ON CODAGENTE = TESTEDOCUMENTI.CODAGENTE1
WHERE
(dbo.TESTEDOCUMENTI.TIPODOC = 'ORDER' OR
dbo.TESTEDOCUMENTI.TIPODOC = 'QUOTATION')
AND (dbo.TESTEDOCUMENTI.DATADOC < DATEADD(DAY, -365, GETDATE())
ORDER BY
TESTEDOCUMENTI.DATADOC DESC
这是
CODCONTO | DSCCONTO1 | TOTDOCUMENTO| DATADOC | TIPODOC| CODAGENTE| DSCAGENTE|
---------+-----------+-------------+---------+--------+----------+----------+
C 10395 |CAIO|€ 1048.22 |03 Aug 15 |ORC |2015-08-03 00:00:00.000 |A 4 |F
C 51282 |SEMPRONIO|€ 2217.41 |03 Aug 15 |PCL |2015-08-03 00:00:00.000 |NULL
C 10120 |PINCO |€ 689.54 |03 Aug 15 |PCL |2015-08-03 00:00:00.000 |A 4 |F
C 746 |TIZIO |€ 205.45 |03 Aug 15 |ORC |2015-08-03 00:00:00.000 |A 4 |F
解决方案(感谢@scsimon) 使用NOT IN:
添加条件 WHERE dbo.TESTEDOCUMENTI.TIPODOC = 'FVC' AND dbo.TESTEDOCUMENTI.DATADOC < DATEADD(DAY, -365, GETDATE())
AND TESTEDOCUMENTI.CODCLIFOR NOT IN (SELECT DISTINCT TESTEDOCUMENTI.CODCLIFOR
FROM dbo.TESTEDOCUMENTI
WHERE dbo.TESTEDOCUMENTI.TIPODOC = 'FVC' AND dbo.TESTEDOCUMENTI.DATADOC > DATEADD(DAY, -365, GETDATE())
)
Hope this will help, if someone else will have my request.
答案 0 :(得分:1)
您想使用NOT EXISTS
。如果ANAGRAFICACF.CODCONTO
是您的CustomerNumber,则以下内容适合您。由于您的列名不是很明显,因此如果我不正确,请将其更改为您需要的名称。但是,NOT EXISTS
的逻辑就是你想要的。
删除:
AND NOT dbo.TESTEDOCUMENTI.DATADOC BETWEEN DATEADD(DAY, -365, GETDATE() ) AND GETDATE())
ADD:
AND ANAGRAFICACF.CODCONTO NOT IN
(SELECT DISTINCTANAGRAFICACF.CODCONTO
FROM dbo.ANAGRAFICACF
INNER JOIN
dbo.TESTEDOCUMENTI ON CODCLIFOR = CODCONTO
WHERE
dbo.TESTEDOCUMENTI.DATADOC > DATEADD(DAY, -365, GETDATE()))
示例数据
点击HERE运行此代码
IF OBJECT_ID('tempdb..#agents') IS NOT NULL DROP TABLE #agents
IF OBJECT_ID('tempdb..#items') IS NOT NULL DROP TABLE #items
create table #agents (AgentID varchar(2), name varchar(50))
insert into #agents values
('A1','Julius Cesar'),
('B2','Albert Einstien'),
('C3','Frank Thomas')
create table #items (AgentID varchar(2), ItemID int, ItemName varchar(50), DT datetime)
insert into #items (AgentID, ItemID, ItemName, DT) values
--Notice Juliues Cesar has items in the current year, AND older than a year
('A1',1,'Apple','7/1/2015'),
('A1',2,'Pear','7/1/2016'),
('A1',3,'Watermelon','12/1/2015'),
('A1',4,'Grape','1/1/2015'),
--Notice Albert Einstien only has items older than a year
('B2',5,'Car','7/1/2015'),
('B2',6,'Truck','5/1/2015'),
('B2',7,'Van','3/1/2015'),
--Notice Frank Thomas only has items older than a year
('C3',8,'Car','7/1/2014'),
('C3',9,'Truck','5/1/2014'),
('C3',10,'Van','3/1/2014')
Select
a.name
,i.*
from
#items i
inner join #agents a on
a.AgentID = i.AgentID
where
--Items that are older than a year
DT < DATEADD(DAY, -365, GETDATE())
AND i.AgentID NOT IN (select distinct AgentID from #items where dt > DATEADD(DAY, -365, GETDATE()))