我对SQL Server
完全不熟悉,任何人都可以指导我查询它对我有用。
我需要像这个例子中那样生成排名:
Report No Rank
----------------------------
18 3229 1
89-0119-87 2
180261292 3
180261292 3
18 3107 4
18A 7 A06 5
T18002415 6
T18002415 6
T18002415 6
0000000018-519 7
0000000018-519 7
0000000018-519 7
0000000018-519 7
0000000018-655 8
000000018-1002 9
000000018-1002 9
000000018-1002 9
000000018-5712 10
如果我使用
生成排名DENSE_RANK() OVER(ORDER BY ReportNo DESC)
然后我没有得到所需的输出。你能指导我吗?如何在上面的例子中生成排名?
答案 0 :(得分:3)
我无法看到如何订购报告编号的逻辑。按照自定义顺序,创建包含所需订单anotherTable
,Report No
的{{1}}(临时表或表变量)。
Rank
然后
Report No Rank
18 3229 1
89-0119-87 2
180261292 3
18 3107 4
18A 7 A06 5
T18002415 6
0000000018-519 7
0000000018-655 8
000000018-1002 9
000000018-5712 10
答案 1 :(得分:0)
试试这个:
@RequestMapping(value="/upload",method=RequestMethod.POST)
public ModelAndView upload(@RequestParam("file") CommonsMultipartFile file,HttpSession session) throws IOException
{
String path = session.getServletContext().getRealPath("/");
String filename=file.getOriginalFilename();
ImagePOJO pojo = new ImagePOJO();
byte barr[]=file.getBytes();
pojo.setPath(path);
pojo.setFilename(filename);
//String q = pojo.setPath(path)+"/"+pojo.setFilename(filename);
String w = pojo.getPath()+""+pojo.getFilename();
//System.out.println(Arrays.toString(barr));
System.out.println(path+" "+filename);
System.out.println(w);
BufferedOutputStream bout;
try {
bout = new BufferedOutputStream(new FileOutputStream(path+"/"+filename));
Object o = bout;
bout.write(barr);
bout.flush();
bout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}[enter image description here][1]
<强>输出:强>
DECLARE @Tab TABLE(Report_No VARCHAR(100))
INSERT INTO @Tab VALUES('18 3229')
INSERT INTO @Tab VALUES('89-0119-87')
INSERT INTO @Tab VALUES('180261292')
INSERT INTO @Tab VALUES('180261292')
INSERT INTO @Tab VALUES('18 3107')
INSERT INTO @Tab VALUES('18A 7 A06')
INSERT INTO @Tab VALUES('T18002415')
INSERT INTO @Tab VALUES('T18002415')
INSERT INTO @Tab VALUES('T18002415')
INSERT INTO @Tab VALUES('0000000018-519')
INSERT INTO @Tab VALUES('0000000018-519')
INSERT INTO @Tab VALUES('0000000018-519')
INSERT INTO @Tab VALUES('0000000018-519')
INSERT INTO @Tab VALUES('0000000018-655')
INSERT INTO @Tab VALUES('000000018-1002')
INSERT INTO @Tab VALUES('000000018-1002')
INSERT INTO @Tab VALUES('000000018-1002')
INSERT INTO @Tab VALUES('000000018-5712')
SELECT *
,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) RN
INTO #temp
FROM @Tab T1
SELECT T1.Report_No
,SUM(CASE WHEN T1.Report_No = T2.Report_No THEN 0 ELSE 1 END) OVER(ORDER BY T1.rn)[Rank]
FROM #temp T1
LEFT JOIN #temp T2 ON T1.rn = T2.rn+1
DROP TABLE #temp
答案 2 :(得分:0)
使用分组依据:
WITH data as (
SELECT '18 3229' col1 UNION ALL
SELECT '89-0119-87' UNION ALL
SELECT '180261292' UNION ALL
SELECT '180261292' UNION ALL
SELECT '18 3107' UNION ALL
SELECT '18A 7 A06' UNION ALL
SELECT 'T18002415' UNION ALL
SELECT 'T18002415' UNION ALL
SELECT 'T18002415' UNION ALL
SELECT '0000000018-519' UNION ALL
SELECT '0000000018-519' UNION ALL
SELECT '0000000018-519' UNION ALL
SELECT '0000000018-519' UNION ALL
SELECT '0000000018-655' UNION ALL
SELECT '000000018-1002' UNION ALL
SELECT '000000018-1002' UNION ALL
SELECT '000000018-1002' UNION ALL
SELECT '000000018-5712'),
data_with_rownumber as (
SELECT *, row_number() over (order by (SELECT 1)) rownum from data
), grouped as (
SELECT col1, min(rownum) rank from data_with_rownumber group by col1)
select d.col1, DENSE_RANK() over (order by rank) from data d inner join grouped gr on d.col1 = gr.col1