/**

 */

select *
from Vendor
where cVenCode = '一冷开利';

select *
from UFDATA_001_2017.dbo.Customer
where cCusCode = 'GLDKJGFYXGSBJFGS';

select *
from CM_List;

select *
from CM_Contract_Main
where strContractID = 'S24057-F1';
select *
from CM_Contract_A
where strContractID = 'S24057-F1';
select *
from CM_Contract_B
where strContractID = 'S24057-F1';
select *
from CM_Contract_C
where strContractID = 'S24057-F1';
select *
from CM_Contract
where strContractID = 'S24057-F1';



select *
from UFDATA_001_2017.dbo.CM_Contract
where GUID = '1B74DDAF-12FA-4676-A236-AFB3245ACB96'
   or strContractID = 'S23111ACRM';
select *
from UFDATA_001_2017.dbo.CM_Contract_B
where GUID = '1B74DDAF-12FA-4676-A236-AFB3245ACB96'
   or strContractID = 'S23111ACRM';



select *
from Person
where cPersonCode = '07';



/**
  * 根据合同ID,统计合同下所有商品信息
 */
select I.GUID,
       sum(isnull(I.dblQuantity, 0))    AS SumdblQuantity,
       sum(isnull(I.dblUntaxSum, 0))    AS SumdblUntaxSum,
       sum(isnull(I.dblSum, 0))         AS SumdblSum,
       sum(isnull(I.PieceNum, 0))       as SumdblPieceNum
from CM_Contract_Item_B I
where I.intEnd = 1
  and I.IntFlag <> 1 and strContractID='S24036ACNJ'
group by I.GUID;


select sum( decCount),sum(decNoRateMoney), sum(decRateMoney)
        from CM_ExecInterface
where cContractID = 'S24036ACNJ';


select *
from CM_ExecInterface left Join so_somain On so_somain.id=CM_ExecInterface.cOrderID where cContractID = 'S24036ACNJ';

/**
  order id 1000003696
 */
/**
  付款计划
 */
 select * from CM_Contract_Pay where strContractID = 'S24036ACNJ';



select * from UFDATA_001_2017.dbo.Inventory as I left join ComputationUnit as U on I.cComUnitCode=U.cComunitCode where I.cInvStd = 'KH29EZ050';


-- 计算查询的CPU使用率占比
DECLARE @init_sum_cpu_time int, @utilizedCpuCount int
SELECT @utilizedCpuCount = COUNT( * )
FROM sys.dm_os_schedulers
WHERE status = 'VISIBLE ONLINE'
SELECT @init_sum_cpu_time = SUM(cpu_time)
FROM sys.dm_exec_requests
WAITFOR DELAY '00:00:05'
SELECT CONVERT(DECIMAL(5,2),((SUM(cpu_time) - @init_sum_cpu_time) / (@utilizedCpuCount * 5000.00)) * 100) AS [CPU FROM Queries AS Percent of Total CPU Capacity]
FROM sys.dm_exec_requests;

-- 获取当前高CPU活动的查询
SELECT TOP 10 s.session_id,
              r.status,
              r.cpu_time,
              r.logical_reads,
              r.total_elapsed_time / (1000 * 60) 'Elaps M',
              SUBSTRING(st.TEXT, (r.statement_start_offset / 2) + 1,
                        ((CASE r.statement_end_offset
                              WHEN -1 THEN DATALENGTH(st.TEXT)
                              ELSE r.statement_end_offset
                              END - r.statement_start_offset) / 2) + 1) AS statement_text,
              s.program_name,
              s.last_request_end_time,
              s.login_time,
              r.open_transaction_count
FROM sys.dm_exec_sessions AS s
         JOIN sys.dm_exec_requests AS r ON r.session_id = s.session_id
         CROSS APPLY sys.Dm_exec_sql_text(r.sql_handle) AS st
WHERE r.session_id != @@SPID
ORDER BY r.cpu_time DESC;


--历史上占用大量 CPU 的查询
SELECT TOP 10 st.text AS batch_text,
              SUBSTRING(st.TEXT, (qs.statement_start_offset / 2) + 1, ((CASE qs.statement_end_offset WHEN - 1 THEN DATALENGTH(st.TEXT) ELSE qs.statement_end_offset END - qs.statement_start_offset) / 2) + 1) AS statement_text,
              (qs.total_worker_time / 1000) / qs.execution_count AS avg_cpu_time_ms,
              (qs.total_elapsed_time / 1000) / qs.execution_count AS avg_elapsed_time_ms,
              qs.total_logical_reads / qs.execution_count AS avg_logical_reads,
              (qs.total_worker_time / 1000) AS cumulative_cpu_time_all_executions_ms,
              (qs.total_elapsed_time / 1000) AS cumulative_elapsed_time_all_executions_ms
FROM sys.dm_exec_query_stats qs
         CROSS APPLY sys.dm_exec_sql_text(sql_handle) st
ORDER BY(qs.total_worker_time / qs.execution_count) DESC;



select cPOID,cInvCode, count(*) from IA_Subsidiary left join PO_Pomain on PO_Pomain.cPOID=IA_Subsidiary.cOrderCode where cPOID='0000004297' group by cPOID,cInvCode;

select GUID,strContractID,strContractName,strContractType,strParentID,strContractKind,strWay,strContractGrp, strContractDesc,strBisectionUnit,strContractOrderDate,strContractStartDate,strContractEndDate,strSetupPerson,strSetupDate,strInurePerson,strInureDate,strVaryPerson,dtVaryDate,strPersonID, dblTotalCurrency,dblExecCurrency,dblTotalQuantity,dblExecQuqantity  from CM_Contract_B where GUID ='A6E6ACE0-EA2A-433B-BC07-19BB8293D7A3';
                                                                                                                                                                                                                                                                                                                                                                  N'%运输费%' escape '' order by i1_0.ID desc limit 0,100;