• (disco) in reply to PleegWat
    PleegWat:
    little-travelled forest path accessible by car.

    Moorland is effective as well.

  • (disco)

    This looks like auto-generated SQL to me, probably by some reporting tool, or a JPA tool like Hibernate generating queries from the JPA Criteria API. Or a reporting tool using the JPA Criteria API - it's turtles all the way down!

    And somebody thought it's a useful query and copied it as-is, adding a few extra tweaks. That would explain the non-existing formatting..

  • (disco) in reply to beeporama
    beeporama:
    (2) I think there is a bug. I suspect this:
      AND  ( 
                          item.country IS NOT NULL 
                 AND      c.country_cd = item.country 
                 OR       item.country IS NULL 
                 AND      c.country_cd = e.country) 
    

    Should actually be:

    AND ( 
                      (item.country IS NOT NULL AND c.country_cd = item.country)
             OR (item.country IS NULL AND c.country_cd = e.country)  )
    

    I mean, I can't be sure, by my intuition is that the latter is the intended logic. If this is stored server-side in a stored procedure or something, a couple of inline comments could be used to make it clear.

    The precedence of OR makes those equivalent, but of course, it's handy for a maintainer to actually be able to see that.

  • (disco)

    Except for the poor formatting and the old style joins, this is reminiscent of some of the queries I have written. I've come up with some real winners. This is a minor demo.

        SELECT       
           I_FED_TAX                                         -- TIN/SS#       
        ,  N_VEND                                            -- VENDOR NAME       
        ,  Q_INV                                             -- # INVOICES 1/1/10 - PRE
        , M.I_CLIE                                           -- HOSP#       
        , M.I_VEND                                           -- VENDOR#       
        , I_PARN_VEND I_VEND_MAST                            -- MASTER VENDOR#       
        , P_DISC_1                                           -- DISC% 1       
        , P_DISC_2                                           -- DISC% 2       
        , P_DISC_3                                           -- DISC% 3       
        , Q_DISC_DAY_1                                       -- DISC DAYS 1       
        , Q_DISC_DAY_2                                       -- DISC DAYS 2       
        , Q_DISC_DAY_3                                       -- DISC DAYS 3       
        , F_SEPR_CHECK AS F_CHECK_SEPR                       -- SEP CHECKS       
        , F_1099                                             -- SUBJ TO 1099       
        , I_CUST                                             -- VENDOR CUST#       
        , M_MINM_ORDR                                        -- SUGGESTED MIN ORDER -$ 
        , M_MINM_ORDR AS M_MINM_ORDR_FREE                    -- MIN ORDER -FREEFRGHT  
        , Q_LEAD_WEEK                                        -- LEAD TIME - WEEKS      
        , COALESCE(F_810_EDI,'N') AS F_810_EDI               -- EDI?       
        , COALESCE(F_820_EDI,'N') AS F_820_EDI       
        , COALESCE(F_850_EDI,'N') AS F_850_EDI       
        , COALESCE(F_855_EDI,'N') AS F_855_EDI       
        , COALESCE(F_864_EDI,'N') AS F_864_EDI       
        , COALESCE(                                          -- 1099 NAME 1       
          ( SELECT N_VEND       
            FROM PRODSCH.VMA97500 C1       
            WHERE C1.I_CLIE = M.I_CLIE       
              AND C1.I_VEND = N.I_VEND       
              AND C1.I_YEAR_1099 = (SELECT MAX(I_YEAR_1099)       
                                    FROM PRODSCH.VMA97500 C1A       
                                    WHERE C1A.I_CLIE = C1.I_CLIE       
                                      AND C1A.I_VEND = C1.I_VEND       
                                      AND C1A.F_STATS_RECR = 'A')       
              AND F_STATS_RECR = 'A'       
          ), '') AS N_VEND_1099_1       
        , ' ' N_VEND_1099_2                                  -- 1099 NAME 2       
        , ' ' N_VEND_1099_3                                  -- 1099 NAME 3       
        , COALESCE(       
          ( SELECT A_STR                                     -- 1099 ADDRESS 1       
            FROM PRODSCH.VMA97030 C2       
            WHERE C2.I_CLIE = M.I_CLIE       
              AND C2.I_VEND = M.I_VEND       
              AND I_CATG_ADDR = '  ' AND F_STATS_RECR = 'A'       
              AND C2.I_TYPE_ADDR = M.I_TYPE_ADDR_1099),'') AS A_STRE_1099       
        , COALESCE(       
          ( SELECT A_EXTR                                    -- 1099 ADDRESS 2       
            FROM PRODSCH.VMA97030 C3       
            WHERE C3.I_CLIE = M.I_CLIE       
              AND C3.I_VEND = M.I_VEND       
              AND I_CATG_ADDR = '  ' AND F_STATS_RECR = 'A'       
              AND C3.I_TYPE_ADDR = M.I_TYPE_ADDR_1099),'') AS A_EXTR_1099       
        , COALESCE(       
          ( SELECT STRIP(A_CITY) || ' ' || STRIP(A_STAT)     -- 1099 CITY/ST       
            FROM PRODSCH.VMA97030 C4       
            WHERE C4.I_CLIE = M.I_CLIE       
              AND C4.I_VEND = M.I_VEND       
              AND I_CATG_ADDR = '  ' AND F_STATS_RECR = 'A'       
              AND C4.I_TYPE_ADDR = M.I_TYPE_ADDR_1099),'') AS A_CITY_ST_1099       
        , COALESCE(       
          ( SELECT A_ZIP                                     -- 1099 ZIP       
            FROM PRODSCH.VMA97030 C5       
            WHERE C5.I_CLIE = M.I_CLIE       
              AND C5.I_VEND = M.I_VEND       
              AND I_CATG_ADDR = '  ' AND F_STATS_RECR = 'A'       
              AND C5.I_TYPE_ADDR = M.I_TYPE_ADDR_1099),'') AS A_ZIP_1099       
        , COALESCE(                                          -- EPAYABLES       
          (  SELECT MAX(F_ACTV_PAYM)       
             FROM PRODSCH.VEP97110 C6       
             WHERE C6.I_CLIE = M.I_CLIE       
               AND C6.I_SOUR_EPAY = '01'       
               AND C6.I_VEND_SOUR = M.I_VEND       
               AND C6.F_ACTV_PAYM = 'Y'       
               AND C6.F_STATS_RECR = 'A'       
          ) , 'N') AS F_EPAY                                 -- EPAYABLES       
        , F_GLN_ACCP                                         -- GLN       
        FROM       
        (  SELECT       
              I_FED_TAX                                         -- TIN/SS#       
            , N_VEND                                            -- VENDOR NAME       
            , ( SELECT SUBSTR(       
                   MAX( CASE WHEN I_TYPE_ADDR = '01' THEN '901'       
                             WHEN I_TYPE_ADDR = '02' THEN '802'       
                             ELSE                         '4' || I_TYPE_ADDR       
                        END ),2,2) AS I_TYPE_ADDR_1099       
                FROM PRODSCH.VMA97030 B1       
                WHERE A.I_CLIE = B1.I_CLIE       
                  AND A.I_VEND = B1.I_VEND       
                  AND I_TYPE_ADDR IN ('01','02')       
                  AND I_CATG_ADDR = '  '       
                  AND F_STATS_RECR = 'A'       
              ) AS I_TYPE_ADDR_1099       
            , ( SELECT COUNT(*)                                 -- # INVOICES 1/1/10 - 
                FROM PRODSCH.VMA97100 B2       
                WHERE B2.I_CLIE = A.I_CLIE       
                  AND B2.I_VEND = A.I_VEND       
                  AND B2.F_STATS_RECR = 'A'       
                  AND B2.I_STAT_INV IN ('P','E','M','A')       
                  AND B2.D_INV >= '01/01/2010'       
              ) Q_INV       
            , I_CLIE                                            -- HOSP#       
            , I_VEND                                            -- VENDOR#       
            , (  SELECT SUBSTR(I_PARN,1,8)                      -- MASTER VENDOR#      
                 FROM PRODSCH.VGL97190 B3       
                 WHERE B3.I_CLIE    = '20'       
                   AND B3.I_CORP    = A.I_CLIE       
                   AND B3.I_SUBS    = 'MA'       
                   AND B3.I_ASSC    = '95'       
                   AND B3.I_DETL    = A.I_VEND       
              ) AS I_PARN_VEND       
            , ( SELECT P_CASH_DISC FROM PROD.CET874.MACASHDS B  -- DISC% 1       
                WHERE B.I_CASH_DISC = A.I_CASH_DISC) P_DISC_1       
            , 0                                      P_DISC_2   -- DISC% 2       
            , 0                                      P_DISC_3   -- DISC% 3       
            , ( SELECT Q_DAY_DISC FROM PROD.CET874.MACASHDS B   -- DISC DAYS 1       
                WHERE B.I_CASH_DISC = A.I_CASH_DISC) Q_DISC_DAY_1       
            , 0                                   Q_DISC_DAY_2  -- DISC DAYS 2       
            , 0                                   Q_DISC_DAY_3  -- DISC DAYS 3       
            ,F_SEPR_CHECK                                       -- SEP CHECKS       
            ,F_1099                                             -- SUBJ TO 1099       
            ,I_CUST                                             -- VENDOR CUST#       
            ,M_MINM_ORDR                                        -- SUGGESTED MIN ORDER 
                                                                -- MIN ORDER -FREE FRGH
            ,0                                    Q_LEAD_WEEK   -- LEAD TIME - WEEKS   
            , F_GLN_ACCP       
           FROM PRODSCH.VMA97010 A       
           WHERE A.I_CLIE IN ('12','16','20','76')       
             AND A.I_VEND >= '00000000'       
        ) AS M       
        LEFT JOIN       
        (   SELECT       
               I_CLIE       
             , I_VEND       
             , MAX(CASE WHEN I_TRAN = '810' THEN 'Y' ELSE NULL END) AS F_810_EDI       
             , MAX(CASE WHEN I_TRAN = '820' OR I_TRAN = '820S'       
                                            THEN 'Y' ELSE NULL END) AS F_820_EDI       
             , MAX(CASE WHEN I_TRAN = '850' THEN 'Y' ELSE NULL END) AS F_850_EDI       
             , MAX(CASE WHEN I_TRAN = '855' THEN 'Y' ELSE NULL END) AS F_855_EDI       
             , MAX(CASE WHEN I_TRAN = '864' THEN 'Y' ELSE NULL END) AS F_864_EDI       
            FROM PRODSCH.VMA97550       
            WHERE F_STATS_RECR = 'A'       
              AND I_MODE = 'P'       
              AND F_ENBL = 'Y'       
            GROUP BY I_CLIE, I_VEND       
        ) AS N       
        ON M.I_CLIE = N.I_CLIE AND M.I_VEND = N.I_VEND       
        WITH UR       
    
  • (disco) in reply to g0uy0u

    If you have to maintain queries that long, you'll learn that ANSI join is godsend. It enables you to conveniently, cleanly do multiline comment to the query, then add them back one by one by just moving the starting comment mark. This makes the style extremely efficient for debugging why a query is duplicate rows instead of using the evil "distinct" statement to mask the problem.

  • (disco) in reply to boomzilla
    boomzilla:
    That said, I've used non-ANSI joins in some situations where they actually made things easier, but those have been very rare. Separating your joins from their conditions is just asking for trouble, IME.

    The only time I find them useful is when I'm intentionally performing a Cartesian product of several tables. Even then, CROSS JOIN is cleaner.

  • (disco) in reply to Vault_Dweller

    SELECT "posts"."id" AS t0_r0, "posts"."user_id" AS t0_r1, "posts"."topic_id" AS t0_r2, "posts"."post_number" AS t0_r3, "posts"."raw" AS t0_r4, "posts"."cooked" AS t0_r5, "posts"."created_at" AS t0_r6, "posts"."updated_at" AS t0_r7, "posts"."reply_to_post_number" AS t0_r8, "posts"."reply_count" AS t0_r9, "posts"."quote_count" AS t0_r10, "posts"."deleted_at" AS t0_r11, "posts"."off_topic_count" AS t0_r12, "posts"."like_count" AS t0_r13, "posts"."incoming_link_count" AS t0_r14, "posts"."bookmark_count" AS t0_r15, "posts"."avg_time" AS t0_r16, "posts"."score" AS t0_r17, "posts"."reads" AS t0_r18, "posts"."post_type" AS t0_r19, "posts"."vote_count" AS t0_r20, "posts"."sort_order" AS t0_r21, "posts"."last_editor_id" AS t0_r22, "posts"."hidden" AS t0_r23, "posts"."hidden_reason_id" AS t0_r24, "posts"."notify_moderators_count" AS t0_r25, "posts"."spam_count" AS t0_r26, "posts"."illegal_count" AS t0_r27, "posts"."inappropriate_count" AS t0_r28, "posts"."last_version_at" AS t0_r29, "posts"."user_deleted" AS t0_r30, "posts"."reply_to_user_id" AS t0_r31, "posts"."percent_rank" AS t0_r32, "posts"."notify_user_count" AS t0_r33, "posts"."like_score" AS t0_r34, "posts"."deleted_by_id" AS t0_r35, "posts"."edit_reason" AS t0_r36, "posts"."word_count" AS t0_r37, "posts"."version" AS t0_r38, "posts"."cook_method" AS t0_r39, "posts"."wiki" AS t0_r40, "posts"."baked_at" AS t0_r41, "posts"."baked_version" AS t0_r42, "post_search_data"."post_id" AS t1_r0, "post_search_data"."search_data" AS t1_r1, "topics"."id" AS t2_r0, "topics"."title" AS t2_r1, "topics"."last_posted_at" AS t2_r2, "topics"."created_at" AS t2_r3, "topics"."updated_at" AS t2_r4, "topics"."views" AS t2_r5, "topics"."posts_count" AS t2_r6, "topics"."user_id" AS t2_r7, "topics"."last_post_user_id" AS t2_r8, "topics"."reply_count" AS t2_r9, "topics"."featured_user1_id" AS t2_r10, "topics"."featured_user2_id" AS t2_r11, "topics"."featured_user3_id" AS t2_r12, "topics"."avg_time" AS t2_r13, "topics"."deleted_at" AS t2_r14, "topics"."highest_post_number" AS t2_r15, "topics"."image_url" AS t2_r16, "topics"."off_topic_count" AS t2_r17, "topics"."like_count" AS t2_r18, "topics"."incoming_link_count" AS t2_r19, "topics"."bookmark_count" AS t2_r20, "topics"."star_count" AS t2_r21, "topics"."category_id" AS t2_r22, "topics"."visible" AS t2_r23, "topics"."moderator_posts_count" AS t2_r24, "topics"."closed" AS t2_r25, "topics"."archived" AS t2_r26, "topics"."bumped_at" AS t2_r27, "topics"."has_summary" AS t2_r28, "topics"."vote_count" AS t2_r29, "topics"."archetype" AS t2_r30, "topics"."featured_user4_id" AS t2_r31, "topics"."notify_moderators_count" AS t2_r32, "topics"."spam_count" AS t2_r33, "topics"."illegal_count" AS t2_r34, "topics"."inappropriate_count" AS t2_r35, "topics"."pinned_at" AS t2_r36, "topics"."score" AS t2_r37, "topics"."percent_rank" AS t2_r38, "topics"."notify_user_count" AS t2_r39, "topics"."subtype" AS t2_r40, "topics"."slug" AS t2_r41, "topics"."auto_close_at" AS t2_r42, "topics"."auto_close_user_id" AS t2_r43, "topics"."auto_close_started_at" AS t2_r44, "topics"."deleted_by_id" AS t2_r45, "topics"."participant_count" AS t2_r46, "topics"."word_count" AS t2_r47, "topics"."excerpt" AS t2_r48, "topics"."pinned_globally" AS t2_r49, "categories"."id" AS t3_r0, "categories"."name" AS t3_r1, "categories"."color" AS t3_r2, "categories"."topic_id" AS t3_r3, "categories"."topic_count" AS t3_r4, "categories"."created_at" AS t3_r5, "categories"."updated_at" AS t3_r6, "categories"."user_id" AS t3_r7, "categories"."topics_year" AS t3_r8, "categories"."topics_month" AS t3_r9, "categories"."topics_week" AS t3_r10, "categories"."slug" AS t3_r11, "categories"."description" AS t3_r12, "categories"."text_color" AS t3_r13, "categories"."read_restricted" AS t3_r14, "categories"."auto_close_hours" AS t3_r15, "categories"."post_count" AS t3_r16, "categories"."latest_post_id" AS t3_r17, "categories"."latest_topic_id" AS t3_r18, "categories"."position" AS t3_r19, "categories"."parent_category_id" AS t3_r20, "categories"."posts_year" AS t3_r21, "categories"."posts_month" AS t3_r22, "categories"."posts_week" AS t3_r23, "categories"."email_in" AS t3_r24, "categories"."email_in_allow_strangers" AS t3_r25, "categories"."topics_day" AS t3_r26, "categories"."posts_day" AS t3_r27 FROM "posts" LEFT OUTER JOIN "post_search_data" ON "post_search_data"."post_id" = "posts"."id" LEFT OUTER JOIN "topics" ON "topics"."id" = "posts"."topic_id" AND ("topics"."deleted_at" IS NULL) LEFT OUTER JOIN "categories" ON "categories"."id" = "topics"."category_id" WHERE ("posts"."deleted_at" IS NULL) AND (post_search_data.search_data @@ TO_TSQUERY('german', 'liebe:*')) AND "topics"."deleted_at" IS NULL AND (topics.visible) AND (topics.archetype <> 'private_message') AND ((categories.id IS NULL) OR (NOT categories.read_restricted) OR (categories.id IN (270))) AND (posts.post_number = 1 OR posts.topic_id = 7173154) ORDER BY CASE WHEN topics.id = 7173154 THEN 0 ELSE 1 END, CASE WHEN topics.id = 7173154 THEN posts.post_number ELSE 999999 END, TS_RANK_CD(TO_TSVECTOR('german', topics.title), TO_TSQUERY('german', 'liebe:*')) DESC, TS_RANK_CD(post_search_data.search_data, TO_TSQUERY('german', 'liebe:*')) DESC, topics.bumped_at DESC LIMIT 6; SELECT u.id AS user_id, topics.id AS topic_id, topics.created_at, highest_post_number, last_read_post_number, c.name AS category_name, tu.notification_level FROM users u INNER JOIN user_stats AS us ON us.user_id = u.id FULL OUTER JOIN topics ON 1=1 LEFT JOIN topic_users tu ON tu.topic_id = topics.id AND tu.user_id = u.id LEFT JOIN categories c ON c.id = topics.category_id WHERE u.id IN (-2) AND topics.archetype IN ('regular') AND (("topics"."deleted_at" IS NULL AND tu.last_read_post_number < topics.highest_post_number AND COALESCE(tu.notification_level, 1) >= 2) OR ("topics"."deleted_at" IS NULL AND topics.created_at >= GREATEST(CASE WHEN COALESCE(u.new_topic_duration_minutes, 2880) = -1 THEN u.created_at WHEN COALESCE(u.new_topic_duration_minutes, 2880) = -2 THEN COALESCE(u.previous_visit_at,u.created_at) ELSE ('2014-06-16 13:52:48.210894'::timestamp - INTERVAL '1 MINUTE' * COALESCE(u.new_topic_duration_minutes, 2880)) END, us.new_since) AND tu.last_read_post_number IS NULL AND COALESCE(tu.notification_level, 2) >= 2)) AND (topics.visible OR u.admin OR u.moderator) AND topics.deleted_at IS NULL AND ( category_id IS NULL OR NOT c.read_restricted OR category_id IN ( SELECT c2.id FROM categories c2 JOIN category_groups cg ON cg.category_id = c2.id JOIN group_users gu ON gu.user_id = u.id AND cg.group_id = gu.group_id WHERE c2.read_restricted )) AND NOT EXISTS( SELECT 1 FROM category_users cu WHERE cu.user_id = u.id AND cu.category_id = topics.category_id AND cu.notification_level = 0) ORDER BY topics.bumped_at DESC LIMIT 500

  • (disco) in reply to riking

    I hope you feel a little dirty after writing that.

  • (disco) in reply to FrostCat

    https://www.riking.org/webm/bad-horse-letter.webm

  • (disco) in reply to riking

    Yes, I was sorry about what happened to Jeph Jaques' web site, too.

  • (disco) in reply to FrostCat
    FrostCat:
    I hope you feel a little dirty after writing that.

    Just add some NOLOCK hints and you'll feel dirty reading it, too!

  • (disco) in reply to FrostCat
    FrostCat:
    Yes, I was sorry about what happened to Jeph Jaques' web site, too.

    wait.... what did i miss about QC?

  • (disco) in reply to ExaDBA

    Or alternatively Oracle fixed a bug in 11.2 but the application depends on the wrong result being returned, which can result in an interesting conversation with developers/project managers.

  • (disco) in reply to accalia
    accalia:
    wait.... what did i miss about QC?

    Not QC, walmart.horse, which he gave up over the weekend, because Walmart is tone-deaf and threatened him with legal action, as if anyone would think walmart.horse would actually lead to trademark dilution.

  • (disco) in reply to FrostCat

    it used the name walmart. so they sued.

    lawyers are not known for having a sense of humor.

    also...

    /me sighs in relief

    i would have missed QC

  • (disco) in reply to accalia
    accalia:
    it used the name walmart. so they sued.

    Walmart should know better than to think they have to do that.

    Popehat had an excellent post some months back on why that behavior is not necessary to prevent trademark dilution.

  • (disco) in reply to FrostCat
    FrostCat:
    Popehat had an excellent post some months back on why that behavior is not necessary to prevent trademark dilution.

    it may not be necessary to prevent trademark dilution but it can be billed to the client as such and corporate lawyers love generating billable hours. particularly for doing things that are not technically necessary bu that the client won't question.

  • (disco) in reply to FrostCat
    FrostCat:
    Not QC, walmart.horse, which he gave up over the weekend, because Walmart is tone-deaf and threatened him with legal action, as if anyone would think walmart.horse would actually lead to trademark dilution.

    What was he doing with it?

  • (disco) in reply to accalia
    accalia:
    it may not be necessary to prevent trademark dilution but it can be billed to the client as such and corporate lawyers love generating billable hours. particularly for doing things that are not technically necessary bu that the client won't question.

    Yes, but consider the source. Walmart's not generally known for frivolously spending money.

  • (disco) in reply to FrostCat
    FrostCat:
    Walmart's not generally known for frivolously spending money.

    famous they are for that. this is true. but what about their legal council?

  • (disco) in reply to boomzilla
    boomzilla:
    What was he doing with it?

    According to this article, it had a picture of a horse in front of a Walmart and played some silly music.

    You can see the original contents of the site (minus the music) here.

  • (disco) in reply to Choonster
    Choonster:
    According to this article, it had a picture of a horse in front of a Walmart and played some silly music.
    I can see why Walmart felt so threatened :unamused:
  • (disco)

    someone should teach them about create temporary table

  • (disco) in reply to beeporama

    foxyshadis, you are correct, I parsed it wrong. But guys like me misreading it are a good case to use parentheses for clarity. :smile:

  • (disco) in reply to boomzilla
    boomzilla:
    What was he doing with it?

    He put a picture of a walmart storefront with a horse sort of photobombing it.

    Since Walmart doesn't sell horses or horse-related stuff, it seems odd how anyone could be confused that that was a Walmart website.

  • (disco) in reply to Guvante
    Guvante:
    Also using ANSI joins does not preclude you from excluding things in the WHERE clause. For instance I usually try to do primary keys in the ON and anything more complex in the WHERE to avoid that issue to great effect.

    Who even believes that? (That using ANSI joins prevents you from excluding things in the WHERE, that is.)

    chris_c:
    Or alternatively Oracle fixed a bug in 11.2 but the application depends on the wrong result being returned, which can result in an interesting conversation with developers/project managers.
    That's just...*deluded*.
  • (disco) in reply to accalia
    accalia:
    what about their legal council?

    Unless there's a whole panel of lawyers sitting there working on this, you'd be talking about a legal counsel.

  • (disco)

    Late to the party, but I feel it should be pointed out that -- strictly speaking -- comma joins are ANSI joins. They're just ANSI-89 joins. ANSI-92 introduced the improved JOIN syntax.

  • (disco) in reply to chris_c
    chris_c:
    Or alternatively Oracle fixed a bug in 11.2 but the application depends on the wrong result being returned, which can result in an interesting conversation with developers/project managers.

    Ah, someone with real-world experience, I see.

Leave a comment on “Sea of SQL”

Log In or post as a guest

Replying to comment #:

« Return to Article