SQL Server选择具有多个行计数的语句

时间:2013-11-20 20:39:48

标签: sql sql-server sql-server-2008 tsql

有没有办法从一个表调用列并按另一个表中的日期排序,并为三个数字5,6,99执行此操作?我有一个可能澄清的例子。这是一个有趣的程序,我们目前在工厂停留。

---这是它的例子表----

表a

6                queued  (there are ten of these with ID's of course)
5                queued  (there are 8 with id's )
99               queued  (there are 7 with id's)

* 表B的例子 ** *

表b

(确切的数量不匹配,这是一个例子。表A中有ID;表B的ID匹配彼此相等。)

id   11/1/2013  10:00:01
id   11/1/2013  10:00:02
id   11/1/2013  10:00:03
id   11/1/2013  10:00:04
id   11/1/2013  10:00:05
id   11/1/2013  10:00:06
id   11/1/2013  10:00:07
id   11/1/2013  10:00:08
id   11/1/2013  10:00:09
id   11/1/2013  10:00:10
id   11/1/2013  10:00:11
id   11/1/2013  10:00:12
id   11/1/2013  10:00:13
id   11/1/2013  10:00:14
id   11/1/2013  10:00:15
id   11/1/2013  10:00:16
id   11/1/2013  10:00:17
id   11/1/2013  10:00:18
id   11/1/2013  10:00:19
id   11/1/2013  10:00:20

* 应该是什么形式注意我正在显示日期以显示订单,但实际上时间不会出现在幕后* * ** * *

表格

 6 id  11/1/2013  10:00:05
 6 id  11/1/2013  10:00:06
 6 id  11/1/2013  10:00:07
 6 id  11/1/2013  10:00:08
 6 id  11/1/2013  10:00:09
 6 id  11/1/2013  10:00:10
 5 id  11/1/2013  10:00:02
 5 id  11/1/2013  10:00:03
 5 id  11/1/2013  10:00:04
 5 id  11/1/2013  10:00:11
 5 id  11/1/2013  10:00:13
 5 id  11/1/2013  10:00:17
99 id  11/1/2013  10:00:01
99 id  11/1/2013  10:00:12
99 id  11/1/2013  10:00:14
99 id  11/1/2013  10:00:15
99 id  11/1/2013  10:00:16
99 id  11/1/2013  10:00:18

VB不需要更改。它适用于其他程序,并且运行得很好。我只需要在表格中出现正确的顺序。有谁知道怎么做?

SQL:

   ALTER PROC [Controls].[spBatch]
       @Status varchar output
      ,@RoasterNumber int output
      ,@RoastProfile int output
      ,@ChargeWeight float output
      ,@SuperSackID int output
   AS
   SET NOCOUNT ON

   DECLARE @RETURN int


select  Top 6 *  
from wip.RoasterQueueSequence RQS 
inner join wip.schedule s on s.fk_roasterqueuesequenceid =           RQS.PK_RoasterQueueSequenceID 
where 
   rqs.Status = 'Queued' 
   and rqs.PK_RoasterQueueSequenceID = s.FK_RoasterQueueSequenceID 
   and rqs.RoasterNumber = 6
   order by 
   s.publishedstartdatetime asc

  SELECT @RETURN = @@rowcount 
  FROM WIP.RoasterQueueSequence 
  WHERE  Roasternumber = 6

  select Top 6 *  
  from wip.RoasterQueueSequence RQS 
  inner join wip.schedule s on s.fk_roasterqueuesequenceid = RQS.PK_RoasterQueueSequenceID 
  where 
  rqs.Status = 'Queued'   
  and rqs.PK_RoasterQueueSequenceID = s.FK_RoasterQueueSequenceID 
  and rqs.RoasterNumber = 5
  order by 
  s.publishedstartdatetime asc

  SELECT @RETURN2 = @@rowcount 
  FROM WIP.RoasterQueueSequence 
  WHERE  Roasternumber = 5

   select  Top 6 *  
   from wip.RoasterQueueSequence RQS 
   inner join wip.schedule s on s.fk_roasterqueuesequenceid =     RQS.PK_RoasterQueueSequenceID 
  where 
   rqs.Status = 'Queued' 
   and rqs.PK_RoasterQueueSequenceID = s.FK_RoasterQueueSequenceID 
   and rqs.RoasterNumber = 99
   order by 
   s.publishedstartdatetime asc

  SELECT @RETURN3 = @@rowcount 
  FROM WIP.RoasterQueueSequence 
  WHERE  Roasternumber = 99


 set @TotalRows = @RETURN + @RETURN2 + @RETURN3

 --set @TotalRows = @@rowcount
 return @TotalRows

0 个答案:

没有答案