string sql = "";
sql += @"
select
- CONVERT(CHAR(19), a1.ps_date, 20) as ps_date, a2.ct_code, a2.ct_name+'('+a5.ct_phone+')' as ct_name, a1.ps_prname, cast(a1.ps_qty as int) ps_qty,
- cast(
- a1.ps_up
- +
- case when (
- isnull(
- (
- isnull((select sum(ps_up * ps_qty) from ps where pe_id= a1.pe_id and ps_prcode not in (select pr_code from pr where kind_code= '1100') and ps_prcode not in (select pr_code from pr where kind_code= '1099')), 0)
- +
- (
- select
- sum(ps_up * ps_qty)/(select count(pe_id) from ps where pe_id= a1.pe_id and ps_prcode not in (select pr_code from pr where kind_code= '1100') and ps_prcode in (select pr_code from pr where kind_code= '1099'))
- from
- ps
- where
- ps_prcode in (select pr_code from pr where kind_code= '1100') and pe_id= a1.pe_id
- )
- )
- , 0)
- ) < 0 then
- isnull(
- (
- isnull((select sum(ps_up * ps_qty) from ps where pe_id= a1.pe_id and ps_prcode not in (select pr_code from pr where kind_code= '1100') and ps_prcode not in (select pr_code from pr where kind_code= '1099')), 0)
- +
- (
- select
- sum(ps_up * ps_qty)/(select count(pe_id) from ps where pe_id= a1.pe_id and ps_prcode not in (select pr_code from pr where kind_code= '1100') and ps_prcode in (select pr_code from pr where kind_code= '1099'))
- from
- ps
- where
- ps_prcode in (select pr_code from pr where kind_code= '1100') and pe_id= a1.pe_id
- )
- )
- , 0) else
- 0 end
- as int) as pe_bamt,
- a1.pe_id, a1.ps_no, isnull(a3.is_earn, 0) as is_earn
+ CONVERT(CHAR(19), a1.ps_date, 20) as ps_date,
+ a2.ct_code,
+ a2.ct_name + '(' + a5.ct_phone + ')' as ct_name,
+ a1.ps_prname,
+ cast(a1.ps_qty as int) ps_qty,
+ cast(
+ a1.ps_up
+ +
+ case when (
+ isnull(
+ (
+ isnull((select sum(ps_up * ps_qty) from ps where pe_id = a1.pe_id and ps_prcode not in (select pr_code from pr where kind_code = '1100') and ps_prcode not in (select pr_code from pr where kind_code in ('1099', '1105', '1106', '1107', '1108'))), 0)
+ +
+ (
+ select
+ sum(ps_up * ps_qty) / (select count(pe_id) from ps where pe_id = a1.pe_id and ps_prcode not in (select pr_code from pr where kind_code = '1100') and ps_prcode in (select pr_code from pr where kind_code in ('1099', '1105', '1106', '1107', '1108')))
+ from
+ ps
+ where
+ ps_prcode in (select pr_code from pr where kind_code = '1100') and pe_id = a1.pe_id
+ )
+ )
+ , 0)
+ ) < 0 then
+ isnull(
+ (
+ isnull((select sum(ps_up * ps_qty) from ps where pe_id = a1.pe_id and ps_prcode not in (select pr_code from pr where kind_code = '1100') and ps_prcode not in (select pr_code from pr where kind_code in ('1099', '1105', '1106', '1107', '1108'))), 0)
+ +
+ (
+ select
+ sum(ps_up * ps_qty) / (select count(pe_id) from ps where pe_id = a1.pe_id and ps_prcode not in (select pr_code from pr where kind_code = '1100') and ps_prcode in (select pr_code from pr where kind_code in ('1099', '1105', '1106', '1107', '1108')))
+ from
+ ps
+ where
+ ps_prcode in (select pr_code from pr where kind_code = '1100') and pe_id = a1.pe_id
+ )
+ )
+ , 0) else
+ 0 end
+ as int) as pe_bamt,
+ a1.pe_id,
+ a1.ps_no,
+ isnull(a3.is_earn, 0) as is_earn
from
- ps a1
- join pe a2
- on a1.pe_id= a2.pe_id and a2.ct_code != ''
- left join disposable_earn_point_status a3
- on a3.pe_id= a1.pe_id and a3.ps_no= a1.ps_no
- join ac a4
- on a4.pe_id= a1.pe_id and a4.ac_fncd2 not in ('01', '00')
- join ct a5
- on a5.ct_code= a2.ct_code
+ ps a1
+ join pe a2 on a1.pe_id = a2.pe_id and a2.ct_code != ''
+ left join disposable_earn_point_status a3 on a3.pe_id = a1.pe_id and a3.ps_no = a1.ps_no
+ join ac a4 on a4.pe_id = a1.pe_id and a4.ac_fncd2 not in ('01', '00')
+ join ct a5 on a5.ct_code = a2.ct_code
where
- a1.ps_prcode in (
- select pr_code from pr where kind_code= '1099'
- )
- and a1.ps_date between '" + selected + @"' and dateadd(day, 1, '" + selected + @"')
+ a1.ps_prcode in (
+ select pr_code from pr where kind_code in ('1099', '1105', '1106', '1107', '1108')
+ )
+ and a1.ps_date between '" + selected + @"' and dateadd(day, 1, '" + selected + @"')
order by
- a1.pe_id desc
+ a1.pe_id desc
";
SqlCommand cmd_select = new SqlCommand(sql, conn);