SQL选择只有一个日期的地方

时间:2009-12-18 12:12:08

标签: sql sql-server

目前此查询:

SELECT     InvoiceNumber, CustomerCode, Amount, InvoiceDate
FROM         Invoice

返回:

aaa, 111, 5, 07/12/2009
bbb, 111, 5, 07/11/2009
ccc, 222, 5, 07/12/2009

但是,我想写一下:Where date = '07/12/2009',但我只想在客户只有一张发票的地方返回结果......

因此,客户代码222将被退回,但111不会,因为111有前几个月的发票....

希望你能帮忙! :d

8 个答案:

答案 0 :(得分:1)

DECLARE @DateParam datetime
SELECT @DateParam = Convert(datetime, '07/12/2009', 103)

SELECT InvoiceNumber, CustomerCode, Amount, InvoiceDate
FROM Invoice
INNER JOIN (SELECT CustomerCode
    FROM Invoice
    GROUP BY CustomerCode
    HAVING Count(*) = 1) AS InvoiceCount ON Invoice.CustomerCode = InvoiceCount.CustomerCode
WHERE InvoiceDate = @DateParam

这将返回InvoiceDate等于@DateParam的任何地方,以及整个Invoice表中客户只有一张发票。

答案 1 :(得分:1)

您可以尝试这样的事情

DECLARE @Table TABLE(
        InvoiceNumber VARCHAR(10), 
        CustomerCode VARCHAR(10), 
        Amount FLOAT, 
        InvoiceDate DATETIME
)

INSERT INTO @Table (InvoiceNumber,CustomerCode,Amount,InvoiceDate) SELECT 'aaa', '111', 5, '07/12/2009'
INSERT INTO @Table (InvoiceNumber,CustomerCode,Amount,InvoiceDate) SELECT 'bbb', '111', 5, '07/11/2009'
INSERT INTO @Table (InvoiceNumber,CustomerCode,Amount,InvoiceDate) SELECT 'ccc', '222', 5, '07/12/2009'

SELECT  t.*
FROM    @Table t INNER JOIN
        (
            SELECT  CustomerCode
            FROM    @Table
            GROUP BY CustomerCode
            HAVING COUNT(CustomerCode) = 1
        ) s ON t.CustomerCode = s.CustomerCode
WHERE   t.InvoiceDate = '07/12/2009'

答案 2 :(得分:1)

SELECT
  MAX(InvoiceNumber) AS InvoiceNumber
 ,CustomerCode
 ,MAX(Amount) AS Amount
 ,MAX(InvoiceDate) AS InvoiceDate
FROM Invoice
GROUP BY CustomerCode
HAVING COUNT(*) = 1
  AND MAX(InvoiceDate) = '07/12/2009'

答案 3 :(得分:0)

试试这个:

SELECT      InvoiceNumber, CustomerCode, Amount, InvoiceDate
FROM        Invoice AS Inv
INNER JOIN  
(
    SELECT CustomerCode
    FROM   Invoice
    GROUP BY CustomerCode
    HAVING COUNT(*) = 1
) AS base
ON        Inv.CustomerCode = base.CustomerCode
WHERE  InvoiceDate = '07/12/2009'

答案 4 :(得分:0)

SELECT    i.InvoiceNumber, i.CustomerCode, i.Amount, i.InvoiceDate
FROM      Invoice i
WHERE  i.InvoiceDate = '7/12/2009'
AND    i.InvoiceNumber IN  (
   SELECT MAX(InvoiceNumber)
   FROM   Invoice      
   GROUP BY CustomerCode
   HAVING COUNT(*) = 1
)

答案 5 :(得分:-1)

试试这个萌芽

SELECT     InvoiceNumber, CustomerCode, Amount, InvoiceDate, (SELECT count(*) FROM Invoice b WHERE a.CustomerCode=b.CustomerCode AND InvoiceDate = '07/12/2009') as count
FROM         Invoice a
Where InvoiceDate = '07/12/2009'
HAVING count = 1

答案 6 :(得分:-1)

这应该做:

SELECT
    InvoiceNumber, CustomerCode, Amount, InvoiceDate
FROM
    Invoice
WHERE
    COUNT(id) = 1 AND InvoiceDate = '07/12/2009'
GROUP BY
    InvoiceDate

答案 7 :(得分:-1)

正如所建议的那样,这个似乎在SQLSERVER下不被接受

SELECT * 
FROM Invoice i 
INNER JOIN (
  SELECT CustomerCode, InvoiceDate 
  FROM Invoice 
  WHERE InvoiceDate='07/12/2009' 
  GROUP BY CustomerCode 
  HAVING COUNT(CustomerCode)=1
 ) grp ON (i.CustomerCode=grp.CustomerCode AND i.InvoiceDate=grp.InvoiceDate)

或者发票号是唯一的

SELECT * 
FROM Invoice i 
INNER JOIN (
  SELECT InvoiceNumber 
  FROM Invoice 
  WHERE InvoiceDate='07/12/2009' 
  GROUP BY CustomerCode 
  HAVING COUNT(CustomerCode)=1
 ) grp ON (i.InvoiceNumber =grp.InvoiceNumber)