查询选择最后一列的所有记录

时间:2015-12-12 05:18:10

标签: sql sql-server

我有一个查询,可以针对某个日期范围对特定日期进行多次条形码扫描。这是我的疑问:

SELECT @startdate                                        AS startdate, 
       @enddate                                          AS enddate, 
       spartnumber, 
       Cast(Floor(Cast(dtcreated AS FLOAT)) AS DATETIME) AS dtCreatedDate, 
       Count(spartnumber)                                AS [Number of Scans], 
       Stuff ((SELECT ', ' 
                      + RIGHT(CONVERT(VARCHAR, dtcreated, 100), 7) AS [text()] 
               FROM   ticketsdetails AS subticket 
               WHERE  ( dtcreated BETWEEN Cast(Floor(Cast(ticketsdetails.dtcreated AS FLOAT)) AS DATETIME) 
                                          AND Cast(Floor(Cast(ticketsdetails.dtcreated AS FLOAT)) AS DATETIME) +'23:59:59' ) 
       AND ( dbltotal <= '0' ) 
       AND ( spartnumber NOT LIKE ' ' ) 
       FOR xml path('')), 1, 1, '')                      AS [Times Scanned] 
FROM   ticketsdetails 
WHERE  ( dtcreated BETWEEN @startdate AND @enddate ) 
       AND ( detail_type_id = '11' ) 
       AND ( dblamount < '0' ) 
       AND ( bpaid = 'true' ) 
GROUP  BY spartnumber, 
          Cast(Floor(Cast(dtcreated AS FLOAT)) AS DATETIME) 
HAVING ( Count(*) > 1 ) 
ORDER  BY dtcreateddate 

以下是我的输出。我只能按照扫描的4次来保存图表,但它列出了当天扫描条形码的所有时间,而不是当天扫描的两次,并显示空白的部分数字。

Startdate  enddate spartnumber   dtcreateddate  #of Scans   Times scanned
12/1/15    12/11/15  1833        12/1/15          2     8:47,8:48,8:49 8:51
12/1/15    12/11/15  194         12/2/15          2    8:26,8:28,8:39,8:40
12/1/15    12/11/15              12/2/15          4    8:26,8:28,8:39,8:40

我只想显示扫描特定条形码的时间,而不显示空白条形码。我的输出应该是:

Startdate  enddate spartnumber   dtcreateddate  #of Scans   Times scanned
12/1/15    12/11/15  1833        12/1/15          2         11:45AM, 5:05PM
12/1/15    12/11/15  194         12/2/15          2         9:55AM, 5:50:PM

如何修复&#34;被扫描的时间&#34;仅显示实际扫描每个特定条形码的时间,而不是当天扫描条形码的所有时间,以及如何删除空白条形码(spartnumber)

1 个答案:

答案 0 :(得分:1)

由于您尚未提供输入数据集,因此我会继续处理您的问题陈述并进行适当的更改

  

如何修复&#34;被扫描的时间&#34;仅显示实际扫描每个特定条形码的时间,而不是所有时间都扫描条形码,

--added (sPartNumber=ticketsdetails.sPartNumber) in the inner STUFF's WHERE clause
  

如何删除空白条形码(spartnumber)

--shifted the WHERE CLAUSE from STUFF to outside query

以下是SQL查询。

SELECT  
    @startdate AS startdate, 
    @enddate AS enddate, 
    sPartNumber, 
    CAST(FLOOR(CAST(dtCreated AS FLOAT)) AS DATETIME) AS dtCreatedDate, 
    COUNT(sPartNumber) AS [Number of Scans], 
    STUFF
         ((SELECT     ', ' + RIGHT(CONVERT(VARCHAR, dtCreated, 100), 7) AS [text()]
                FROM    TicketsDetails AS subticket
            WHERE     
                (
                    dtCreated BETWEEN 
                                CAST(FLOOR(CAST(ticketsdetails.dtcreated AS FLOAT)) AS DATETIME) 
                                    AND 
                                CAST(FLOOR(CAST(ticketsdetails.dtcreated AS FLOAT)) AS DATETIME) + '23:59:59'
                )
                AND 
                (dblTotal <= '0') 
                AND 
                (sPartNumber=ticketsdetails.sPartNumber) -- this will get correct times
            FOR XML path('')), 1, 1, '') AS [Times Scanned]
FROM         
    TicketsDetails
WHERE     
    (dtCreated BETWEEN @startdate AND @enddate) 
    AND (Detail_Type_ID = '11') 
    AND (dblAmount < '0') 
    AND (bPaid = 'true')
    AND (sPartNumber NOT LIKE '') -- This will remove the blank partnumbers
GROUP BY sPartNumber, CAST(FLOOR(CAST(dtCreated AS FLOAT)) AS DATETIME)
HAVING      (COUNT(*) > 1)
ORDER BY dtcreateddate