查询SQL时的括号

时间:2018-07-04 19:00:51

标签: sql sql-server

我不得不问,因为我找不到解决我问题的方法。 我有查询示例(SQL Server):

=ERROR REPORT==== 4-Jul-2018::20:50:47 ===^M
Error in process <0.31747.0> on node 'animate_online_real_actors@online.anim.int' with exit value:^M
{function_clause,^M
    [{lists,nth,[1,[]],[{file,"lists.erl"},{line,170}]},^M
     {hookup,suggested_system_actor_data_for_hookup,4,^M
         [{file,^M
              "/projects/animate_online_real_actors/animate_online_real_actors/_build/prod/lib/animate_online_real_actors/src/hookup.erl"},^M
          {line,194}]},^M
     {hookup,send_hookup_with_verification_of_suggested_system_actor,12,^M
         [{file,^M
              "/projects/animate_online_real_actors/animate_online_real_actors/_build/prod/lib/animate_online_real_actors/src/hookup.erl"},^M
          {line,81}]},^M
     {hookup,process_hookup,3,^M
         [{file,^M
              "/projects/animate_online_real_actors/animate_online_real_actors/_build/prod/lib/animate_online_real_actors/src/hookup.erl"},^M
...etc

我什至尝试过(但没有意义)

SELECT ad_nr FROM ad LEFT JOIN ku ON ku.NR = ad.NR
LEFT JOIN kd ON kd.NR_1 = adNR 
LEFT JOIN bb ON bb.NR = kd.NR_1 
WHERE 
    KD_DATE >= 170701 AND 
AND (bb.BB = 20 OR bb.BB = 21 OR bb.BB = 22 OR bb.BB =23)    /* <- THIS IS THE PROBLEM!! */
( 
  ( 
    ku_6 + ku_5 + ku_4 + ku_3 
  ) BETWEEN 0 AND 6300
 ) GROUP BY AD_KUNDNR  

我尝试了两次,但是不记得了...

问题是我所做的,查询未考虑方括号(SELECT ad_nr FROM ad LEFT JOIN ku ON ku.NR = ad.NR LEFT JOIN kd ON kd.NR_1 = adNR LEFT JOIN bb ON bb.NR = kd.NR_1 WHERE KD_DATE >= 170701 AND AND bb.BB = 20 OR bb.BB = 21 OR bb.BB = 22 OR bb.BB =23 /* <- WITHOUT brackets */ ( ( ku_6 + ku_5 + ku_4 + ku_3 ) BETWEEN 0 AND 6300 ) GROUP BY AD_KUNDNR )中的值-好像根本就没有

我想要这样的东西:

BB = ...

我希望你能理解...

1 个答案:

答案 0 :(得分:1)

主要问题是多余的AND或缺少ANDs:

<View>
        <StatusBar backgroundColor="rgba(1.0, 0, 0, 0.2)" translucent />
        <SearchBar
          ref='searchBar'
          placeholder='Find me'
          barStyle="black"
          showsCancelButtonWhileEditing={false}
        />
        <MapView
          provider={ PROVIDER_GOOGLE }
          style={ styles.container }
          initialRegion={{
            latitude: 32.815013,
            longitude: -117.273404,
            latitudeDelta: 0.0922,
            longitudeDelta: 0.0421,
          }}
        />
      </View>

另一个问题是您正在选择的分组依据列表中没有应用聚合功能的列。您可以通过多种方式进行纠正

...
WHERE 
    KD_DATE >= 170701 AND /* another AND follows on next line! */
AND (bb.BB = 20 OR bb.BB = 21 OR bb.BB = 22 OR bb.BB =23)    /* Missing AND here */
( 
  ( 
    ku_6 + ku_5 + ku_4 + ku_3 
  ) BETWEEN 0 AND 6300
 ) 

或按与选择列表相同的列进行分组

SELECT MAX(ad_nr)
...
GROUP BY AD_KUNDNR  

这等效于

SELECT ad_nr
...
GROUP BY ad_nr

更正的查询

SELECT DISTINCT ad_nr
/* without GROUP BY */

我还使用SELECT DISTINCT ad_nr FROM ad LEFT JOIN ku ON ku.NR = ad.NR LEFT JOIN kd ON kd.NR_1 = ad.NR LEFT JOIN bb ON bb.NR = kd.NR_1 WHERE KD_DATE >= 170701 AND bb.BB IN (20, 21, 22, 23) AND (ku_6 + ku_5 + ku_4 + ku_3) BETWEEN 0 AND 6300 代替了较长的OR链。