如何通过满足HAVING子句的哪一部分来区分查询结果?

时间:2015-12-23 16:15:59

标签: sql sql-server tsql

我有一个GROUP BY和一个HAVING子句的查询。在HAVING子句中,我有一个OR。完整查询可能如下所示:

   (MAX(CASE WHEN city = 'London' AND language = 'English' THEN 1 ELSE 0 END) = 1
AND MAX(CASE WHEN city = 'San Francisco' AND language = 'English' THEN 1 ELSE 0 END) = 1)

我想要的是在SELECT的结果中区分是否满足HAVING子句的第一部分的一些方法:

MAX(CASE WHEN city = 'Madrid' AND language = 'Spanish' THEN 1 ELSE 0 END) = 1

或者OR之后的部分是否满足:

app.php

或两者都满意。

有没有什么好方法可以获得这种结果?

4 个答案:

答案 0 :(得分:2)

也许您只想将列移动到select子句:

SELECT JotID,
       MAX(CASE WHEN city = 'London' AND language = 'English' THEN 1 ELSE 0 END) as EnglishFlag,
       MAX(CASE WHEN city = 'Madrid' AND language = 'Spanish' THEN 1 ELSE 0 END) as SpanishFlag
FROM uv_attributes
WHERE HOC = 1 AND JotTypeID = 5
GROUP BY JotID
HAVING MAX(CASE WHEN city = 'London' AND language = 'English' THEN 1 ELSE 0 END) = 1
    OR MAX(CASE WHEN city = 'Madrid' AND language = 'Spanish' THEN 1 ELSE 0 END) = 1;

我很想在WHERE子句中添加条件。这样可以减少 join之前的数据量,并且不再需要HAVING子句。

SELECT JotID,
       MAX(CASE WHEN city = 'London' AND language = 'English' THEN 1 ELSE 0 END) as EnglishFlag,
       MAX(CASE WHEN city = 'Madrid' AND language = 'Spanish' THEN 1 ELSE 0 END) as SpanishFlag
FROM uv_attributes
WHERE HOC = 1 AND JotTypeID = 5 AND
      ((city = 'London' AND language = 'English') OR
       (city = 'Madrid' AND language = 'Spanish')
      )
GROUP BY JotID;

答案 1 :(得分:0)

将CASE添加到SELECT列表中:

SELECT JotID,
CASE 
 WHEN MAX(CASE WHEN city = 'London' AND language = 'English' THEN 1 ELSE 0 END) = 1
   AND MAX(CASE WHEN city = 'Madrid' AND language = 'Spanish' THEN 1 ELSE 0 END) = 1 THEN 'Both'
 WHEN MAX(CASE WHEN city = 'London' AND language = 'English' THEN 1 ELSE 0 END) = 1 THEN 'London'
 WHEN MAX(CASE WHEN city = 'Madrid' AND language = 'Spanish' THEN 1 ELSE 0 END) = 1 THEN 'Madrid'
 ELSE 'Neither'
END AS LondonMadrid
FROM uv_attributes
...

答案 2 :(得分:0)

Environment:

Request Method: POST
Request URL: http://192.168.0.106:8000/jobs/edit/

Django Version: 1.8.7
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.admindocs',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.messages',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.staticfiles',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'rest_framework',
 'reversion',
 'commons',
 'members',
 'buyers',
 'freelancers',
 'payments',
 'geographic',
 'locations',
 'jobs',
 'technicians',
 'shifts')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/home/matt/.virtualenvs/hmd/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/matt/Projects/hmd/hiremedirect/jobs/views.py" in new
  35.             return HttpResponseRedirect('detail', pk=job.pk)
File "/home/matt/.virtualenvs/hmd/local/lib/python2.7/site-packages/django/http/response.py" in __init__
  457.         super(HttpResponseRedirectBase, self).__init__(*args, **kwargs)
File "/home/matt/.virtualenvs/hmd/local/lib/python2.7/site-packages/django/http/response.py" in __init__
  340.         super(HttpResponse, self).__init__(*args, **kwargs)

Exception Type: TypeError at /jobs/edit/
Exception Value: __init__() got an unexpected keyword argument 'pk'

答案 3 :(得分:0)

也许我错过了一些东西,但我认为这个简单的查询可以完成这项工作:

Select 
    JotID, city+language as 'PlaceLanguage'
From 
    uv_attributes
Where 
    ( HOC = 1  )
    And ( JotTypeID = 5 )
    And (
        ( ( city = 'London' ) And ( language = 'English' ) )
        Or ( ( city = 'Madrid' ) And ( language = 'Spanish' ) )
        Or ( ( city = 'New York' ) And ( language = 'English' ) )             
        Or ( ( city = 'Barcelona' ) And ( language = 'Spanish' ) )
    )

我无法在原始发布的查询中找到用于分组和使用的内容。此外,为了区分结果,您需要第二列。

但正如专员戈登在蝙蝠侠中说的那样......我以前错了#34; ; - )

在用户澄清后编辑。

在您回答我的评论后,我仍然相信这可能会按照您的意愿行事。当然,对于长Ors,对每个案例使用UNION性能更好