- 1
- 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)
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
−116
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. Может есть где-то специальное учебное заведение, где обучают таким техникам?
−122
exec @res = dbo.p_bis_prepaid_diagnostic @tar_addr, @req_dubug_info output
if (@res = 0)
set @res = 1
else
set @res = 0
−119
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;
Задача на агрегацию с конкатенацией строк. Можно решить правильно - через свой агрегирующий объект, а можно так, "не по правилам" ))
−153
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
задача - выбрать кол-во книг из текущей категории и всех вложенных
−78
tg_hujak_v_zap_na_sklade
Название триггера в промышленной системе.
Hint: zap_na_sklade - таблица
−165
$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
−115
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.
−118
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
−131
alter table EqualityCodes add constraint chk_EqualityCodes_Code
check (Code not in ('', ' ', ' ', ' ', ' ', ' ', ' ', ' '));
−121
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