我们公司用条码来跟踪产品的生产状态,有时候产品会返修,这样在某一个工序的扫描点这个条码会被扫描两次,问题是,如何按月为单位,从数据库中获取经过某点次数为2次或者更多次的条码?
举个例子:
条码ABAB123456789
扫描点    扫描时间               扫描人:     判断结果
A                2012-10-01           X                合格
B                2012-10-02           X                 合格
C                 2012-10-02          X                 返修
B                2012-10-02           X                 合格
谢谢各位

解决方案 »

  1.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2012-11-05 10:46:06
    -- Version:
    --      Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    -- Jul  9 2008 14:43:34 
    -- Copyright (c) 1988-2008 Microsoft Corporation
    -- Developer Edition on Windows NT 6.1 <X86> (Build 7600: )
    --
    ----------------------------------------------------------------
    --> 测试数据:[test]
    if object_id('[test]') is not null 
    drop table [test]
    go 
    create table [test](
    [扫描点] varchar(1),
    [扫描时间] datetime,
    [扫描人] varchar(1),
    [判断结果] varchar(4)
    )
    insert [test]
    select 'A','2012-10-01','X','合格' union all
    select 'B','2012-10-02','X','合格' union all
    select 'C','2012-10-02','X','返修' union all
    select 'B','2012-10-02','X','合格'
    --------------开始查询--------------------------with
    t as(
    select
    [扫描点],
    [扫描时间],
    [扫描人],
    [判断结果],
    [扫描次数]=COUNT(1)over(partition by month([扫描时间]),[扫描点])
    from 
    test
    )
    select 
    *
    from 
    t
    where 
    [扫描次数]=2
    ----------------结果----------------------------
    /* 
    扫描点  扫描时间                    扫描人  判断结果 扫描次数
    ---- ----------------------- ---- ---- -----------
    B    2012-10-02 00:00:00.000 X    合格   2
    B    2012-10-02 00:00:00.000 X    合格   2(2 行受影响)*/
      

  2.   

    select 条码 from tb group by 条码 having count(*)>1
      

  3.   


    group by [扫描点],[扫描时间]
      

  4.   

    请问:
     [扫描次数]=COUNT(1)over(partition by month([扫描时间]),[扫描点]) 
    这行代码什么意思? 谢谢!
      

  5.   

    Quote: 引用 5 楼 TravyLee 的回复:
    那count(1) 这个函数起什么作用呀?
      

  6.   

    with
    t as(
    select
        barlog_fromsite,
        barlog_part,
        barlog_result,
        barlog_date,
        [扫描次数]=COUNT(1)over(partition by month(2012/10),'油漆上线')
    from 
        barlog_det
    )
    select 
        *
    from 
        t
    where 
        [扫描次数]=2 我这样用有错吗? 获取的结果有点问题
      

  7.   

    with
    t as(
    select
        barlog_fromsite,
        barlog_part,
        barlog_result,
        barlog_date,
        [扫描次数]=COUNT(1)over(partition by month(日期字段的名称啊),'油漆上线')
    from 
        barlog_det
    )
    select 
        *
    from 
        t
    where 
        [扫描次数]=2 and month(日期字段)=你需要的月份
      

  8.   


    TravyLee 版主,如图是我数据库的表结构  barlog_code是条码号, barlog_fromsite是扫描点名称,barlog_date,是扫描日期,barlog_part是条码所对应的零件号.
    由于一个月我们这边可能会处理几十万条条码记录,故我想用sql查询语句查出哪些条码经过特点扫描点的次数.
    举个例子:
    我想查在2012-10-01 到2012-10-31这个区间经过'油漆上线扫描'这个点次数超过2次的所有条码,并按照他们所对应的barlog_part汇总,请问这个该如何处理?
    谢谢!
      

  9.   

    select count(barlog_code),barlog_code,count(barlog_fromsite),barlog_fromsite from barlog_det
    where barlog_date>='2012-10-01' and barlog_date<='2012-10-31' and barlog_fromsite='油漆上线扫描'
    group by barlog_code,barlog_fromsite不知道行不行 
    我菜鸟一个
      

  10.   

    declare mycursor cursor for select info_Time from stu_Info
    declare @ddate datetime,@oldate datetime
    open mycursor
    fetch next from mycursor into @oldate
    while @@fetch_status=0
    begin
    if convert(char(10),@oldate,120) ='2012-11-22'
    begin
    set @ddate=dateadd(dd,1,@oldate)
    update a set a.info_Time=@ddate from stu_Info a where a.info_Time=@oldate
    fetch next from mycursor into @ddate
    end
    else 
    fetch next from mycursor into @ddate
    end
    fetch next from mycursor into @ddate
    close mycursor
    deallocate mycursor
      

  11.   

    select * from (select 扫描点,COUNT(扫描点)as 扫描次数,convert(varchar(7),扫描时间,121)as 扫描时间 from test  group by DATEPART(month,扫描时间),扫描点,convert(varchar(7),扫描时间,121)) tt where 扫描次数>=2 order by 扫描点