简体   繁体   English

将所有行从表中的列返回到sql中的视图

[英]Return all rows from a column in a table to a view in sql

Could someone please help me with this question: So I have a table called Orders which has ordernumber, requireddate, shippeddate columns. 有人可以帮我解决这个问题:所以我有一个名为Orders的表,该表具有ordernumber,requireddate,shippeddate列。 I need to find what the most number of days between shippeddate and requireddate is and I need to list the ordernumber. 我需要查找在发运日期和必需日期之间最多的天数,并且我需要列出订单号。

I created a view which I want it to include ordernumber and datediff(requireddate, shippeddate). 我创建了一个视图,希望它包含订单号和datediff(必需日期,发货日期)。 This is my code for it 这是我的代码

create view Numberdays (ordernumber, number_of_days)
as select ordernumber, datediff(requireddate, shippeddate) 
from CM_orders
group by ordernumber

But my view showed only 30 rows of the table even though I have about 300 rows in the Orders table. 但是,即使我在Orders表中大约有300行,我的视图也只显示了该表的30行。 Could someone please explain what I got wrong and how to do the view which will display all the rows from the table, please? 有人可以解释一下我出了什么问题以及如何显示将显示表格中所有行的视图吗? Thank you in advance. 先感谢您。

Hoping i understood your problem correctly. 希望我正确理解您的问题。

I have tried to create the similar situation. 我试图造成类似的情况。 Check sqlfiddle link below. 检查下面的sqlfiddle链接。 Removed group by from your create view query 已从创建视图查询中删除分组依据

create view Numberdays (ordernumber, number_of_days)
as select ordernumber, datediff(requireddate, shippeddate) 
from Orders; -- i think no need to group by if there is not any aggregate function

Getting complete order numbers. 获取完整的订单号。

http://sqlfiddle.com/#!9/03d58a/1/0 http://sqlfiddle.com/#!9/03d58a/1/0

What you need to do is first find the max requiredate and the minimum shippeddate and then do the datediff 您需要做的是首先找到最大要求和最小发货日期,然后执行datediff

CREATE VIEW Numberdays (ordernumber, number_of_days)
AS SELECT ordernumber, datediff(rdate,sdate)
FROM(
  SELECT ordernumber, max(requiredate) AS rdate,min(shippeddate) AS sdate
  FROM CM_orders
  GROUP by ordernumber
)

You grouped by the ordernumber. 您按订单号分组。 I am guessing that you only have 30 ordernumbers, therefore only 30 rows returned. 我猜您只有30个订单号,因此仅返回30行。

I would suggest taking apart your query and rebuilding it. 我建议拆开您的查询并重新构建它。 If you simplify it, you will likely find that many more rows are returned, thus eliminating a limiter as a possibility. 如果进行简化,则可能会发现返回了更多的行,因此消除了限制器的可能性。 Then follow the path, slowly adding in pieces to your query that narrow the returned data, until you either get the result you want, or identify the limiting factor. 然后按照以下路径进行操作,慢慢地将片段添加到查询中,以缩小返回数据的范围,直到获得所需的结果或确定限制因素。

Its a matter of trial and error to diagnose the problem when unexpected data is returned, and without the dataset to test against, nearly impossible for us to identify the problem. 返回意外数据并且没有可测试的数据集来诊断问题,这是反复试验的问题,这几乎使我们无法识别问题。 So the best advice I can give you, is to take it back and go simple, then add complexity until you identify the problem. 因此,我能为您提供的最佳建议是将其取回并简化,然后增加复杂性,直到您发现问题为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM