select 
	BRANCH_NO2
	,COUNT(BRANCH_NO2) as countlist
from ehongdb.dbo.product_transferdata
where status in ('N','C') and (year(trans_date)>=2013) and datediff(day,trans_date,getdate())>1
group by BRANCH_NO2
order by BRANCH_NO2

select 
	PRODUCT_ID
	,PRODUCT_NAME
	,TRANS_NO
	,BRANCH_NO1
	,BRANCH_NO2
	,TRANS_DATE
	,datediff(day,trans_date,getdate()) as xdate 
from ehongdb.dbo.product_transferdata
where status in ('N','C') and (year(trans_date)>=2013) and datediff(day,trans_date,getdate())>1
order by BRANCH_NO2,trans_date

SELECT
	a.[SZONE_CODE]
	,b.PRODUCT_ID
	,b.PRODUCT_NAME
	,b.TRANS_NO
	,b.BRANCH_NO1
	,b.BRANCH_NO2
	,b.TRANS_DATE
	,[BR_NAME]
FROM [EHONGDB].[dbo].[BRANCH] a
	left outer join(
		select 
			PRODUCT_ID
			,PRODUCT_NAME
			,TRANS_NO
			,BRANCH_NO1
			,BRANCH_NO2
			,TRANS_DATE
			,datediff(day,trans_date,getdate()) as xdate 
		from ehongdb.dbo.product_transferdata
		where status in ('N','C') and (year(trans_date)>=2013) and datediff(day,trans_date,getdate())>1) b on (a.BR_ID like b.BRANCH_NO2)
where BR_ID != '%' and a.SZONE_CODE is not null

/****** Script for SelectTopNRows command from SSMS  ******/
SELECT
	[PS_ID]
	,[TI_NAME]		
	,[PS_FNAME]
	,[PS_LNAME]
	,[PS_PHONE]
	,[PS_MOBILE]
	,[CD_DATA]
	,[img_path]
FROM [EHONGDB].[dbo].[PERSON]
/*where PS_ID like '102152005320' or PS_PHONE like '' or PS_MOBILE like ''*/
where PS_ID like '102152005320'
/*where TI_NAME like 'นางสาว' and PS_FNAME like 'สุมาลัย' and PS_LNAME like 'อุดมรัตน์'*/

/****** Script for SelectTopNRows command from SSMS  ******/
SELECT
	a.[ct_no]
	,a.[ps_id]
	,a.[ps_name]
	,b.[TI_NAME]		
	,b.[PS_FNAME]
	,b.[PS_LNAME]
	,b.[PS_PHONE]
	,b.[PS_MOBILE]
	,b.[CD_DATA]
	,a.[ct_date]
	,a.[br_id]
	,a.[sbr_id]
	,a.[price]
	,a.[payamnt]
	,a.[due_amnt]
	,a.[due_count]
	,a.[product_id]
	,a.[product_name]
	,a.[serial_no]
	,a.[status]
	,a.[cnsvbarcode]
	,a.[cnsvrefcode]
FROM [EHONGDB].[dbo].[hps_ctmain] a
	left outer join(
		SELECT
			[PS_ID]
			,[TI_NAME]		
			,[PS_FNAME]
			,[PS_LNAME]
			,[PS_PHONE]
			,[PS_MOBILE]
			,[CD_DATA]
			,[img_path]
		FROM [EHONGDB].[dbo].[PERSON]) b on (a.ps_id=b.PS_ID)
where cnsvbarcode is not null and status like 'N'
order by ct_date desc

/****** Script for SelectTopNRows command from SSMS  ******/
SELECT
	a.ps_id
	,ps_name
	,COUNT(ps_name) as count
FROM [EHONGDB].[dbo].[hps_ctmain] a
	left outer join(
		SELECT
			[PS_ID]
			,[TI_NAME]		
			,[PS_FNAME]
			,[PS_LNAME]
			,[PS_PHONE]
			,[PS_MOBILE]
			,[CD_DATA]
			,[img_path]
		FROM [EHONGDB].[dbo].[PERSON]) b on (a.ps_id=b.PS_ID)
where cnsvbarcode is not null
group by ps_name,a.ps_id
order by count desc

/****** Script for SelectTopNRows command from SSMS  ******/
SELECT 
	[BR_ID]
	,[BR_NAME]
FROM [EHONGDB].[dbo].[BRANCH]

/****** Script for SelectTopNRows command from SSMS  ******/
SELECT
	a.[ct_no]
	,a.[ps_id]
	,a.[ps_name]
	,b.[TI_NAME]		
	,b.[PS_FNAME]
	,b.[PS_LNAME]
	,b.[PS_PHONE]
	,b.[PS_MOBILE]
	,b.[CD_DATA]
	,a.[ct_date]
	,a.[br_id]
	,a.[sbr_id]
	,a.[price]
	,a.[payamnt]
	,a.[due_amnt]
	,a.[due_count]
	,a.[product_id]
	,a.[product_name]
	,a.[serial_no]
	,a.[status]
	,a.[cnsvbarcode]
	,a.[cnsvrefcode]
FROM [EHONGDB].[dbo].[hps_ctmain] a
	left outer join(
		SELECT
			[PS_ID]
			,[TI_NAME]		
			,[PS_FNAME]
			,[PS_LNAME]
			,[PS_PHONE]
			,[PS_MOBILE]
			,[CD_DATA]
			,[img_path]
		FROM [EHONGDB].[dbo].[PERSON]) b on (a.ps_id=b.PS_ID)
where cnsvbarcode is not null and status like 'N' and(a.ps_id like '160135660' or )