且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何选择SQL Server中记录组的最后一条记录

更新时间:2022-10-28 22:29:40

SQL Server不保证记录存储的任何特定顺序(与FoxPro等平面模型相比)。如果您想要记录条目信息,您必须在表格中考虑它。如果[id]字段是 IDENTITY 字段,那么它总是按顺序排列,最新的是最大的数字。



所以 - 所有你需要做的是适当地将你的查询写入 JOIN 父表和子表,并使用 WHERE 子句(或 HAVING 子句,如果必要的)你要从你想要用来选择最新记录的表中请求 MAX(id)



既然你没有'提供你的实际查询,你的你有什么尝试并没有告诉我们什么,我不能更具体。






Hi everybody!
	I am sure this is pretty simple to do but I can’t do that. My problem :
I Have sql server data base with two table :1-parent_table   2-child_table
Parent table has 5 columns  as follow:    
Id    emp_num   group_num  edit_num  name_emp 
------------------------------------------------      
1     912230      1           1      James              
2     912230      1           2      James              
3     912230      2           1      David               
4     912230      2           2      David               
5     914350      1           1      John  
6     914350      2           1      Mark
.
.       
.         
Id column is primary key and   each  record of name_emp  has exclusive emp_num and exclusive group and may has many edition_num . each record in parent table is exclosive.  
For each records of parent table Exist many records in child table.
Id    emp_num   group_num  edit_num  name_emp  good  payment  date       discount
---------------------------------------------------------------------------------
1     912230     1         1        James     egg    100     20016/01/01  10
2     912230     1         1        James     pear   200     2016/02/01   15
3     914350     2         1        mark      egg    100     2016/02/02   10
.
.
.
How can  I  get  to latest  record of  (emp_num of X group of x edit) in child  table  for each record in parent table not base on date but i maen last record that inserted
For e.g  how can I select  id = 2 in chld table  and how can I   show  Count rows which discount > 0  in lable.Text

Please help!
Thanks



What I have tried:

i try this query:

Use mydatabase
Select emp_num,group_num,edit_num,id=MAX(id)
From tbl_child
Group By emp_num,group_num,edit_num

it make group my detail table(child) successfully and show last record but only display 3 columns when I add other column to SELECT error show:
Column 'tbl_child.sh_ghab' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

SQL Server does not guarantee any particular order for record storage (vs. a flat model such as FoxPro). If you want record entry information you must make allowance for it in the table. If the [id] field is an IDENTITY field then it is always in sequential order, newest with the largest number.

So - all you need to do is write your query to JOIN the parent and child tables appropriately, and the use a WHERE clause (or HAVING clause, if necessary) where you request MAX(id) from the table you wish to use to select the most recent record.

Since you didn't give your actual query, and your "What have you tried" doesn't tell us anything, I cannot be more specific.