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

    −131.6

    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
    SELECT ct.com_id, cdt.com_name, ct.tax_id, pt.tax_name, tt.date  FROM com_table AS ct LEFT OUTER 
    JOIN com_des_table AS cdt ON cdt.id = ct.com_id LEFT OUTER JOIN payments_table AS pt ON pt.id = 
    ct.tax_id LEFT OUTER JOIN tax_table AS tt ON tt.tax_id = ct.tax_id WHERE ct.tax_id LIKE '001%' and 
    ct.com_id = '1' UNION SELECT ct.com_id, cdt.com_name, ct.tax_id, rt.tax_name, tt.date  FROM com_table 
    AS ct LEFT OUTER JOIN com_des_table AS cdt ON cdt.id = ct.com_id LEFT OUTER JOIN reports_table AS 
    rt ON rt.id = ct.tax_id LEFT OUTER JOIN tax_table AS tt ON tt.tax_id = ct.tax_id WHERE ct.tax_id LIKE 
    '002%' and ct.com_id = '1' UNION SELECT ct.com_id, cdt.com_name, ct.tax_id, pt.tax_name, tt.date  FROM 
    com_table AS ct LEFT OUTER JOIN com_des_table AS cdt ON cdt.id = ct.com_id LEFT OUTER JOIN 
    payments_table AS pt ON pt.id = ct.tax_id LEFT OUTER JOIN tax_table AS tt ON tt.tax_id = ct.tax_id 
    WHERE ct.tax_id LIKE '001%' and ct.com_id = '0' UNION SELECT ct.com_id, cdt.com_name, ct.tax_id, 
    rt.tax_name, tt.date  FROM com_table AS ct LEFT OUTER JOIN com_des_table AS cdt ON cdt.id = ct.com_id 
    LEFT OUTER JOIN reports_table AS rt ON rt.id = ct.tax_id LEFT OUTER JOIN tax_table AS tt ON tt.tax_id = 
    ct.tax_id WHERE ct.tax_id LIKE '002%' and ct.com_id = '0' UNION SELECT ct.com_id, cdt.com_name, 
    ct.tax_id, pt.tax_name, tt.date  FROM com_table AS ct LEFT OUTER JOIN com_des_table AS cdt ON cdt.id = 
    ct.com_id LEFT OUTER JOIN payments_table AS pt ON pt.id = ct.tax_id LEFT OUTER JOIN tax_table AS tt 
    ON tt.tax_id = ct.tax_id WHERE ct.tax_id LIKE '001%' and ct.com_id = '2' UNION SELECT ct.com_id, 
    cdt.com_name, ct.tax_id, rt.tax_name, tt.date  FROM com_table AS ct LEFT OUTER JOIN com_des_table AS 
    cdt ON cdt.id = ct.com_id LEFT OUTER JOIN reports_table AS rt ON rt.id = ct.tax_id LEFT OUTER JOIN 
    tax_table AS tt ON tt.tax_id = ct.tax_id WHERE ct.tax_id LIKE '002%' and ct.com_id = '2' UNION SELECT 
    ct.com_id, cdt.com_name, ct.tax_id, pt.tax_name, tt.date  FROM com_table AS ct LEFT OUTER JOIN 
    com_des_table AS cdt ON cdt.id = ct.com_id LEFT OUTER JOIN payments_table AS pt ON pt.id = ct.tax_id 
    LEFT OUTER JOIN tax_table AS tt ON tt.tax_id = ct.tax_id WHERE ct.tax_id LIKE '001%' and ct.com_id = '3' 
    UNION SELECT ct.com_id, cdt.com_name, ct.tax_id, rt.tax_name, tt.date  FROM com_table AS ct LEFT 
    OUTER JOIN com_des_table AS cdt ON cdt.id = ct.com_id LEFT OUTER JOIN reports_table AS rt ON rt.id = 
    ct.tax_id LEFT OUTER JOIN tax_table AS tt ON tt.tax_id = ct.tax_id WHERE ct.tax_id LIKE '002%' and 
    ct.com_id = '3';

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

    Washington, 17 Апреля 2010

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

    −864.6

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    "SELECT null AS counter, t1.* FROM student.select_recomended_candidate_zo(" + ((DataBaseItem)lbSpeciality.SelectedItem).ItemId + ") t1 " +
                                                 "LEFT JOIN (SELECT t3.id, t3.specperiod_id FROM general.select_speciality_extended_alt(" + AppData.PointOfActualityDBFormat + ") t3) lj2 ON lj2.id = " + ((DataBaseItem)lbSpeciality.SelectedItem).ItemId + " " +
                                                 "LEFT JOIN (SELECT DISTINCT id, ex1_val, ex2_val, ex3_val FROM student.select_candidate_dean(" + ((DataBaseItem)lbSpeciality.SelectedItem).ItemId + ") t2) lj1 ON t1.id = lj1.id " +
                                                "WHERE (lj2.specperiod_id = 1 AND lj1.ex1_val IS NOT null AND lj1.ex2_val IS NOT null AND lj1.ex3_val IS NOT null) OR (lj2.specperiod_id > 1 AND lj1.ex1_val IS NOT null) IS true " +
                                                "ORDER BY has_benifit DESC, this_is_special DESC, sum DESC"

    des-1008d, 16 Апреля 2010

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

    −869.4

    1. 1
    select ST1,lower(naim||' ( '||nam||' )') from ST_REAL, (select nvl(d.k_dor, g.k_admi+200) kod, decode(:lng,'uk',n_strnus/*ua_country*/,'ru',N_Strnr/*ru*_country*/) nam from (select relsa.k_admi,N_Strnr,n_strnus from d_adm@psv_nsi relsa, d_strn@psv_nsi strana/*, arm_spr.gosdor@uzc1 doroga*/ where relsa.k_strn=strana.k_strn)

    ramzes2, 10 Апреля 2010

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

    −180.2

    1. 1
    SELECT ((SELECT COUNT(*) FROM `books` WHERE `author` = '?') + (SELECT COUNT(*) FROM `magazins` WHERE `author` = '?')));

    не многовато ли селектов?:))
    вместо '?' пхпшный код ставит значение.

    litrumb, 09 Апреля 2010

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

    −862

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    CREATE TABLE IF NOT EXISTS `tbl_game` (
      `game_id` bigint(20) NOT NULL AUTO_INCREMENT,
      `game_name` varchar(150) CHARACTER SET cp1251 COLLATE cp1251_ukrainian_ci NOT NULL DEFAULT '',
      `upload_date` bigint(20) NOT NULL DEFAULT '0',
      `magazine_id` bigint(20) NOT NULL DEFAULT '0',
      `producer_id` int(11) NOT NULL DEFAULT '0',
      `dir` varchar(60) NOT NULL DEFAULT '',
      PRIMARY KEY (`game_id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=538 ;

    Вот наткнулся на такую вот говенную таблицу.

    Для некоторых случаев возможностей utf-8 видно не хватило, и пришлось использовать "продвинутую" cp1251

    Yngvie, 07 Апреля 2010

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

    −866.8

    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
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    93. 93
    94. 94
    95. 95
    CREATE procedure dbo.BASE_W_ORDER_NEYAVKAM
    @date_in as nvarchar(10),
    @id_users as int
    as
    
    declare @date as datetime
    
    set @date = convert(datetime, @date_in, 104)
    --set @date = convert(datetime, cast(@date_in as datetime), 104)
    
    select* from(
    select
    p.[ID] as [ID],
    p.TABEL_NUM as TABEL_NUM,
    p.FAMILY as FAMILY,
    p.[NAME] as NAME,
    p.SECNAME as SECNAME,
    p.ID_DEPT as ID_DEPT,
    d.[NAME] as DEPT,
    p.POSITION as POSITION,
    --dbo.GetCmena(p.ID_GRAPH, convert(nvarchar(10), getdate(), 104)) as SID,
    s.[id] as SID,
    s.[NAME] as SMENA,
    s.START_SM as START_SM,
    s.END_SM as END_SM,
    s.START_DIN as START_DIN,
    s.END_DIN as END_DIN,
    p.PHOTO as PHOTO,
    -- формируем отчет для тех
    -- если приказ не назначен
    case	when not exists(select*from base_codes_s cc where cc.[id] = p.id_codes and cc.status like '0_1_____') then 
    		-- у кого график назначен
    	case	when (isnull(p.id_graph,0) = 0) then 1
    		-- у кого код пропуска присвоен
    		when (isnull(p.id_Codes,0) = 0) then 1
    		-- пропуск не имеет статус администратора
    		when exists(select*from base_codes_s cc where cc.[id] = p.id_codes and cc.status like '0_1_____') then 1
    		-- пропуск не просрочен
    		when (@date < isnull(c.date_in, @date)
    		or @date > isnull(c.date_out, @date)) then 1
    		-- Сотрудник в отпуске
    		when exists(select*from TABL_OTPUSK o where o.ID_PERSONAL = p.id_codes and convert(nvarchar(10), o.[DATE], 104) = convert(nvarchar(10), @date, 104)) then 1
    		-- празднечный день
    		when exists(select*from TABL_HOLIDAY_S hol where convert(nvarchar(10), hol.[DATE], 104) = convert(nvarchar(10), @date, 104)) then 1
    		else 0 end
    else
    -- делаем расчеты в соответствии с приказом
    
    
    		-- у кого график назначен
    	case	when (isnull(p.id_graph,0) = 0) then 1
    		-- у кого код пропуска присвоен
    		when (isnull(p.id_Codes,0) = 0) then 1
    		-- пропуск не имеет статус администратора
    		when exists(select*from base_codes_s cc where cc.[id] = p.id_codes and cc.status like '0_1_____') then 1
    		-- пропуск не просрочен
    		when (@date < isnull(c.date_in, @date)
    		or @date > isnull(c.date_out, @date)) then 1
    		-- Сотрудник в отпуске
    		when exists(select*from TABL_OTPUSK o where o.ID_PERSONAL = p.id_codes and convert(nvarchar(10), o.[DATE], 104) = convert(nvarchar(10), @date, 104)) then 1
    		-- празднечный день
    		when exists(select*from TABL_HOLIDAY_S hol where convert(nvarchar(10), hol.[DATE], 104) = convert(nvarchar(10), @date, 104)) then 1
    		else 0 end end as XREN
    
    from base_pesonal_S p
    -- присоединяем пропуска
    left join base_codes_s c on
    	p.id_codes = c.[id]
    -- присоединяем отделы
    left join base_dept d on
    	p.id_dept = d.[id]
    -- Узнаем смену назначенную на день отчета для графика который назначен сотруднику
    LEFT OUTER join BASE_SMENA_S s ON
    	s.[id] = (select top 1 ID_SM
    	from
    	BASE_GRAPH as g
     inner join BASE_GRAPH_NAME_S as gn on gn.[id] = g.ID_GRAPH_NAME
     inner join BASE_SMENA as s on s.[id] = g.id_SM
     inner join (select BASE_GRAPH.ID_GRAPH_NAME, count(*) cntsmen from BASE_GRAPH group by BASE_GRAPH.ID_GRAPH_NAME) sm
     on (datediff(dd, gn.date_in, convert(datetime, @date, 104)) % sm.cntsmen) + 1 = g.[num] and g.ID_GRAPH_NAME = sm.ID_GRAPH_NAME
    where g.id_graph_name = p.ID_GRAPH)
    ) as t1
    -- смена не должна быть выходной
    
    where XREN <> 1 
    	and SID <> 1 
    	and not exists(select top 1 * from base_events e where id_p = [id] 
    			and convert(nvarchar(10), e.[TIME], 104) = convert(nvarchar(10), @date, 104) 
    			and (ascii(e.CODE) = ascii('I') 
    				or ascii(e.CODE) = ascii('O')
    			)
    		)
    	and id_dept in(select id_dept from BASE_ACCEESS where id_users = @id_users) -- отчет только для интерисующих на отделов
    
    GO

    leo777777, 07 Апреля 2010

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

    −857

    1. 1
    2. 2
    SELECT * FROM [dbo].[Publications]												
    where [UserId] not like 'NULL'

    UserId, естественно, int

    synapse, 01 Апреля 2010

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

    −873.6

    1. 1
    2. 2
    declare @FromDate datetime, @ToDate datetime
    set @FromDate =  convert(char(10),dateadd(dd,-1,getdate()),20)

    synapse, 31 Марта 2010

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

    −151.8

    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
    CREATE TABLE IF NOT EXISTS `upmenu_block` (
      `id_foto_b` int(5) NOT NULL auto_increment,
      `id_sec` int(5) NOT NULL,
      `f_b1` varchar(100) NOT NULL,
      `f_b2` varchar(100) NOT NULL,
      `f_b3` varchar(100) NOT NULL,
      `f_b4` varchar(100) NOT NULL,
      `f_o1` varchar(200) NOT NULL,
      `f_o2` varchar(200) NOT NULL,
      `f_o3` varchar(200) NOT NULL,
      `f_o4` varchar(200) NOT NULL
      PRIMARY KEY  (`id_foto_b`)
    )

    В зависимости от таба (1-4), в котором будет показана фотка, ее название и имя файла записываются в поля f_bN, f_oN.

    deraider, 27 Марта 2010

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

    −157.2

    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
    Select @clientMoney_DayEnd = coalesce([Money], 0.0)
    FROM dbo.AccountOperation 
    where 
    	OperationDateTime = (
    		select max(OperationDateTime) 
    		from dbo.AccountOperation 
    		where 
    			cast(year(OperationDateTime) as varchar(4)) + 
    				right('0' + cast(month(OperationDateTime) as varchar(2)), 2) + 
    				right('0' + cast(day(OperationDateTime) as varchar(2)), 2) <= 
    			(
    				select cast(datepart(year, max(QuoteDateTime)) as varchar(4)) +
    						right('0' + cast(datepart(month, max(QuoteDateTime)) as varchar(2)), 2) +
    						right('0' + cast(datepart(day, max(QuoteDateTime)) as varchar(2)), 2) + ' 23:59'
    				from CloseQuote
    				where
    					QuoteDateTime < dateadd(day, 1, @currDate)			
    			) and 
    			[Money] is not null and 
    			ClientID = @clientID
    	)  and 
    	[Money] is not null and 
    	ClientID = @clientID
    	order by ID desc

    Разбить datetime на год, месяц, день. Отдельно каждое начение привести к чару, собрать из них строку даты... Ну и посравнивать ее с такой же шедеврашьной строкой...
    А да... и все это в мега-курсоре для каждой даты...
    Другой вариант для данной задачи, хотябы cast(OperationDateTime as date) cast(QuoteDateTime as date), неприемлем категорически:):):):):)::)

    38popugaev, 26 Марта 2010

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