Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- select c.id company_id, c.name company, DATE(receipt_date) receipt, count(*) count from act
- join company c on c.id = act.company_id
- where
- act.is_delete = 0
- group by act.company_id, receipt
- select c.id company_id, c.name company, DATE(issue_date) issue, count(*) count from act
- join company c on c.id = act.company_id
- where
- act.is_delete = 0
- group by act.company_id, issue
- select company_id,
- date(receipt_date) as "dt",
- count(*) as "qty"
- into #receipt_total
- from act
- where is_delete = 0
- group by company_id,
- "dt"
- ;
- -- indexes
- select company_id,
- date(receipt_date) as "dt",
- count(*) as "qty"
- into #issue_total
- from act
- where is_delete = 0
- group by company_id,
- "dt"
- ;
- -- indexes
- select isnull(t1.company_id, t2.company_id) as "company_id",
- isnull(t1.dt, t2.dt) as "dt",
- isnull(t1.qty, 0) as "qty_receipt",
- isnull(t2.qty, 0) as "qty_issue"
- into #result
- from #receipt_total as t1
- full join #issue_total as t2 on t2.company_id = t1.company_id
- and t2.dt = t1.dt
- ;
- select t1.company_id,
- t2.company_name,
- t1.dt,
- t1.qty_receipt,
- t1.qty_issue
- from #result as t1
- join company as t2 on t2.id = t1.company_id
- ;
- select company_id,
- 'receipt' as "rtype",
- date(receipt_date) as "dt",
- count(*) as "qty"
- into #receipt_total
- from act
- where is_delete = 0
- group by company_id,
- "dt"
- ;
- -- indexes
- select company_id,
- 'issue' as "rtype",
- date(receipt_date) as "dt",
- count(*) as "qty"
- into #issue_total
- from act
- where is_delete = 0
- group by company_id,
- "dt"
- ;
- -- indexes
- select *
- into #total_data
- from #receipt_total
- union all
- select *
- from #issue_total
- ;
- select company_id,
- dt,
- sum(case when rtype = 'receipt'
- then qty
- end) as "qty_receipt",
- sum(case when rtype = 'issue'
- then qty
- end) as "qty_issue"
- into #result
- from #total_data
- ;
- select t1.company_id,
- t2.company_name,
- t1.dt,
- t1.qty_receipt,
- t1.qty_issue
- from #result as t1
- join company as t2 on t2.id = t1.company_id
- ;
Add Comment
Please, Sign In to add comment