在选择时添加列以显示派生的行号

时间:2016-02-20 07:31:27

标签: sql oracle

如果我从表中选择数据,如何在表格中添加一列新列?

例如,我从表中获取数据

select cont_no,Cont_name from emp;

cont_no     cont_name
abc         abc1
cde         cde1
cde         cde2
cde         cde3
efg         efg1
efg         efg2
hij         hij

但我想添加一个像这样的虚拟列:

added_column   cont_no     cont_name
     1         abc         abc1
     2         cde         cde1
               cde         cde2
               cde         cde3
     3         efg         efg1
               efg         efg2
     4         hij         hij

1 个答案:

答案 0 :(得分:2)

import bs4
import requests
import re
import os

# don't want to download while experimenting
tmp_file = 'data.html'

if True and os.path.exists('data.html'):   # switch True to false in production
    with open(tmp_file) as fp:
        data = fp.read()
else:
    res = requests.get('http://www.trademe.co.nz/browse/categorylistings.aspx?mcatpath=sports%2fcycling%2fmountain-bikes%2ffull-suspension&page=2&sort_order=default&rptpath=5-380-50-7145-')
    res.raise_for_status()
    data = res.text
    with open(tmp_file, 'w') as fp:
        fp.write(data)

soup = bs4.BeautifulSoup(data, 'html.parser')
# and start experimenting with your regular expressions
regex = re.compile('...........htm')
for links in soup.find_all(regex):
    print (links.get('href'))
# the above doesn't find anything, you need to search the hrefs
print('try again')
for links in soup.find_all(href=regex):
    print (links.get('href'))

<强>输出

WITH emp ( cont_no, cont_name ) AS (
  SELECT 'abc', 'abc1' FROM DUAL UNION ALL
  SELECT 'cde', 'cde1' FROM DUAL UNION ALL
  SELECT 'cde', 'cde2' FROM DUAL UNION ALL
  SELECT 'cde', 'cde3' FROM DUAL UNION ALL
  SELECT 'efg', 'efg1' FROM DUAL UNION ALL
  SELECT 'efg', 'efg2' FROM DUAL UNION ALL
  SELECT 'hij', 'hij' FROM DUAL
)
SELECT CASE cont_no
         WHEN LAG( cont_no ) OVER ( ORDER BY cont_no, cont_name )
         THEN NULL
         ELSE DENSE_RANK() OVER ( ORDER BY cont_no )
         END as added_column,
       cont_no,
       cont_name
FROM   emp;