1. SQL / Говнокод #11405

    −116

    1. 1
    2. 2
    DECLARE @SysDatoFull datetime
    SET @SysDatoFull = CAST(YEAR(getdate()) as varchar) + RIGHT('00'+CAST(MONTH(getdate()) as varchar), 2) + RIGHT('00'+CAST(DAY(getdate()) as varchar), 2)

    SQL, даты... nuff said
    Вспомнился http://govnokod.ru/9211. Может есть где-то специальное учебное заведение, где обучают таким техникам?

    DBdev, 12 Июля 2012

    Комментарии (17)
  2. SQL / Говнокод #11376

    −122

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    exec @res = dbo.p_bis_prepaid_diagnostic @tar_addr, @req_dubug_info output
      
    if (@res = 0)
      set @res = 1
    else
      set @res = 0

    aloha, 07 Июля 2012

    Комментарии (9)
  3. SQL / Говнокод #11244

    −119

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    select a, trim(both ',' from max(SYS_CONNECT_BY_PATH(b, ',')))bb from (
      select count(1) over (partition by a order by /* Важно:*/ a,b /*!*/)rn, a, b from (
    
        select a, b from (
          select 1 a, 'a' b from dual union all
          select 1 a, 'b' b from dual union all
          select 2 a, 'c' b from dual union all
          select 2 a, 'd' b from dual union all
          select 2 a, 'e' b from dual union all
          select 3 a, 'f' b from dual
        ) order by dbms_random.value
    
      )
    )
    start with rn=1 connect by nocycle prior a=a and prior rn+1=rn
    group by a;

    Задача на агрегацию с конкатенацией строк. Можно решить правильно - через свой агрегирующий объект, а можно так, "не по правилам" ))

    RomaShka, 20 Июня 2012

    Комментарии (10)
  4. SQL / Говнокод #11177

    −153

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    SELECT count(*) as cnt FROM cms_books AS b  	
    	JOIN (SELECT DISTINCT r.book_id FROM cms_book2cat AS r
    	JOIN (SELECT 40 AS category_id 
    		UNION SELECT 105 AS category_id UNION SELECT 212 AS category_id UNION SELECT 404 AS category_id UNION SELECT 1 AS category_id UNION SELECT 2 AS category_id UNION SELECT 3 AS category_id UNION SELECT 5 AS category_id UNION SELECT 62 AS category_id UNION SELECT 60 AS category_id UNION SELECT 59 AS category_id UNION SELECT 58 AS category_id UNION SELECT 55 AS category_id UNION SELECT 54 AS category_id UNION SELECT 51 AS category_id UNION SELECT 50 AS category_id UNION SELECT 49 AS category_id UNION SELECT 71 AS category_id UNION SELECT 72 AS category_id UNION SELECT 78 AS category_id UNION SELECT 85 AS category_id UNION SELECT 86 AS category_id UNION SELECT 90 AS category_id UNION SELECT 91 AS category_id UNION SELECT 92 AS category_id UNION SELECT 94 AS category_id UNION SELECT 96 AS category_id UNION SELECT 101 AS category_id UNION SELECT 156 AS category_id UNION SELECT 157 AS category_id UNION SELECT 222 AS category_id UNION SELECT 226 AS category_id UNION SELECT 398 AS category_id UNION SELECT 406 AS category_id UNION SELECT 407 AS category_id UNION SELECT 416 AS category_id UNION SELECT 417 AS category_id UNION SELECT 890 AS category_id UNION SELECT 106 AS category_id UNION SELECT 108 AS category_id UNION SELECT 111 AS category_id UNION SELECT 115 AS category_id UNION SELECT 152 AS category_id UNION SELECT 121 AS category_id UNION SELECT 125 AS category_id UNION SELECT 159 AS category_id UNION SELECT 195 AS category_id UNION SELECT 257 AS category_id UNION SELECT 262 AS category_id UNION SELECT 302 AS category_id UNION SELECT 306 AS category_id UNION SELECT 397 AS category_id UNION SELECT 401 AS category_id UNION SELECT 402 AS category_id UNION SELECT 403 AS category_id UNION SELECT 413 AS category_id UNION SELECT 414 AS category_id UNION SELECT 213 AS category_id UNION SELECT 214 AS category_id UNION SELECT 215 AS category_id UNION SELECT 216 AS category_id UNION SELECT 217 AS category_id UNION SELECT 4 AS category_id UNION SELECT 477 AS category_id UNION SELECT 45 AS category_id UNION SELECT 10 AS category_id UNION SELECT 9 AS category_id UNION SELECT 11 AS category_id UNION SELECT 65 AS category_id UNION SELECT 64 AS category_id UNION SELECT 63 AS category_id UNION SELECT 61 AS category_id UNION SELECT 57 AS category_id UNION SELECT 56 AS category_id UNION SELECT 53 AS category_id UNION SELECT 52 AS category_id UNION SELECT 48 AS category_id UNION SELECT 47 AS category_id UNION SELECT 46 AS category_id UNION SELECT 43 AS category_id UNION SELECT 44 AS category_id UNION SELECT 66 AS category_id UNION SELECT 67 AS category_id UNION SELECT 68 AS category_id UNION SELECT 69 AS category_id UNION SELECT 70 AS category_id UNION SELECT 73 AS category_id UNION SELECT 74 AS category_id UNION SELECT 75 AS category_id UNION SELECT 76 AS category_id UNION SELECT 77 AS category_id UNION SELECT 79 AS category_id UNION SELECT 80 AS category_id UNION SELECT 81 AS category_id UNION SELECT 82 AS category_id UNION SELECT 83 AS category_id UNION SELECT 84 AS category_id UNION SELECT 87 AS category_id UNION SELECT 88 AS category_id UNION SELECT 89 AS category_id UNION SELECT 93 AS category_id UNION SELECT 95 AS category_id UNION SELECT 97 AS category_id UNION SELECT 98 AS category_id UNION SELECT 102 AS category_id UNION SELECT 103 AS category_id UNION SELECT 109 AS category_id UNION SELECT 112 AS category_id UNION SELECT 113 AS category_id UNION SELECT 114 AS category_id UNION SELECT 116 AS category_id UNION SELECT 117 AS category_id UNION SELECT 119 AS category_id UNION SELECT 122 AS category_id UNION SELECT 123 AS category_id UNION SELECT 124 AS category_id UNION SELECT 126 AS category_id UNION SELECT 133 AS category_id UNION SELECT 134 AS category_id UNION SELECT 137 AS category_id UNION SELECT 142 AS category_id UNION SELECT 146 AS category_id UNION SELECT 149 AS category_id UNION SELECT 153 AS category_id UNION SELECT 155 AS category_id UNION SELECT 158 AS category_id UNION SELECT 160 AS category_id UNION SELECT 162 AS category_id UNION SELECT 163 AS category_id UNION SELECT 169 AS category_id UNION SELECT 176 AS category_id UNION SELECT 185 AS category_id UNION SELECT 193 AS category_id UNION SELECT 194 AS category_id UNION SELECT 196 AS category_id UNION SELECT 197 AS category_id UNION SELECT 198 AS category_id UNION SELECT 199 AS category_id UNION SELECT 200 AS category_id UNION SELECT 202 AS category_id UNION SELECT 205 AS category_id UNION SELECT 207 AS category_id UNION SELECT 208 AS category_id UNION SELECT 209 AS category_id UNION SELECT 210 AS category_id UNION SELECT 211 AS category_id UNION SELECT 228 AS category_id UNION SELECT 221 AS category_id UNION SELECT 224 AS category_id UNION SELECT 225 AS category_id UNION SELECT 238 AS category_id UNION SELECT 249 AS category_id UNION SELECT 250 AS category_id UNION SELECT 254 AS category_id UNION SELECT 255 AS category_id UNION SELECT 256 AS category_id UNION SELECT 258 AS category_id UNION SELECT 259 AS category_id UNION SELECT 263 AS category_id UNION SELECT 264 AS category_id UNION SELECT 265 AS category_id UNION SELECT 266 AS category_id UNION SELECT 267 AS category_id UNION SELECT 268 AS category_id UNION SELECT 269 AS category_id UNION SELECT 270 AS category_id UNION SELECT 271 AS category_id UNION SELECT 272 AS category_id UNION SELECT 274 AS category_id UNION SELECT 275 AS category_id UNION SELECT 276 AS category_id UNION SELECT 277 AS category_id UNION SELECT 278 AS category_id 
    	AS c ON (c.category_id=r.category_id)) AS br ON (b.book_id=br.book_id)
    WHERE 1

    половину пришлось отрезать - форма не пускала. Общая длина запроса - более 12k
    задача - выбрать кол-во книг из текущей категории и всех вложенных

    IntenT, 18 Июня 2012

    Комментарии (23)
  5. SQL / Говнокод #10979

    −78

    1. 1
    tg_hujak_v_zap_na_sklade

    Название триггера в промышленной системе.
    Hint: zap_na_sklade - таблица

    Xps, 16 Июня 2012

    Комментарии (34)
  6. SQL / Говнокод #10965

    −165

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    $sql = 'SELECT * FROM 
                        (SELECT COUNT(company.id) as cnt_company , 
                        FROM_DAYS(TO_DAYS( company.created_at )) as day 
                            FROM company
                        JOIN contact ON contact.company_id = company.id
                        JOIN issuer ON issuer.id = company.issuer_id
                        JOIN bill ON bill.company_id = company.id
                            WHERE contact.partner_id ='.$this->_partner->getId().'
                            AND company.created_at '.$query.'
                        GROUP BY day) as A 
                    LEFT JOIN
                        (SELECT COUNT(process.id) as finished, 
                        FROM_DAYS(TO_DAYS( process.finished_at )) as day2 
                            FROM process
                        JOIN bill on bill.id= process.bill_id
                        JOIN company ON bill.company_id = company.id
                        JOIN contact ON contact.company_id = company.id
                        JOIN issuer ON issuer.id = company.issuer_id
                            WHERE contact.partner_id ='.$this->_partner->getId().'
                            AND issuer.signature_article_id = bill.article_id
                        GROUP BY day2) as B ON A.day = B.day2
                    LEFT JOIN
                        (SELECT COUNT( * ) as count,
                        FROM_DAYS(TO_DAYS(click.created_at)) as day3,
                        COUNT( DISTINCT click.ip ) as uniq 
                            FROM click 
                        WHERE click.partner_id = '.$this->_partner->getId().'
                        AND click.created_at '.$query.'
                           GROUP BY day3)  as C ON A.day = C.day3
                    UNION
                    SELECT * FROM 
                        (SELECT COUNT(company.id) as cnt_company , 
                        FROM_DAYS(TO_DAYS( company.created_at )) as day 
                            FROM company
                        JOIN contact ON contact.company_id = company.id
                        JOIN issuer ON issuer.id = company.issuer_id
                        JOIN bill ON bill.company_id = company.id
                            WHERE contact.partner_id ='.$this->_partner->getId().'
                            AND DATE(company.created_at)'.$query.'
                        GROUP BY day) as A 
                    RIGHT JOIN
                        (SELECT COUNT(process.id) as finished, 
                        FROM_DAYS(TO_DAYS( process.finished_at )) as day2 
                            FROM process
                        JOIN bill on bill.id= process.bill_id
                        JOIN company ON bill.company_id = company.id
                        JOIN contact ON contact.company_id = company.id
                        JOIN issuer ON issuer.id = company.issuer_id
                            WHERE contact.partner_id ='.$this->_partner->getId().'
                        AND issuer.signature_article_id = bill.article_id
                        GROUP BY day2 ) as B ON A.day = B.day2
                    RIGHT JOIN
                        (SELECT COUNT( * ) as count,
                        FROM_DAYS(TO_DAYS(click.created_at)) as day3,
                        COUNT( DISTINCT click.ip ) as uniq 
                            FROM click 
                        WHERE click.partner_id = '.$this->_partner->getId().'
                        AND click.created_at '.$query.'
                           GROUP BY day3)  as C ON A.day = C.day3';

    T_T

    raga, 15 Июня 2012

    Комментарии (6)
  7. SQL / Говнокод #10923

    −115

    1. 1
    AND ((FORM_DATA_KONEC IS NULL AND FORM_DATA_NACHALA IS NULL) OR (to_date(to_char(sysdate,'dd.mm'),'dd.mm') BETWEEN to_date(to_char(FORM_DATA_NACHALA,'dd.mm'),'dd.mm') AND to_date(to_char(FORM_DATA_KONEC,'dd.mm'),'dd.mm')))

    Нашел вот у себя.

    Нужно было определить входит ли текущие день-месяц в диапазон FORM_DATA_NACHALA .. FORM_DATA_KONEC, при том что там хранится дата целиком для других целей.

    Oracle.

    vitaly, 12 Июня 2012

    Комментарии (15)
  8. SQL / Говнокод #10433

    −118

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    SELECT x.org_spec_id, (
                    SELECT count(c.id) FROM customers c , government_customers_require gcr               
                    WHERE c.org_spec_id=x.org_spec_id AND gcr.customer_id=c.id                
                ) as bal_ins, 
                sum(x.type_b1) as type_b1, sum(x.type_b2) as type_b2, sum(x.type_b3) as type_b3, sum(x.bus_debit) as bus_debit,
                sum(x.gost_ac1) as gost_ac1, sum(x.gost_ac2) as gost_ac2, sum(x.nav_ac1) as nav_ac1, sum(x.nav_ac2) as nav_ac2,
                \'-\' as price1, \'-\' as price2, \'-\' as price3, \'-\' as price4, sum(x.scope_b1) as scope_b1,
                sum(x.scope_b2) as scope_b2, sum(x.scope_b3) as scope_b3, sum(x.scope_b4) as scope_b4, sum(x.scope_b5) as scope_b5,
                sum(x.scope_b6) as scope_b6, sum(x.add11) as add11, sum(x.add12) as add12, sum(x.add21) as add21, 
                sum(x.add22) as add22, sum(x.add23) as add23, sum(x.add31) as add31, sum(x.add32) as add32, sum(x.add33) as add33
                
                FROM (SELECT c.org_spec_id, b.id , c.id as bal_ins,
                case when bsd.id is not null then 1 else 0 end as bus_debit,
                case when bsd.id is null then case when bm.type_id = 1 then 1 else 0 end else 0 end as type_b1, 
                case when bsd.id is null then case when bm.type_id = 2 then 1 else 0 end else 0 end as type_b2, 
                case when bsd.id is null then case when bm.type_id = 3 then 1 else 0 end else 0 end as type_b3,            
                case when bsd.id is null AND bm.type_id = 1 then case when gst.accepted then 1 else 0 end else 0 end as gost_ac1,
                case when bsd.id is null AND bm.type_id = 1 then case when gst.accepted then 0 else 1 end else 0 end as gost_ac2,
                case when bsd.id is null AND bm.type_id = 1 then case when nav.id is not null then 1 else 0 end else 0 end as nav_ac1,
                case when bsd.id is null AND bm.type_id = 1 then case when nav.id is not null then 0 else 1 end else 0 end as nav_ac2,
                case when bsd.id is null AND b.scope_id = 2 then 1 else 0 end as scope_b1,
                case when bsd.id is null AND b.scope_id = 1 then 1 else 0 end as scope_b2,
                case when bsd.id is null AND b.scope_id = 3 then 1 else 0 end as scope_b3,
                case when bsd.id is null AND b.scope_id = 4 then 1 else 0 end as scope_b4,
                case when bsd.id is null AND b.scope_id = 6 then 1 else 0 end as scope_b5,
                case when bsd.id is null AND b.scope_id = 5 then 1 else 0 end as scope_b6,
                case when bsd.id is null then case when add1.id is not null then case when add1.selected_variant=\'on\' then 1 else 0 end else 1 end else 0 end as add11,
                case when bsd.id is null then case when add1.id is not null then case when add1.selected_variant=\'off\' then 1 else 0 end else 0 end else 0 end as add12,
                case when bsd.id is null then case when add2.id is not null then case when add2.selected_variant=\'on\' then 1 else 0 end else 0 end else 0 end as add21,
                case when bsd.id is null then case when add2.id is not null then case when add2.selected_variant=\'off\' then 1 else 0 end else 0 end else 0 end as add22,
                case when bsd.id is null then case when add2.id is not null then case when add2.selected_variant=\'no\' then 1 else 0 end else 1 end else 0 end as add23,
                case when bsd.id is null then case when add3.id is not null then case when add3.selected_variant=\'on\' then 1 else 0 end else 0 end else 0 end as add31,
                case when bsd.id is null then case when add3.id is not null then case when add3.selected_variant=\'off\' then 1 else 0 end else 0 end else 0 end as add32,
                case when bsd.id is null then case when add3.id is not null then case when add3.selected_variant=\'no\' then 1 else 0 end else 1 end else 0 end as add33

    asfalanaft, 29 Мая 2012

    Комментарии (12)
  9. SQL / Говнокод #10376

    −131

    1. 1
    2. 2
    alter table EqualityCodes add constraint chk_EqualityCodes_Code
      check (Code not in ('', ' ', '  ', '   ', '    ', '     ', '      ', '       '));

    glprizes, 28 Мая 2012

    Комментарии (16)
  10. SQL / Говнокод #10365

    −121

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    case when z.nprc is null then
                                 case when ..........
                                      then  z.nsalary
                                      else  .........
                                 end * (nvl(sum(z.hour), 0)) * nvl(z.nprc, 0) / 100
                              else
                                 .........
                              end

    При условии если z.nprc is null то входим в кейс значение которого при любом раскладе будет умножено на ноль
    * (nvl(sum(z.hour), 0)) * nvl(z.nprc, 0) / 100 эквивалентно (nvl(sum(z.hour), 0)) * 0 / 100

    siv163, 25 Мая 2012

    Комментарии (1)