SQL不同的多个查询组合

时间:2016-06-15 12:52:36

标签: mysql

我需要从两个表中获取信息,如下所示,但是通过shift,目前它们基本上运行相同的查询4次,每次轮班运行2次。 对于第一班,他们抓住所有不等于DELC的纸箱编号,并且第二次查询所有纸箱EQUAL到DELC。问题是我们想要不同的纸箱编号,如果纸箱在第一班上处理了一半并在第二班完成,即使我们做的不同,纸箱也会出现两次,因为它对每个查询都是唯一的。

有没有办法一起运行所有4个查询并对整个数据做出明显的分析?

1 Day old 1st shift = DELC
select count(distinct a.Barcode)
from [RL_Ship].[dbo].[mSCAN] as a inner join  
[RL_Ship].dbo].wmsInboundQueue] on
a.barcode = substring(message,26,20)
Where BagToteFlag = 'Y' and direction = 'Send'
and timeStamp >= '2016-06-14 03:00:00' 
and timeStamp < '2016-06-14    15:00:00' 
and substring(message,64,4) = 'DELC'
and SUBSTRING(rawdata,21,20) > '0'

1 Day old 1st shift <> DELC
select count(distinct a.Barcode)
from [RL_Ship].[dbo].[mSCAN] as a inner join    
[RL_Ship].dbo].wmsInboundQueue] on
a.barcode = substring(message,26,20) 
Where a.BagToteFlag = 'Y' and a.direction = 'Send' 
and a.timeStamp >= '2016-06-14 03:00:00'
and a.timeStamp < '2016-06-14 15:00:00'
and substring(message,64,4) <> 'DELC'
and SUBSTRING(rawdata,21,20) > '0'

1 Day old 2nd shift = DELC
select count(distinct a.Barcode)
from [RL_Ship].[dbo].[mSCAN] as a inner join [RL_Ship].[dbo].
[wmsInboundQueue] on
a.barcode = substring(message,26,20) 
Where a.BagToteFlag = 'Y' and a.direction = 'Send' 
and a.timeStamp >= '2016-06-14 15:00:00' 
and a.timeStamp < '2016-06-15 03:00:00'
and substring(message,64,4) = 'DELC'
and SUBSTRING(rawdata,21,20) > '0'

1 Day old 2nd shift <> DELC
select count(distinct a.Barcode)
from [RL_Ship].[dbo].[mSCAN] as a inner join [RL_Ship].[dbo].
[wmsInboundQueue] on
a.barcode = substring(message,26,20)
Where a.BagToteFlag = 'Y' and a.direction = 'Send'
and a.timeStamp >= '2016-06-14 15:00:00' 
and a.timeStamp < '2016-06-15 03:00:00' 
and substring(message,64,4) <> 'DELC'
and SUBSTRING(rawdata,21,20) > '0'

2 个答案:

答案 0 :(得分:1)

如果我理解了你的问题,那么第一个查询我会尝试得到你想要的结果

from django.contrib.auth.hashers import make_password

User.objects.bulk_create([
    User(
        username=name,
        email='sample@email.co.uk',
        password=make_password('Sample&Password!'),
        is_active=True,
    ) for name in f.read().splitlines()
])

答案 1 :(得分:0)

如果我执行联合并取消计数部分,我会得到正确的数字,因为所有查询都会删除重复项,但如果我执行联合并且计数已关闭,则不会删除重复项。 ......

我实际上需要每个查询的总计数,因为它们是不同的班次/时间范围。

做一个计数然后来自的答案是好的,但我无法让它工作,2我认为这只会给我一个倒计数好吗?

我尝试了以下操作,但它给了我以下错误:

第36行,第15行,第1行,第36行 关键字“select”附近的语法不正确。

编辑:好的,我通过在结尾添加'as table1'来实现这一点,但是我怀疑它只给了我一个总数,但没有重复,这很好,但我需要计算4个查询中的每一个。有什么想法?

select count(distinct Barcode) from
(select distinct a.barcode
from [RL_Ship].[dbo].[mSCAN] as a inner join [RL_Ship].[dbo].
[wmsInboundQueue] on
a.barcode = substring(message,26,20)
Where BagToteFlag = 'Y' and direction = 'Send'
and timeStamp >= '2016-06-14 03:00:00' and timeStamp < '2016-06-14
15:00:00' 
and substring(message,64,4) = 'DELC'
and SUBSTRING(rawdata,21,20) > '0'
union
select distinct a.Barcode
from [RL_Ship].[dbo].[mSCAN] as a inner join [RL_Ship].[dbo].
[wmsInboundQueue] on
a.barcode = substring(message,26,20) 
Where a.BagToteFlag = 'Y' and a.direction = 'Send' 
and a.timeStamp >= '2016-06-14 03:00:00'
and a.timeStamp < '2016-06-14 15:00:00'
and substring(message,64,4) <> 'DELC'
and SUBSTRING(rawdata,21,20) > '0'
union
select distinct a.Barcode
from [RL_Ship].[dbo].[mSCAN] as a inner join [RL_Ship].[dbo].
[wmsInboundQueue] on
a.barcode = substring(message,26,20) 
Where a.BagToteFlag = 'Y' and a.direction = 'Send' 
and a.timeStamp >= '2016-06-14 15:00:00' 
and a.timeStamp < '2016-06-15 03:00:00'
and substring(message,64,4) = 'DELC'
and SUBSTRING(rawdata,21,20) > '0'
union
select distinct a.Barcode
from [RL_Ship].[dbo].[mSCAN] as a inner join [RL_Ship].[dbo].
[wmsInboundQueue] on
a.barcode = substring(message,26,20)
Where a.BagToteFlag = 'Y' and a.direction = 'Send'
and a.timeStamp >= '2016-06-14 15:00:00' 
and a.timeStamp < '2016-06-15 03:00:00' 
and substring(message,64,4) <> 'DELC'
and SUBSTRING(rawdata,21,20) > '0')