如何在sql中获得最少的行

时间:2013-02-05 23:16:28

标签: sql sql-server

您好,我想从Sql Server Table中检索具有最小值但不应更改行中数据顺序的行,即ID旁边的数据不应该被错误我试图得到结果但是我对得到的东西不满意。我知道我可以使用where子句来阻止数据放错位置但是因为我想把它与java向量联系起来我不能使用那个选项。  这是我目前的编码rm_id是我的ID,但我到处都是最小值0

SELECT   b.rm_id AS 'REMITTANCE ID',
     min(b.[1]) AS 'Re. 1',
     min(b.[2]) AS 'Rs. 2',
     min(b.[5]) AS 'Rs. 5',
     min(b.[10]) AS 'Rs. 10',
     min(b.[20]) AS 'Rs. 20',
     min(b.[50]) AS 'Rs. 50',
     min(b.[100]) AS 'Rs. 100',
     min(b.[500]) AS 'Rs. 500',
     min(b.[1000]) AS 'Rs. 1000',
     min(b.[tp]) AS 'T.P',
     min(b.[tv]) AS 'T.V'
FROM     trans_det AS b
GROUP BY b.rm_id WITH ROLLUP
ORDER BY rm_id DESC;

1 个答案:

答案 0 :(得分:0)

我怀疑你可能会想要这个:

select top 1 td.* from trans_det order by b.[1] union all
select top 1 td.* from trans_det order by b.[2] union all
select top 1 td.* from trans_det order by b.[5] union all
. . .
select top 1 td.* from trans_det order by b.tv

这将返回每个值的最小行数。