虽然有从兄弟的帮助,我弄了两天,但这个sql我还是没法写出,再请兄弟帮助看看,先谢谢

wongwhb 2010-10-23 03:20:18
需求:公司要求根据发料单查投料单的发料情况
具体如下:
表A: finterid fitemid ficmointerid foutqty foutbillno
001 001-1 09001001 20 test-1
001 001-2 09001001 50 test-1
002 001-1 09002001 20 test-2
003 001-2 09003001 50 test-3
表B:
finterid fitemid ficmointerid foutqty
001 001-1 09001001 80
001 001-2 09001001 100
001 001-3 09001001 200
001 001-4 09001001 150
002 001-1 09002001 80
002 001-2 09002001 100
003 001-1 09003001 80
003 001-2 09003001 100
003 001-3 09003001 40
两表合并查询当条件
foutbillno=test-1得出这样的结果:
finterid fitemid ficmointerid foutqty
001 001-1 09001001 20
001 001-2 09001001 50
001 001-3 09001001 200
001 001-4 09001001 150

当查询条件
foutbillno=test-2得出这样的结果:
finterid fitemid ficmointerid foutqty
002 001-1 09002001 20
002 001-2 09002001 100
当查询条件
foutbillno=test-2得出这样的结果:
finterid fitemid ficmointerid foutqty
001 001-1 09003001 80
001 001-2 09001001 50
001 001-3 09001001 40



请问应怎样写sql语句啊?请各位兄弟帮助,谢谢了


--> 测试数据:#ta
if object_id('tempdb.dbo.#ta') is not null drop table #ta
go
create table #ta([finterid] varchar(3),[fitemid] varchar(5),[ficmointerid] varchar(8),[foutqty] int,[fbillno] varchar(20))
insert #ta
select '001','001-1','09001001',20,'test-1' union all
select '001','001-2','09001001',50,'test-1' union all
select '003','001-1','09001001',20,'test-2' union all
select '004','001-2','09001001',50,'test-3'
go

--> 测试数据:#tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb([finterid] varchar(3),[fitemid] varchar(5),[ficmointerid] varchar(8),[foutqty] int)
insert #tb
select '001','001-1','09001001',80 union all
select '001','001-2','09001001',100 union all
select '001','001-3','09001001',200 union all
select '001','001-4','09001001',150 union all
select '002', '001-1','09002001',80 union all
select '002', '001-2','09002001', 100 union all
select '003', '001-1', '09003001', 80 union all
select '003', '001-2' , '09003001', 100 union all
select '003', '001-3' ,'09003001',40
go


...全文
149 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
sport3884 2010-10-25
  • 打赏
  • 举报
回复
确实该楼主结账,呵呵
SQLCenter 2010-10-23
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 wongwhb 的回复:]

明白了,谢谢
[/Quote]

呵呵,不要给我分了,kevin87923 给你解释了半天。

结账吧。
wongwhb 2010-10-23
  • 打赏
  • 举报
回复
明白了,谢谢
kevin87923 2010-10-23
  • 打赏
  • 举报
回复
就看你是用存储过程还是不用,传参都差不多啊
kevin87923 2010-10-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sqlcenter 的回复:]

目测/手写
SQL code
select b.finterid, b.fitemid, b.ficmointerid, isnull(a.foutqty,b.foutqty)foutqty
from #ta a right join #tb b on a.finterid=b.finterid and a.fitemid=b.fitemid
where exists (select 1 f……
[/Quote]

没用存储过程。
string sql=select b.finterid, b.fitemid, b.ficmointerid, isnull(a.foutqty,b.foutqty)foutqty
from #ta a right join #tb b on a.finterid=b.finterid and a.fitemid=b.fitemid
where exists (select 1 from #ta where finterid=b.finterid and fbillno='"+条件+"')
wongwhb 2010-10-23
  • 打赏
  • 举报
回复
那就是用存诸过程了,谢谢
wongwhb 2010-10-23
  • 打赏
  • 举报
回复
不是,是那样的,ficmointerid都是09002001,谢
「已注销」 2010-10-23
  • 打赏
  • 举报
回复
后面的数据是对的,我拿得是顶楼的数据,还郁闷了....
DECLARE @condition AS VARCHAR(30)
SET @condition='TEST-3'

SELECT B.FINTERID,B.FITEMID,B.FICMOINTERID,ISNULL(A.FOUTQTY,B.FOUTQTY)FOUTQTY
FROM #TB B
LEFT JOIN #TA A
ON (A.FINTERID=B.FINTERID AND A.FITEMID=B.FITEMID)
WHERE EXISTS (SELECT NULL FROM #TA WHERE FINTERID=B.FINTERID AND FBILLNO=@condition)

FINTERID FITEMID FICMOINTERID FOUTQTY
-------- ------- ------------ -----------
003 001-1 09003001 80
003 001-2 09003001 50
003 001-3 09003001 40

(3 row(s) affected)
wongwhb 2010-10-23
  • 打赏
  • 举报
回复
直接查询怎样传参数?写成存诸过程?我是菜鸟,谢谢了
「已注销」 2010-10-23
  • 打赏
  • 举报
回复
foutbillno=test-2得出这样的结果:应该为下面的吧?

FINTERID FITEMID FICMOINTERID FOUTQTY
-------- ------- ------------ -----------
003 001-1 09003001 20
003 001-2 09003001 100
003 001-3 09003001 40

(3 row(s) affected)
「已注销」 2010-10-23
  • 打赏
  • 举报
回复
你的数据好像还是有问题,test-2,test-3,看着不对啊
kevin87923 2010-10-23
  • 打赏
  • 举报
回复
难到我理解错了, 他那没错啊, 只要你传个参数啊。
kevin87923 2010-10-23
  • 打赏
  • 举报
回复
可以接结了。 6楼已写出
wongwhb 2010-10-23
  • 打赏
  • 举报
回复
sqlcenter兄:
过滤的条件是动态的,通过过滤条件查询的,这样写死了啊,谢谢
SQLCenter 2010-10-23
  • 打赏
  • 举报
回复
目测/手写
select b.finterid, b.fitemid, b.ficmointerid, isnull(a.foutqty,b.foutqty)foutqty
from #ta a right join #tb b on a.finterid=b.finterid and a.fitemid=b.fitemid
where exists (select 1 from #ta where finterid=b.finterid and foutbillno='test-1')
wongwhb 2010-10-23
  • 打赏
  • 举报
回复
呼叫zsh0809兄,谢
wongwhb 2010-10-23
  • 打赏
  • 举报
回复
重新再发一次:
需求:公司要求根据发料单查投料单的发料情况
具体如下:
表A: finterid fitemid ficmointerid foutqty foutbillno
001 001-1 09001001 20 test-1
001 001-2 09001001 50 test-1
002 001-1 09002001 20 test-2
003 001-2 09003001 50 test-3
表B:
finterid fitemid ficmointerid foutqty
001 001-1 09001001 80
001 001-2 09001001 100
001 001-3 09001001 200
001 001-4 09001001 150
002 001-1 09002001 80
002 001-2 09002001 100
003 001-1 09003001 80
003 001-2 09003001 100
003 001-3 09003001 40
两表合并查询当条件
foutbillno=test-1得出这样的结果:
finterid fitemid ficmointerid foutqty
001 001-1 09001001 20
001 001-2 09001001 50
001 001-3 09001001 200
001 001-4 09001001 150

当查询条件
foutbillno=test-2得出这样的结果:
finterid fitemid ficmointerid foutqty
002 001-1 09002001 20
002 001-2 09002001 100
当查询条件
foutbillno=test-3得出这样的结果:
finterid fitemid ficmointerid foutqty
001 001-1 09003001 80
001 001-2 09001001 50
001 001-3 09001001 40



请问应怎样写sql语句啊?请各位兄弟帮助,谢谢了


--> 测试数据:#ta
if object_id('tempdb.dbo.#ta') is not null drop table #ta
go
create table #ta([finterid] varchar(3),[fitemid] varchar(5),[ficmointerid] varchar(8),[foutqty] int,[fbillno] varchar(20))
insert #ta
select '001','001-1','09001001',20,'test-1' union all
select '001','001-2','09001001',50,'test-1' union all
select '003','001-1','09001001',20,'test-2' union all
select '004','001-2','09001001',50,'test-3'
go

--> 测试数据:#tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb([finterid] varchar(3),[fitemid] varchar(5),[ficmointerid] varchar(8),[foutqty] int)
insert #tb
select '001','001-1','09001001',80 union all
select '001','001-2','09001001',100 union all
select '001','001-3','09001001',200 union all
select '001','001-4','09001001',150 union all
select '002', '001-1','09002001',80 union all
select '002', '001-2','09002001', 100 union all
select '003', '001-1', '09003001', 80 union all
select '003', '001-2' , '09003001', 100 union all
select '003', '001-3' ,'09003001',40
go

谢谢了
claro 2010-10-23
  • 打赏
  • 举报
回复
请zsh0809给你回答。
wongwhb 2010-10-23
  • 打赏
  • 举报
回复
是test-3
不好意思
打错
claro 2010-10-23
  • 打赏
  • 举报
回复
结果陈述有问题。
当查询条件
foutbillno=test-2得出这样的结果: 还是3?
finterid fitemid ficmointerid foutqty
001 001-1 09003001 80
001 001-2 09001001 50
001 001-3 09001001 40
内容概要:本书《Deep Reinforcement Learning with Guaranteed Performance》探讨了基于李雅普诺夫方法的深度强化学习及其在非线性系统最优控制中的应用。书中提出了一种近似最优自适应控制方法,结合泰勒展开、神经网络、估计器设计及滑模控制思想,解决了不同场景下的跟踪控制问题。该方法不仅保证了性能指标的渐近收敛,还确保了跟踪误差的渐近收敛至零。此外,书中还涉及了执行器饱和、冗余解析等问题,并提出了新的冗余解析方法,验证了所提方法的有效性和优越性。 适合人群:研究生及以上学历的研究人员,特别是从事自适应/最优控制、机器人学和动态神经网络领域的学术界和工业界研究人员。 使用场景及目标:①研究非线性系统的最优控制问题,特别是在存在输入约束和系统动力学的情况下;②解决带有参数不确定性的线性和非线性系统的跟踪控制问题;③探索基于李雅普诺夫方法的深度强化学习在非线性系统控制中的应用;④设计和验证针对冗余机械臂的新型冗余解析方法。 其他说明:本书分为七章,每章内容相对独立,便于读者理解。书中不仅提供了理论分析,还通过实际应用(如欠驱动船舶、冗余机械臂)验证了所提方法的有效性。此外,作者鼓励读者通过仿真和实验进一步验证书中提出的理论和技术。

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧