好的......我现在花了2天的时间写作,重写,重新思考,我有点被困在这里。
我会尝试在"简单模式"
中进行解释我有一张表格,其中包含从仓库中挑选的订单的历史记录。
有时你会发现"存储"他们"不应该" be"存储"我的想法是建立一个显示用户的报告:
哪个店内订单和拣货订单最后一次访问此位置。不仅是订单号,还包括这些订单上的所有文章。
因此,通过向变量添加地址,我希望结果显示“此”订单号最后访问“此”位置,这些文章是“那个”订单。
与其他订单类型相同。
示例(简化)
Type OrderNo ArtNo DateTime Address
A 111 10 2014-09-01 11:08:09.000 05-009-09-09
E 222 20 2014-09-01 11:08:09.000 02-021-02-01
A 333 30 2014-10-04 11:08:09.000 01-001-01-01
E 222 60 2014-08-01 11:08:09.000 02-056-02-07
A 444 90 2014-07-01 11:08:09.000 01-022-01-01
A 444 40 2014-07-01 11:08:09.000 01-056-02-01
E 777 70 2014-10-02 11:08:09.000 04-102-12-04
E 777 80 2014-10-02 11:08:09.000 01-001-01-01
A 555 50 2014-10-01 11:08:09.000 02-099-07-01
E 777 10 2014-10-02 11:08:09.000 03-011-03-02
订单号333是订单类型A的最后订单访问地址01-001-01-01,订单号777是订单类型E的最后订单号访问地址01-001-01-01。
我的SQL应该给我结果:
A 333 30 2014-10-01 11:08:09.000 01-001-01-01
E 777 70 2014-10-01 11:08:09.000 04-102-12-04
E 777 80 2014-10-01 11:08:09.000 01-001-01-01
E 777 10 2014-10-01 11:08:09.000 03-011-03-02
基于我的变量01-001-01-01
重要的栏目是:
Ordertype, orderNumber, articleNo, dateTime and adress
Adress很棘手,因为它存储在4个不同的列中:
shelfName, tray, tpos, depth
我用
解决了这个问题Right(replicate('0', 2) + CONVERT(varchar(2), shelfname), 2) + '-' +
Right(replicate('0', 3) + CONVERT(varchar(3), tray), 3) + '-' +
Right(replicate('0', 2) + CONVERT(varchar(2), tpos), 2) + '-' +
Right(replicate('0', 2) + CONVERT(varchar(2), depth), 2)
结果是
02-035-09-02
所以我的想法是
declare @var1 varchar(12)
set @var1='02-035-09-02'
能够找到与该位置有关的最后店内和店外订单。
select
.... ...
where
@var1 = Right(replicate('0', 2) + CONVERT(varchar(2), shelfname), 2) + '-' +
Right(replicate('0', 3) + CONVERT(varchar(3), tray), 3) + '-' +
Right(replicate('0', 2) + CONVERT(varchar(2), tpos), 2) + '-' +
Right(replicate('0',2)+CONVERT(varchar(2),depth), 2)
但我只想要1个ordertype E和1个ordertype A(有点不同)(下面的样本)
示例数据
OrderType (varchar) | orderNumber (varchar) | ArticleNo (varchar) | dateTime | adress
E 4967 14608 2014-10-08 11:08:09.000 04-030-12-04
E 4967 14608 2014-10-08 11:08:03.000 04-030-10-02
E 4967 14608 2014-10-08 11:07:04.000 02-020-13-04
E 4967 14608 2014-10-08 11:06:48.000 03-016-08-02
E 4967 14608 2014-10-08 11:06:38.000 04-015-05-03
E 4967 14608 2014-10-08 11:06:28.000 03-016-04-04
E 4967 14608 2014-10-08 11:06:13.000 02-020-12-03
A 1942513 10134 2014-10-08 10:54:53.000 05-086-01-03
A 1942517 10848 2014-10-08 10:53:52.000 05-091-05-03
A 1942517 10103 2014-10-08 10:52:10.000 05-015-14-01
A 1942517 10103 2014-10-08 10:51:49.000 05-015-10-02
A 1942514 10995 2014-10-08 10:50:23.000 05-027-11-01
A 1942517 10142 2014-10-08 10:48:54.000 05-040-17-03
A 1942519 40180 2014-10-08 10:48:33.000 01-052-14-03
A 1942519 40180 2014-10-08 10:48:10.000 01-033-18-02
A 1942517 10142 2014-10-08 10:47:44.000 05-041-04-02
A 1942513 10570 2014-10-08 10:46:47.000 05-052-05-03
A 1942513 50019 2014-10-08 10:45:01.000 05-065-02-04
A 1942513 11726 2014-10-08 10:41:22.000 01-040-16-02
A 1942517 10848 2014-10-08 10:39:07.000 04-011-05-03
A 1942514 10995 2014-10-08 10:38:15.000 01-057-22-03
E 4964 30989 2014-10-08 10:32:11.000 01-058-03-02
E 4964 17301 2014-10-08 10:31:58.000 01-054-32-02
E 4964 3970 2014-10-08 10:31:48.000 01-054-14-02
E 4964 17823 2014-10-08 10:31:14.000 01-020-31-03
A 1942421 14782 2014-10-08 10:22:06.000 02-033-08-01
A 1942282 14992 2014-10-08 10:20:50.000 02-032-13-01
A 1942421 14585 2014-10-08 10:19:51.000 02-029-01-03
我的示例中的所有列名都是为了便于理解而编写的。
这并不容易解释......如果我应该提供任何测试数据,请告诉我
答案 0 :(得分:1)
每个类型的地址的最后一个条目。我是否正确地理解了它?
select
T.*
from sampleTable T
inner join (
select
OrderType,
MAX([dateTime]) LastEntry
from sampleTable
where
adress = Right(replicate('0', 2) + CONVERT(varchar(2), shelfname), 2) + '-' +
Right(replicate('0', 3) + CONVERT(varchar(3), tray), 3) + '-' +
Right(replicate('0', 2) + CONVERT(varchar(2), tpos), 2) + '-' +
Right(replicate('0',2)+CONVERT(varchar(2),depth), 2)
group by
OrderType
) Filter ON
Filter.LastEntry = T.[Datetime] AND
Filter.OrderType = T.OrderType
答案 1 :(得分:0)
感谢mxix的一些意见。我现在已经解决了。
declare @adress varchar(12)
set @adress='01-070-13-01'
select articleNo, OrderNo, type, dateTime, Right(replicate('0', 2) + CONVERT(varchar(2), genr), 2) + '-' +Right(replicate('0', 3) + CONVERT(varchar(3), tsnr), 3) + '-' +Right(replicate('0', 2) + CONVERT(varchar(2), lkvon), 2) + '-' +Right(replicate('0',2)+CONVERT(varchar(2),favon), 2)
from myTable where OrderNo in
(select
T.orderNo
from myTable T
inner join (
select
typ,
MAX([dateTime]) LastEntry
from myTable
where @adress = Right(replicate('0', 2) + CONVERT(varchar(2), shelfname), 2) + '-' +Right(replicate('0', 3) + CONVERT(varchar(3), tray), 3) + '-' +Right(replicate('0', 2) + CONVERT(varchar(2), tpos), 2) + '-' +Right(replicate('0',2)+CONVERT(varchar(2),depth), 2)
group by type
) Filter ON
Filter.LastEntry = T.[dateTime] AND
Filter.type = T.type
)
order by dateTime