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

    −860

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    -- Из простого...
    select kdet, cexnum, cex, last_value(cex) over(partition by kdet) as final_cex
    from xxtpa_rout_aztpa_v;
    
    -- Сделаем сложное...
    select t.*, max(max_cex) over(partition by kdet, rout_num) max_cex
    from (select t.*, case cex_num when max(cex_num) over(partition by t.kdet, t.rout_num) then t.cex else null end max_cex
    from xxtpa_rout_aztpa_v t) t;

    Пример того, как НЕ применять KISS-принцип.

    dwinner, 22 Апреля 2011

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

    −860

    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 
    (CASE WHEN d.t1_aup_f IS NULL THEN (
      CASE WHEN (lag(sum(d.t1_aup_f),1) over (order by d.real_day)) IS NULL THEN (
       CASE WHEN (lag(sum(d.t1_aup_f),2) over (order by d.real_day)) IS NULL THEN (
     /* ... */
                                 CASE WHEN (lag(sum(d.t1_aup_f),28) over (order by d.real_day)) IS NULL THEN (
                                  CASE WHEN (lag(sum(d.t1_aup_f),29) over (order by d.real_day)) IS NULL THEN
                                   lag(sum(d.t1_aup_f),30) over (order by d.real_day )
                                  ELSE (lag(sum(d.t1_aup_f),29) over (order by d.real_day)) END)
                                 ELSE (lag(sum(d.t1_aup_f),28) over (order by d.real_day)) END)
                                ELSE (lag(sum(d.t1_aup_f),27) over (order by d.real_day)) END)
     /* ... */
       ELSE (lag(sum(d.t1_aup_f),2) over (order by d.real_day)) END)
      ELSE (lag(sum(d.t1_aup_f),1) over (order by d.real_day)) END)
    ELSE d.t1_aup_f END) gf_1 
    from sometable

    таких полей было больше. тут http://paste.org.ru/?zlc3b8 - целиком. для полноты картины. надеюсь, не я написал, хотя... ))))

    RomaShka, 15 Апреля 2011

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

    −858

    1. 1
    2. 2
    3. 3
    4. 4
    select
        if(t.town_name is null, t.id, t.town_name) as 'town',
        ...
     from towns t;

    в sql внезапно тоже есть приведение типов и можно получить вот такое назвение города
    1101000010010010110100001011101111010000 1011000011010000101101001101000010111000 1101000010110010110100001011111011010001 1000000111010001100000101101000010111110 1101000010111010

    zlob.jc, 13 Апреля 2011

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

    −856

    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
    ALTER   PROCEDURE Estimate  @Goods_ID int ,@Workshop_ID int AS
    
    SELECT 9, null, null,  'Арматура',null, null ,  null,  null
    Union
    SELECT     8, dbo.Charges.FK_TypeCh, dbo.Charges.ID, '       '+dbo.Charges.NAME, dbo.Charge_Price.KBO,   
    	             dbo.Charges.Cost, Round(dbo.Charges.Cost*dbo.Charge_Price.KBO,1), 
    		Round( dbo.Charges.Cost*dbo.Charge_Price.KBO/ dbo.Goods.volume,1)
    FROM         dbo.Charge_Price INNER JOIN
                          dbo.Charges ON dbo.Charge_Price.FK_Charge = dbo.Charges.ID INNER JOIN
                          dbo.Workshop ON dbo.Charge_Price.FK_Workshop = dbo.Workshop.ID INNER JOIN
    	        dbo.Goods ON dbo.Charge_Price.FK_Goods= dbo.Goods.ID	
    WHERE     (dbo.Charge_Price.FK_Workshop = 6) AND (dbo.Charge_Price.FK_Goods = @Goods_ID)
    Union
    SELECT     7, null, null,'         Всего  по арматуре', null,   null,Round( SUM( dbo.Charges.Cost*dbo.Charge_Price.KBO) ,1)as Сумма, 
    	Round(SUM( dbo.Charges.Cost*dbo.Charge_Price.KBO/ dbo.Goods.volume),1)
    FROM         dbo.Charge_Price INNER JOIN
    	      dbo.Charges ON dbo.Charge_Price.FK_Charge = dbo.Charges.ID INNER JOIN
                        dbo.Workshop ON dbo.Charge_Price.FK_Workshop = dbo.Workshop.ID INNER JOIN
    	        dbo.Goods ON dbo.Charge_Price.FK_Goods= dbo.Goods.ID	
    WHERE     (dbo.Charge_Price.FK_Workshop = 6) AND (dbo.Charge_Price.FK_Goods = @Goods_ID)
    Union
    /*
      Ещё мурнадцать запросов с объединением
    */
    UNION
    SELECT     -3,  null, null,'         Полная себестоимость',   null, null, Round(SUM( dbo.Charges.Cost*dbo.Charge_Price.KBO)*1.05,1) as Сумма, 
    	Round(SUM( dbo.Charges.Cost*dbo.Charge_Price.KBO/ dbo.Goods.volume)*1.05,1)
    FROM         dbo.Charge_Price INNER JOIN
    	      dbo.Charges ON dbo.Charge_Price.FK_Charge = dbo.Charges.ID INNER JOIN
                        dbo.Workshop ON dbo.Charge_Price.FK_Workshop = dbo.Workshop.ID INNER JOIN
    	        dbo.Goods ON dbo.Charge_Price.FK_Goods= dbo.Goods.ID	
    WHERE    ( (dbo.Charge_Price.FK_Workshop = @Workshop_ID)  OR  (dbo.Charge_Price.FK_Workshop = 6)   OR
    		 (dbo.Charge_Price.FK_Workshop = 7) )AND (dbo.Charge_Price.FK_Goods = @Goods_ID) 
    UNION 
    SELECT     -4,  null, null,'         Плановые накопления',   null, null, Round(SUM( dbo.Charges.Cost*dbo.Charge_Price.KBO)*0.1575,1) as Сумма, 
    	Round(SUM( dbo.Charges.Cost*dbo.Charge_Price.KBO/ dbo.Goods.volume)*0,1575,1)
    FROM         dbo.Charge_Price INNER JOIN
    	      dbo.Charges ON dbo.Charge_Price.FK_Charge = dbo.Charges.ID INNER JOIN
                        dbo.Workshop ON dbo.Charge_Price.FK_Workshop = dbo.Workshop.ID INNER JOIN
    	        dbo.Goods ON dbo.Charge_Price.FK_Goods= dbo.Goods.ID	
    WHERE    ( (dbo.Charge_Price.FK_Workshop = @Workshop_ID)  OR  (dbo.Charge_Price.FK_Workshop = 6)   OR
    		 (dbo.Charge_Price.FK_Workshop = 7) )AND (dbo.Charge_Price.FK_Goods = @Goods_ID) 
    UNION
    SELECT     -5,  null, null,'         Цена оптовая',   null, null, Round(SUM( dbo.Charges.Cost*dbo.Charge_Price.KBO)*1.2075,1) as Сумма, 
    	Round(SUM( dbo.Charges.Cost*dbo.Charge_Price.KBO/ dbo.Goods.volume)*1,2075,1)
    FROM         dbo.Charge_Price INNER JOIN
    	      dbo.Charges ON dbo.Charge_Price.FK_Charge = dbo.Charges.ID INNER JOIN
                        dbo.Workshop ON dbo.Charge_Price.FK_Workshop = dbo.Workshop.ID INNER JOIN
    	        dbo.Goods ON dbo.Charge_Price.FK_Goods= dbo.Goods.ID	
    WHERE    ( (dbo.Charge_Price.FK_Workshop = @Workshop_ID)  OR  (dbo.Charge_Price.FK_Workshop = 6)   OR
    		 (dbo.Charge_Price.FK_Workshop = 7) )AND (dbo.Charge_Price.FK_Goods = @Goods_ID) 
    
    Order by 1 desc, 2, 3

    Формирование какой то сметы в стле 1С

    dens, 12 Апреля 2011

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

    −855

    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
    SELECT h.*,
    			      if(h.gender_id != 0, (SELECT g.title FROM p_cm5_horses_gender g WHERE h.gender_id = g.id AND h.lang_id = g.lang_id),"") AS gender,
    			        if(h.colour_id != 0, (SELECT c.title FROM p_cm5_horses_colour c WHERE h.colour_id = c.id AND h.lang_id = c.lang_id),"") AS colour,
    			        if(h.color_group != 0, (SELECT cg.title FROM p_cm5_horses_colour_group cg WHERE h.color_group = cg.id AND h.lang_id = cg.lang_id),"") AS color_group_name,
    			        if(h.temper_id != 0, (SELECT t.title FROM p_cm5_horses_temper t WHERE h.temper_id = t.id AND h.lang_id = t.lang_id),"") AS temper,
    			        if(h.grading_id != 0, (SELECT t.title FROM p_cm5_horses_grading t WHERE h.temper_id = t.id AND h.lang_id = t.lang_id),"") AS grading,
    			        if(h.breed_id != 0, (SELECT b.title FROM p_cm5_horses_studbook b WHERE h.breed_id = b.id AND h.lang_id = b.lang_id),"") AS breed,
    			        if(h.training_first != 0, (SELECT t.training_name FROM p_cm5_horses_training t WHERE h.training_first = t.id AND h.lang_id = t.lang_id),"") AS training_first_name,
    			        if(h.level_first != 0, (SELECT t.level_name FROM p_cm5_horses_training_level t WHERE h.level_first = t.id AND h.lang_id = t.lang_id),"") AS level_first_name,
    			        if(h.training_second != 0, (SELECT t.training_name FROM p_cm5_horses_training t WHERE h.training_second = t.id AND h.lang_id = t.lang_id),"") AS training_second_name,
    			        if(h.level_second != 0, (SELECT t.level_name FROM p_cm5_horses_training_level t WHERE h.level_second = t.id AND h.lang_id = t.lang_id),"") AS level_second_name,
    			        if(h.price_id != 0, (SELECT p.title FROM p_cm5_horses_price p WHERE h.price_id = p.id AND h.lang_id = p.lang_id),"") AS price,
    			        if(h.price_id != 0, (SELECT p.title FROM p_cm5_horses_price p WHERE h.price_id = p.id AND p.lang_id = 2),"") AS price2
    			        FROM p_cm5_horses h
    			        WHERE h.enabled = 1 AND h.lang_id = 1 AND h.id = 219

    копался в одном проекте который нам отдали на аутсорс, нашел своего рода LEFT JOIN-ины

    fosco, 08 Апреля 2011

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

    −855

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    ALTER  TRIGGER Snag ON [dbo].[Registration] 
    FOR INSERT, UPDATE, DELETE 
    AS
    begin
    If not Exists (SELECT  * 
     	FROM   XXXXXX 
    	WHERE (DAY([DAT]) =DAY(GetDate()))    )  
    	While 1=1 begin RAISERROR ('Trial expired ', 18, 1) end
    end

    trial period control :)

    dens, 06 Апреля 2011

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

    −864

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    SELECT COUNT(*) FROM apteka.cache_offers WHERE ORG_ID = '21963' AND `subject_id` IN (35,30052,20262,20263,20001,30488,30776,22099,22044,21982,21938,21823,
    (еще триста чисел),
    26313,26358,26478,23381,27430,26367,26418,23433,23404,23372,23289,23321,23285,23315,23282,23406,23287,
    26356,23429,23263,23349,23399,23356,23291,23346,23351,23250,23198,23194,23319,23238,23296,23259,23167,23268,23130,23052,23119,22430,23146,22315,23069,23083,23
    127,23189,23023,23014,23059,22988,23020,23007,22989,22949,22937,22939,22941,22979,22786,22842,22849,22758,22913,22919,22833,22718,22753,22779,22724,22857,2276
    8,22761,22733,22823,22759,22764,22658,22681,22674,22706,22599,22698,22667,22627,22776,22654,22655,22601,22619,22598,22694,22696,22603,22541,22570,22469,22534,
    22515,22676,22516,22477,22491,22677,22513,22421,22537,22451,22507,22482,22440,22353,22523,22422,22510,22397,22473,22348,22383,22424,22333,22380,22359,22508,22
    486,22456,22330);

    Стандартный запрос. При формировании страницы конструкция встречается многократно (>10 раз)

    deadwire, 05 Апреля 2011

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

    −162

    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
    USE [VOLTAREN]
    GO
    /****** Object:  UserDefinedFunction [dbo].[myReplace]    Script Date: 04/05/2011 12:11:42 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER FUNCTION [dbo].[myReplace](@NAME varchar(255))
    RETURNS varchar(255)
    BEGIN
    set @NAME = (
    	UPPER(
    replace(
    	replace(
    		replace(
    			replace(
    				replace(
    					replace(
    						replace(
    							replace(
    								replace(
    									replace(
    										replace(
    											replace(
    												replace(
    													replace(
    														replace(
    															replace(
    																replace(
    																	replace(
    																		replace(
    																			replace(
    																				replace(
    																					replace(
    																						replace(
    																							replace(
    																								replace(
    																									replace(
    																										replace(
    																											replace(
    																												replace(
    																													replace(
    																														replace(
    																															replace(
    																																replace(
    																																	replace(
    																																		replace(
    																																			replace(
    																																				@NAME,'*','')
    																																			,'табл','таб')
    																																		,'~','')
    																																	,'`','')
    																																,'<','')
    																															,'>','')
    																														,'}','')
    																													,'{','')
    																												,']','')
    																											,'[','')
    																										,'=','')
    																									,'_','')
    																								,'#','')
    																							,'$','')
    																						,'?','')
    																					,'^','')
    																				,'&','')
    																			,'@','')
    																		,'''','')
    																	,'"','')	
    																,':','')
    															,'%','')
    														,'-','')
    													,'+','')
    												,')','')
    											,'(','')
    										,'N','')
    									,'№','')
    								,'|','')
    							,'\','')
    						,'/','')
    					,'!','')
    				,';','')
    			,',','')
    		,'.','')
    	,' ','')));
    	
    
    return(
    replace(
    	@NAME,
    'покробол','по')
    )
    	
    
    END;

    функция удаления не нужных символов из строки

    cu3blukekc, 05 Апреля 2011

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

    −858

    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
    create or replace function t8() returns integer as $$
    declare
        rec record;
    begin
        for rec in select * from purchase loop
          update purchase set amount=(
                  select book.cost*purchase.quantity*(1-customer.discount/100.0)
                  from purchase, book, customer
                  where purchase.id=rec.id and
                      book.id=purchase.book and
                      customer.id=purchase.customer
          )
          where purchase.id=rec.id;
        end loop;
    
        return 1;
    end;
    $$ language plpgsql;
    
    select t8();

    прочно засевшие в голове алгоритмические языки

    ilardm, 04 Апреля 2011

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

    −861

    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
    if @new_dg_sor_code is null and @dg_sor_code<>7
    and
    (
    	(
    		(select count(*) from tbl_dogovorlist WITH (nolock) where dl_dgcod=@dg_code and dl_svkey in (1,1158,1166) and dl_control=0)=
    		(select count(*) from tbl_dogovorlist WITH (nolock) where dl_dgcod=@dg_code and dl_svkey in (1,1158,1166))
    	)
    	and (
    		(select count(*) from tbl_dogovorlist WITH (nolock) where dl_dgcod=@dg_code and dl_svkey=3 and dl_control=0)=
    		(select count(*) from tbl_dogovorlist WITH (nolock) where dl_dgcod=@dg_code and dl_svkey=3)
    	)
    	and (
    		(select count(*) from tbl_dogovorlist WITH (nolock) where dl_dgcod=@dg_code and dl_svkey=5 and dl_control=0)=
    		(select count(*) from tbl_dogovorlist WITH (nolock) where dl_dgcod=@dg_code and dl_svkey=5)
    	)
    )

    Видимо зарплата этого парня зависит от количества запросов к tbl_dogovorlist.
    А иначе зачем ему 6 запросов вместо одного?

    Anatoly, 01 Апреля 2011

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