2012年10月31日 星期三

[MAC] How to install wget

Link: http://osxdaily.com/2012/05/22/install-wget-mac-os-x/


Note:
  1. Install Xcode v4.5.1
  2. Launch Xcode: Preferences... => Downloads => Install Command Line Tools
  3. Launch Terminal and enter the following commands:
    curl -O http://ftp.gnu.org/gnu/wget/wget-1.14.tar.gz
    ((Check http://ftp.gnu.org/gnu/wget/ for the latest version))

The rest are the same. Enjoy it.

生活的變化


我選擇別人選擇的生活。
我選擇別人選擇的工作。
我選擇別人選擇的社會。
我選擇別人選擇的自由。


當情勢無法改變,我選擇別人選擇的順民。



去你媽的情勢,
幹你娘的順民,
懶覺毛的社會,
覽趴火的工作,
吃大便的社會。


等著看!生活也是有變化的,因為那是我的自由。


2012年10月30日 星期二

聖稜線計畫 (2012-11-08 ~ 13)


D1 池有山登山口→三叉營地→池有山→新達山屋

D2 新達山屋→品田山→新達山屋→霸南山屋舊址營地

D3 霸南山屋舊址營地→大霸尖山→小霸尖山→大霸尖山→中霸尖山→伊澤山→中霸尖山→大霸尖山→霸南山屋舊址營地

D4 霸南山屋舊址營地→巴紗拉雲山→布秀蘭山→素密達山→素密達山屋

D5 素密達山屋→雪山北峰→雪北山屋→凱蘭特崑山→北稜角→翠池山屋

D6 翠池山屋→雪山主峰→三六九山莊→雪山東峰→七卡山莊→雪山登山口

2012年10月29日 星期一

[FWD] SQL Server: SQL Server Delays Demystified

Link: http://technet.microsoft.com/en-us/magazine/hh781189.aspx


Our second sample script using the sys.dm_os_wait_stats DMV will help determine the resources on which SQL Server is spending the most time waiting...

Inside Microsoft SQL Server 2008: T-SQL Querying P137 的 query 是

...
AND wait_type NOT LIKE N'%SLEEP%'
AND wait_type NOT LIKE N'%IDLE%'
AND wait_type NOT LIKE N'%QUEUE%'
...


整理一下:

WITH Waits AS 
SELECT 
wait_type, 
wait_time_ms / 1000. AS wait_time_s, 
100. * wait_time_ms / SUM(wait_time_ms) OVER () AS pct,
ROW_NUMBER() OVER (ORDER BY wait_time_ms DESC) AS rn
FROM sys.dm_os_wait_stats 
WHERE wait_type NOT IN 
(
'CLR_SEMAPHORE', 
'SQLTRACE_BUFFER_FLUSH', 
'REQUEST_FOR_DEADLOCK_SEARCH', 
'XE_TIMER_EVENT', 
'BROKER_TO_FLUSH', 
'BROKER_TASK_STOP', 
'CLR_MANUAL_EVENT', 
'CLR_AUTO_EVENT', 
'XE_DISPATCHER_WAIT', 
'XE_DISPATCHER_JOIN'
    ) 
    AND wait_type NOT LIKE N'%SLEEP%'
AND wait_type NOT LIKE N'%IDLE%'
AND wait_type NOT LIKE N'%QUEUE%'
SELECT 
W1.wait_type, 
CAST(W1.wait_time_s AS DECIMAL(12, 2)) AS wait_time_s, 
CAST(W1.pct AS DECIMAL(12, 2)) AS pct, 
CAST(SUM(W2.pct) AS DECIMAL(12, 2)) AS running_pct 
FROM Waits AS W1 
INNER JOIN Waits AS W2 
ON W2.rn <= W1.rn 
GROUP BY W1.rn, W1.wait_type, W1.wait_time_s, W1.pct
HAVING SUM(W2.pct) - W1.pct < 95 -- percentage threshold
OR W1.rn <= 5
ORDER BY W1.rn;


這個 query 跟計算累積營收很像,必須重複參考同個 table,接著用 W2.rn <= W1.rn 類似技巧整理出最後要的東西。

2012年10月28日 星期日

Argo (2012 film)


《大河灣》這樣描寫飛機,人已經到達目的地,但心還在原地。


每次出入境都有這種感覺,很緊張,離開馬來西亞讓我最緊張,害怕有壞人偷放毒品到我衣服口袋,馬來西亞運毒是唯一死刑,不是鬧著玩的。我知道那種屏息的感覺,喵的,就只能拼命裝龜孫子,祈禱不要被找到毒品。


記得把呼吸急促寫進去

[SQL] CCC研究


Definition: http://en.wikipedia.org/wiki/Cash_conversion_cycle

CCC = Inventory conversion period + Receivables conversion period - Payables conversion period
  • Inventory conversion period = Avg. Inventory / (COGS / 365)
  • Receivables conversion period = Avg. Accounts Receivable / (Credit Sales / 365)
  • Payables conversion period = Avg. Accounts Payable / (Purchases / 365)


怎麼用 SQL 取出 Inventory conversion period 呢?

-- Average Inventory
select A.activity_date, avg(B.number) as avg_inventory from
(
    select max(report_date), activity_date, number from BalanceSheet
    where
        report_type = 'C'
        and stock_code = '1101'
        and item = '存 貨'
    group by activity_date
) as A,
(
    select max(report_date), activity_date, number from BalanceSheet
    where
        report_type = 'C'
        and stock_code = '1101'
        and item = '存 貨'
    group by activity_date
) as B
where B.activity_date <= A.activity_date
and julianday(A.activity_date) <= julianday(B.activity_date, '+3 month')
group by A.activity_date

-- COGS
select max(report_date), activity_date, number from IncomeStmt
where
    report_type = 'C'
    and stock_code = '1101'
    and item = '營業成本合計'
group by activity_date

-- Annual Adjusted COGS
select
    max(report_date),
    activity_date,
    case
        when strftime('%m', activity_date) = '03' then number * 4/1
        when strftime('%m', activity_date) = '06' then number * 4/2
        when strftime('%m', activity_date) = '09' then number * 4/3
        else number
    end as cogs
from IncomeStmt
where
    report_type = 'C'
    and stock_code = '1101'
    and item = '營業成本合計'
group by activity_date

-- Inventory conversion period (days inventory outstanding)
select activity_date, dio from
(
    select
        C.activity_date,
        C.avg_inventory / D.cogs * 365 as dio
    from
    (
        -- Average Inventory
        select A.activity_date, A.number, avg(B.number) as avg_inventory from
        (
            select max(report_date), activity_date, number from BalanceSheet
            where
                report_type = 'C'
                and stock_code = '1101'
                and item = '存 貨'
            group by activity_date
        ) as A,
        (
            select max(report_date), activity_date, number from BalanceSheet
            where
                report_type = 'C'
                and stock_code = '1101'
                and item = '存 貨'
            group by activity_date
        ) as B
        where B.activity_date <= A.activity_date
        and julianday(A.activity_date) <= julianday(B.activity_date, '+3 month')
        group by A.activity_date
    ) as C,
    (
        --COGS
        select
            max(report_date),
            activity_date,
            case
                when strftime('%m', activity_date) = '03' then number * 4/1
                when strftime('%m', activity_date) = '06' then number * 4/2
                when strftime('%m', activity_date) = '09' then number * 4/3
                else number
            end as cogs
        from IncomeStmt
        where
            report_type = 'C'
            and stock_code = '1101'
            and item = '營業成本合計'
        group by activity_date
    ) as D
    where C.activity_date = D.activity_date
)
where dio is not null
order by activity_date



怎麼用 SQL 取出 Receivables conversion period 呢?

-- Receivables
select activity_date, sum(number) as receivable from
(
    select activity_date, item, number, max(report_date) from BalanceSheet
    where
        report_type = 'C'
        and stock_code = '1101'
        and item like '%應收%'
    group by activity_date, item
)
group by activity_date

-- Average Receivables
select A.activity_date, avg(B.receivable) as avg_receivable from
(
    select activity_date, sum(number) as receivable from
    (
        select activity_date, item, number, max(report_date) from BalanceSheet
        where
            report_type = 'C'
            and stock_code = '1101'
            and item like '%應收%'
        group by activity_date, item
    )
    group by activity_date
) as A,
(
    select activity_date, sum(number) as receivable from
    (
        select activity_date, item, number, max(report_date) from BalanceSheet
        where
            report_type = 'C'
            and stock_code = '1101'
            and item like '%應收%'
        group by activity_date, item
    )
    group by activity_date
) as B
where B.activity_date <= A.activity_date
and julianday(A.activity_date) <= julianday(B.activity_date, '+3 month')
group by A.activity_date

-- Receivables conversion period
select activity_date, dso from
(
    select
        C.activity_date,
        C.avg_receivables / D.revenue * 365 as dso
    from
    (
        -- Average Receivables
        select A.activity_date, avg(B.receivables) as avg_receivables from
        (
            select activity_date, sum(number) as receivables from
            (
                select activity_date, item, number, max(report_date) from BalanceSheet
                where
                    report_type = 'C'
                    and stock_code = '1101'
                    and item like '%應收帳款%'
                group by activity_date, item
            )
            group by activity_date
        ) as A,
        (
            select activity_date, sum(number) as receivables from
            (
                select activity_date, item, number, max(report_date) from BalanceSheet
                where
                    report_type = 'C'
                    and stock_code = '1101'
                    and item like '%應收帳款%'
                group by activity_date, item
            )
            group by activity_date
        ) as B
        where B.activity_date <= A.activity_date
        and julianday(A.activity_date) <= julianday(B.activity_date, '+3 month')
        group by A.activity_date    
    ) as C,
    (
        -- Revenue
        select
            max(report_date),
            activity_date,
            case
                when strftime('%m', activity_date) = '03' then number * 4/1
                when strftime('%m', activity_date) = '06' then number * 4/2
                when strftime('%m', activity_date) = '09' then number * 4/3
                else number
            end as revenue
        from IncomeStmt
        where
            report_type = 'C'
            and stock_code = '1101'
            and item = '營業收入合計'
        group by activity_date
    ) as D
    where C.activity_date = D.activity_date
)
where dso is not null
order by activity_date



怎麼取出 Payables conversion period?

-- Average Payables
select A.activity_date, avg(B.receivable) as avg_receivable from
(
    select activity_date, sum(number) as receivable from
    (
        select activity_date, item, number, max(report_date) from BalanceSheet
        where
            report_type = 'C'
            and stock_code = '1101'
            and item like '%應付%'
        group by activity_date, item
    )
    group by activity_date
) as A,
(
    select activity_date, sum(number) as receivable from
    (
        select activity_date, item, number, max(report_date) from BalanceSheet
        where
            report_type = 'C'
            and stock_code = '1101'
            and item like '%應付%'
        group by activity_date, item
    )
    group by activity_date
) as B
where B.activity_date <= A.activity_date
and julianday(A.activity_date) <= julianday(B.activity_date, '+3 month')
group by A.activity_date

-- Payables conversion period
select activity_date, dpo from
(
    select
        C.activity_date,
        C.avg_payables / D.cogs * 365 as dpo
    from
    (
        -- Average Payables
        select A.activity_date, avg(B.payables) as avg_payables from
        (
            select activity_date, sum(number) as payables from
            (
                select activity_date, item, number, max(report_date) from BalanceSheet
                where
                    report_type = 'C'
                    and stock_code = '1101'
                    and item like '%應付帳款%'
                group by activity_date, item
            )
            group by activity_date
        ) as A,
        (
            select activity_date, sum(number) as payables from
            (
                select activity_date, item, number, max(report_date) from BalanceSheet
                where
                    report_type = 'C'
                    and stock_code = '1101'
                    and item like '%應付帳款%'
                group by activity_date, item
            )
            group by activity_date
        ) as B
        where B.activity_date <= A.activity_date
        and julianday(A.activity_date) <= julianday(B.activity_date, '+3 month')
        group by A.activity_date    
    ) as C,
    (
        --COGS
        select
            max(report_date),
            activity_date,
            case
                when strftime('%m', activity_date) = '03' then number * 4/1
                when strftime('%m', activity_date) = '06' then number * 4/2
                when strftime('%m', activity_date) = '09' then number * 4/3
                else number
            end as cogs
        from IncomeStmt
        where
            report_type = 'C'
            and stock_code = '1101'
            and item = '營業成本合計'
        group by activity_date
    ) as D
    where C.activity_date = D.activity_date
)
where dpo is not null
order by activity_date



Hence, CCC

select A.activity_date, A.dio + B.dso - C.dpo as ccc from
(
    select activity_date, dio from
    (
        select
            C.activity_date,
            C.avg_inventory / D.cogs * 365 as dio
        from
        (
            -- Average Inventory
            select A.activity_date, A.number, avg(B.number) as avg_inventory from
            (
                select max(report_date), activity_date, number from BalanceSheet
                where
                    report_type = 'C'
                    and stock_code = '1101'
                    and item = '存 貨'
                group by activity_date
            ) as A,
            (
                select max(report_date), activity_date, number from BalanceSheet
                where
                    report_type = 'C'
                    and stock_code = '1101'
                    and item = '存 貨'
                group by activity_date
            ) as B
            where B.activity_date <= A.activity_date
            and julianday(A.activity_date) <= julianday(B.activity_date, '+3 month')
            group by A.activity_date
        ) as C,
        (
            --COGS
            select
                max(report_date),
                activity_date,
                case
                    when strftime('%m', activity_date) = '03' then number * 4/1
                    when strftime('%m', activity_date) = '06' then number * 4/2
                    when strftime('%m', activity_date) = '09' then number * 4/3
                    else number
                end as cogs
            from IncomeStmt
            where
                report_type = 'C'
                and stock_code = '1101'
                and item = '營業成本合計'
            group by activity_date
        ) as D
        where C.activity_date = D.activity_date
    )
    where dio is not null
    order by activity_date
) as A,
(
    select activity_date, dso from
    (
        select
            C.activity_date,
            C.avg_receivables / D.revenue * 365 as dso
        from
        (
            -- Average Receivables
            select A.activity_date, avg(B.receivables) as avg_receivables from
            (
                select activity_date, sum(number) as receivables from
                (
                    select activity_date, item, number, max(report_date) from BalanceSheet
                    where
                        report_type = 'C'
                        and stock_code = '1101'
                        and item like '%應收帳款%'
                    group by activity_date, item
                )
                group by activity_date
            ) as A,
            (
                select activity_date, sum(number) as receivables from
                (
                    select activity_date, item, number, max(report_date) from BalanceSheet
                    where
                        report_type = 'C'
                        and stock_code = '1101'
                        and item like '%應收帳款%'
                    group by activity_date, item
                )
                group by activity_date
            ) as B
            where B.activity_date <= A.activity_date
            and julianday(A.activity_date) <= julianday(B.activity_date, '+3 month')
            group by A.activity_date     
        ) as C,
        (
            -- Revenue
            select
                max(report_date),
                activity_date,
                case
                    when strftime('%m', activity_date) = '03' then number * 4/1
                    when strftime('%m', activity_date) = '06' then number * 4/2
                    when strftime('%m', activity_date) = '09' then number * 4/3
                    else number
                end as revenue
            from IncomeStmt
            where
                report_type = 'C'
                and stock_code = '1101'
                and item = '營業收入合計'
            group by activity_date
        ) as D
        where C.activity_date = D.activity_date
    )
    where dso is not null
    order by activity_date
) as B,
(
    select activity_date, dpo from
    (
        select
            C.activity_date,
            C.avg_payables / D.cogs * 365 as dpo
        from
        (
            -- Average Payables
            select A.activity_date, avg(B.payables) as avg_payables from
            (
                select activity_date, sum(number) as payables from
                (
                    select activity_date, item, number, max(report_date) from BalanceSheet
                    where
                        report_type = 'C'
                        and stock_code = '1101'
                        and item like '%應付帳款%'
                    group by activity_date, item
                )
                group by activity_date
            ) as A,
            (
                select activity_date, sum(number) as payables from
                (
                    select activity_date, item, number, max(report_date) from BalanceSheet
                    where
                        report_type = 'C'
                        and stock_code = '1101'
                        and item like '%應付帳款%'
                    group by activity_date, item
                )
                group by activity_date
            ) as B
            where B.activity_date <= A.activity_date
            and julianday(A.activity_date) <= julianday(B.activity_date, '+3 month')
            group by A.activity_date     
        ) as C,
        (
            --COGS
            select
                max(report_date),
                activity_date,
                case
                    when strftime('%m', activity_date) = '03' then number * 4/1
                    when strftime('%m', activity_date) = '06' then number * 4/2
                    when strftime('%m', activity_date) = '09' then number * 4/3
                    else number
                end as cogs
            from IncomeStmt
            where
                report_type = 'C'
                and stock_code = '1101'
                and item = '營業成本合計'
            group by activity_date
        ) as D
        where C.activity_date = D.activity_date
    )
    where dpo is not null
    order by activity_date
) as C
where A.activity_date = B.activity_date
and B.activity_date = C.activity_date
and C.activity_date = A.activity_date

2012年10月27日 星期六

[SQL] 營業外收支研究


可能還要留意會計科目變化

select activity_date, non_operating_income_ratio from
(
    select activity_date, non_operating_income_ratio, max(report_date) from
    (
        select
            A.activity_date,
            A.number / B.number as non_operating_income_ratio,
            A.report_date
        from IncomeStmt as A
        inner join
        IncomeStmt as B
        on A.stock_code = B.stock_code
            and A.activity_date = B.activity_date
            and A.item in ('營業外收入合計', '營業外收入及利益')
            and B.item in ('繼續營業部門稅前淨利(淨損)', '繼續營業單位稅前淨利(淨損)')
            and A.report_type = 'C'
            and B.report_type = 'C'
            and A.stock_code = '2002'
    )
    where non_operating_income_ratio is not null
    group by activity_date
    order by activity_date
)