替代SQL中的子查询吗?

时间:2019-02-27 12:23:42

标签: sql sql-server-2008 subquery

我有应返回少量列结果的查询。还有一个要求,我必须对其中一列应用其他过滤器。我发现使用子查询(在主选择内选择)的解决方案是示例:

SELECT 
   recid,
   price,
   receive_date,
   (SELECT deduction FROM Cost WHERE recid = c.recid AND status = 'Y') AS deduction
FROM Cost c
   INNER JOIN cost_types ct
      c.recid = ct.recid
WHERE cost_year = '2018'

上面的查询必须从Cost表中提取与cost_type表记录匹配且cost_year2018的所有记录。最重要的是,我必须提交deduction,其中statusY。我想知道我可以使用什么其他技术进行此查询?我知道UNION / UNION ALL,但这是更多代码,似乎很多余。如果有人有建议或更好的方法来解决这种情况,请告诉我。另外,我想知道这是否更适合存储过程?会推荐吗?

2 个答案:

答案 0 :(得分:1)

JOIN是正常的方法,如下所示:

SELECT c.recid, c.price, c.receive_date,
       cd.deduction
FROM cost c INNER JOIN
     cost_types ct
     ON c.recid = ct.recid LEFT JOIN
     cost cd
     ON cd.recid = c.recid AND cd.status = 'Y'
WHERE c.cost_year = 2018;

这是在猜测列的来源。如果这些列来自不同的表,请调整限定词。

您可能还可以使用窗口功能:

SELECT c.recid, c.price, c.receive_date,
       SUM(CASE WHEN c.status = 'Y' THEN c.deduction END) OVER (PARTITION BY c.recid) as deduction
FROM cost c INNER JOIN
     cost_types ct
     ON c.recid = ct.recid 
WHERE c.cost_year = 2018;

答案 1 :(得分:0)

为什么不简单使用 python3 -m virtualenv env Using base prefix '/usr' New python executable in /home/myusername/code/ML/env/bin/python3 Not overwriting existing python script /home/myusername/code/ML/env/bin/python (you must use /home/myusername/code/ML/env/bin/python3) Installing setuptools, pip, wheel... Complete output from command /home/myusername/code/ML/env/bin/python3 - setuptools pip wheel: Looking in indexes: https://pypi.org/simple, https://firstname.lastname:jFrog12345@companydev.jfrog.io/companydev/api/pypi/pypi/simple Looking in links: /home/myusername/.local/lib/python3.6/site-packages, /home/myusername/.local/lib/python3.6/site-packages/virtualenv_support, /home/myusername/.local/lib/python3.6/site-packages/virtualenv_support Collecting setuptools User for companydev.jfrog.io: Exception: Traceback (most recent call last): File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv_support/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/basecommand.py", line 228, in main status = self.run(options, args) File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv_support/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/commands/install.py", line 291, in run resolver.resolve(requirement_set) File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv_support/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/resolve.py", line 103, in resolve self._resolve_one(requirement_set, req) File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv_support/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/resolve.py", line 257, in _resolve_one abstract_dist = self._get_abstract_dist_for(req_to_install) File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv_support/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/resolve.py", line 210, in _get_abstract_dist_for self.require_hashes File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv_support/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/operations/prepare.py", line 245, in prepare_linked_requirement req.populate_link(finder, upgrade_allowed, require_hashes) File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv_support/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/req/req_install.py", line 307, in populate_link self.link = finder.find_requirement(self, upgrade) File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv_support/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/index.py", line 484, in find_requirement all_candidates = self.find_all_candidates(req.name) File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv_support/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/index.py", line 442, in find_all_candidates for page in self._get_pages(url_locations, project_name): File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv_support/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/index.py", line 587, in _get_pages page = self._get_page(location) File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv_support/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/index.py", line 705, in _get_page return HTMLPage.get_page(link, session=self.session) File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv_support/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/index.py", line 814, in get_page "Cache-Control": "max-age=600", File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv_support/pip-10.0.1-py2.py3-none-any.whl/pip/_vendor/requests/sessions.py", line 521, in get return self.request('GET', url, **kwargs) File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv_support/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/download.py", line 397, in request return super(PipSession, self).request(method, url, *args, **kwargs) File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv_support/pip-10.0.1-py2.py3-none-any.whl/pip/_vendor/requests/sessions.py", line 508, in request resp = self.send(prep, **send_kwargs) File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv_support/pip-10.0.1-py2.py3-none-any.whl/pip/_vendor/requests/sessions.py", line 625, in send r = dispatch_hook('response', hooks, r, **kwargs) File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv_support/pip-10.0.1-py2.py3-none-any.whl/pip/_vendor/requests/hooks.py", line 31, in dispatch_hook _hook_data = hook(hook_data, **kwargs) File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv_support/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/download.py", line 189, in handle_401 username = six.moves.input("User for %s: " % parsed.netloc) EOFError: EOF when reading a line ---------------------------------------- ...Installing setuptools, pip, wheel...done. Traceback (most recent call last): File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "/usr/lib/python3.6/runpy.py", line 85, in _run_code exec(code, run_globals) File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv.py", line 2343, in <module> main() File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv.py", line 712, in main symlink=options.symlink) File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv.py", line 947, in create_environment download=download, File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv.py", line 904, in install_wheel call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT) File "/home/myusername/.local/lib/python3.6/site-packages/virtualenv.py", line 796, in call_subprocess % (cmd_desc, proc.returncode)) OSError: Command /home/myusername/code/ML/env/bin/python3 - setuptools pip wheel failed with error code 2 ?:

CASE

编辑::我怀疑您想要一些东西:

SELECT recid, price, receive_date,
      (CASE WHEN status = 'Y' THEN deduction END) AS deduction
FROM Cost c INNER JOIN 
     cost_types ct
     ON c.recid = ct.recid 
WHERE cost_year =  2018;