sql SQL Server和Oracle SQL的SQL语法的一些示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql SQL Server和Oracle SQL的SQL语法的一些示例相关的知识,希望对你有一定的参考价值。

/* SQL Server example 1 (datepart, convert) */
select convert(varchar(10), LM.CURVEDATE, 121) as TRANSACTIONDATE, 
datepart(month, LM.CURVEDATE) as TRANSACTIONMTH, 
datepart(year, LM.CURVEDATE) as TRANSACTIONYEAR, 
convert(date, '01-' + LM.TENORTEXT) as DELIVERYDATE, 
datepart(month, convert(date, '01-' + LM.TENORTEXT)) as DELIVERYMTH, 
datepart(year, convert(date, '01-' + LM.TENORTEXT)) as DELIVERYYEAR, 
LM.MARKET as MARKET, LM.COMPONENT as COMPONENT, LM.price as PRICE 
from V_componentLimprices LM where LM.MARKET = 'NYMEX'  
and LM.COMPONENT in ('HO') 
and LM.TIMESET = 'Mid'  
and LM.PRICEVIEW = 'Monthly Prices'  
and convert(varchar(10), LM.CURVEDATE, 121) >= '2011-01-01' 
and convert(varchar(10), LM.CURVEDATE, 121) <= '2017-10-10 15:00:08' 
order by convert(varchar(10), LM.CURVEDATE, 121),  convert(date, '01-' + LM.TENORTEXT)

/* SQL Server example 2 (case when) */
select convert(datetime, convert(varchar(8), a.CurveDateKey), 112) as CurveDate, 
        convert(datetime, convert(varchar(8), a.ForwardDateKey), 112) as DelDate, 
        convert(datetime, convert(varchar(8), a.ForwardMonthDateKey), 112) as DelMo,
        
        b.PlantName, c.UnitName, 
        (case when (a.HourKey > '6' and a.HourKey < '23') then 'Period7x16' else 'Period7x8' end) as Period,
        sum(a.AmountGeneration) as TotalGen
from dbo.BIFactGenerationForecastDetail a
join dbo.BIDimPlant b on a.PlantKey  = b.PlantKey
join dbo.BIDimGenCompNameRef c on a.UnitKey = c.GenUnitKey
where a.CurveDateKey = '20160111'
and a.NGEDateKey = '20160111'
and a.UnitKey in (36,37,47,48,65,66,67,68)
and convert(datetime, convert(varchar(8), a.ForwardDateKey), 112) >= '01FEB2016'
and convert(datetime, convert(varchar(8), a.ForwardDateKey), 112) <= '31DEC2018'
group by a.CurveDateKey, a.ForwardMonthDateKey, a.ForwardDateKey, 
 b.PlantName, c.UnitName, (case when (a.HourKey > '6' and a.HourKey < '23') then 'Period7x16' else 'Period7x8' end)
order by a.CurveDateKey, a.ForwardMonthDateKey, a.ForwardDateKey, b.PlantName, c.UnitName

/* SQL Server example 3 (year, month, day) */
Select REPORT_DATE, BOOK, MARKET_1, MARKET_2, COMPONENT_1, COMPONENT_2, COMMODITY_ATTRIB_1, TRADE_BUCKET,
	    (Case When POW_SEG = 'PEAK' Then 'ONPEAK'
            When POW_SEG = 'RTC' Then 'MID' Else 'OFFPEAK' End) As PKOP,
       YEAR(DEL_MONTH) As Year, MONTH(DEL_MONTH) As Month, DAY(DEL_MONTH) As Day, EXPOSURE_TYPE2, STRIKE,
      (Case When PUT_CALL = 'CALL' Then 1 When PUT_CALL = 'PUT' Then 2 Else 3 End) As OPT_TYPE,
       SUM(DELTA_VOLUME) As DeltaVol_1, SUM(DELTA_VOLUME_2) As DeltaVol_2,
       SUM(VOLUME_1) As TotalVol_1, SUM(VOLUME_2) As TotalVol_2, 
		 SUM(TRADE_PRICE*VOLUME_1) As Premium_1, SUM(TRADE_PRICE*VOLUME_2) As Premium_2
From Risk_MB_Archive.dbo.RA_MTM_DAILY_NRG_HISTORY
Where REPORT_DATE = '20160205'
  And DEL_MONTH > '20160201'
  And BOOK In ('ERLD', 'ERDS', 'ERSS')
  And REALIZED_UNREALIZED = 'UNREALIZED'
  And TRANSACTION_TYPE <> 'FTR'
  and MARKET_2 = 'FX'
  AND MARKET_1 NOT IN ('CTFS', 'DLBLCL', 'HPL', 'KM TJ', 'NAKX', 'NOKX', 'STK', 'STKX', 'TXCOAL')
  AND COMMODITY_1 <> 'ANCILLARY'
  AND EXPOSURE_TYPE2 <> 'OPTION'
Group by REPORT_DATE, BOOK, MARKET_1, MARKET_2, COMPONENT_1, COMPONENT_2, COMMODITY_ATTRIB_1, TRADE_BUCKET,
        (Case When POW_SEG = 'PEAK' Then 'ONPEAK' When POW_SEG = 'RTC' Then 'MID' Else 'OFFPEAK' End),
         YEAR(DEL_MONTH), MONTH(DEL_MONTH), DAY(DEL_MONTH), EXPOSURE_TYPE2, STRIKE,
        (Case When PUT_CALL = 'CALL' Then 1 When PUT_CALL = 'PUT' Then 2 Else 3 End)
Order by REPORT_DATE, BOOK, MARKET_1, COMPONENT_1, COMPONENT_2, YEAR, MONTH, DAY, PKOP

/* Oracle SQL example 1 (max, min, avg, round) */
select c5_mkt, c6_cdy, c7_dmo,
       max(hr_up_chk) max_hr_up_chk, min(hr_up_chk) min_hr_up_chk,
       max(hr_dn_chk) max_hr_dn_chk, min(hr_dn_chk) min_hr_dn_chk,
       max(gas_up_chk) max_gas_up_chk, min(gas_up_chk) min_gas_up_chk,
       max(gas_dn_chk) max_gas_dn_chk, min(gas_dn_chk) min_gas_dn_chk,
       avg(hr_up_chk) avg_hr_up_chk,avg(hr_dn_chk) avg_hr_dn_chk,
       avg(gas_up_chk) avg_gas_up_chk,avg(gas_dn_chk) avg_gas_dn_chk
 from (
    select a.*, round(a.c13_hrup1 / a.c11_ng_price -1 - a.c12_hr,2) as hr_up_chk, 
            round(a.c14_hrdn1 / a.c11_ng_price +1 - a.c12_hr,2) as hr_dn_chk,
            round(A.C15_gasup_5 / a.c12_hr - 0.5 - a.c11_ng_price, 2) as gas_up_chk,
            round(A.C16_GASDN_5 / a.c12_hr + 0.5 - a.c11_ng_price, 2) as gas_dn_chk
    from risk.z_gc_hr_hourly_2015 a
    ) T
where (hr_up_chk <> 0
OR hr_dn_chk <>0
or gas_up_chk <> 0
or gas_dn_chk <>0)
and to_char(c8_date,'MM') <> '11'
group by c5_mkt, c6_cdy, c7_dmo
order by c5_mkt, c6_cdy, c7_dmo

/* Oracle SQL example 2 (case when...then, extract, dual) */
select * from 
(
select a.correlation_id, b.correlation_nm, c.mkt as side1_mkt, c.comp as side1_comp, 
          case when e.fillpatid = 1 then 'rtc'
          when e.fillpatid = 2 then 'peak'
          when e.fillpatid = 5 then 'offpeak' end as side1_seg,
       d.mkt as side2_mkt, d.comp as side2_comp, 
         case when f.fillpatid = 1 then 'rtc'
         when f.fillpatid = 2 then 'peak'
         when f.fillpatid = 5 then 'offpeak' end as side2_seg, 
       e.syr as fwd_yr_side1, e.smo as fwd_mnth_side1, 
       f.syr as fwd_yr_side2, f.smo as fwd_mnth_side2, a.correlation_amt as corr
from pst_correlation a
left join pst_correlationids b
on a.correlation_id = b.correlation_id
left join pst_indexdesc c
on a.indexid1 = c.id
left join pst_indexdesc d
on a.indexid2 = d.id
left join pst_bucketids e
on a.bucketid1 = e.bucketid
left join pst_bucketids f
on a.bucketid2 = f.bucketid
where (a.correlation_id = 295
and e.syr = 2018 
and c.mkt in ('ERCOT', 'CAISO', 'NYISO', 'NEISO', 'PJM', 'NYMEX', 'TRANSC', 'TETCO')
and d.mkt in ('ERCOT', 'CAISO', 'NYISO', 'NEISO', 'PJM', 'NYMEX', 'TRANSC', 'TETCO')
and e.fillpatid in (1,2)
and f.fillpatid in (1,2)) 
or (a.correlation_id = 295
and c.mkt in ('ERCOT', 'CAISO', 'NYISO', 'NEISO', 'PJM', 'NYMEX', 'TRANSC', 'TETCO')
and d.mkt in ('ERCOT', 'CAISO', 'NYISO', 'NEISO', 'PJM', 'NYMEX', 'TRANSC', 'TETCO')
and e.fillpatid in (1,2)
and f.fillpatid in (1,2)
and e.syr = (select extract(year from sysdate) from dual)
and e.smo >= (select extract(month from sysdate) from dual))
order by side1_mkt, side1_comp, side2_mkt, side2_comp, fwd_yr_side1, fwd_mnth_side1, fwd_yr_side2, fwd_mnth_side2
)
where fwd_yr_side1 = 2018
and fwd_mnth_side1 = 8
and fwd_yr_side2 = 2018
and fwd_mnth_side2 = 8

/* Oracle SQL example 3 (union all, trunc, dual, level, connect by level) */
select *
from(
select risk.z_dy_ercot_d_hedges_3.*, c30_market_value as adj_market_val, 
c31_trade_value as adj_trade_val, c15_volume as adj_volume, 
c1_del_month as delivery_month, c2_delivery_date as delivery_date
from risk.z_dy_ercot_d_hedges_3
where c2_delivery_date >= c1_del_month

union all

select * from
(select risk.z_dy_ercot_d_hedges_3.*, c30_market_value/c37_days_in_mon as adj_market_val, 
c31_trade_value/c37_days_in_mon as adj_trade_val, c15_volume/c37_days_in_mon as adj_volume
from risk.z_dy_ercot_d_hedges_3
where c2_delivery_date < c1_del_month) a
full outer join
(select trunc(sysdate, 'MM') as delivery_month, (trunc(sysdate, 'MM')-1) + level as delivery_date
from dual
connect by level <= last_day(trunc(sysdate, 'dd')) - (trunc(sysdate, 'MM')-1)) b
on a.c1_del_month = b.delivery_month
)

以上是关于sql SQL Server和Oracle SQL的SQL语法的一些示例的主要内容,如果未能解决你的问题,请参考以下文章

SQL SERVER和ORACLE中SQL语句一样吗?

SQL 在 SQL Server 和 Oracle 中返回固定数据

sql server与oracle链接问题

从Oracle到SQL Server和从SQL Server到Oracle的迁移指南

SQL Server 和 Oracle 以及 MySQL 都有哪些区别

sql server的sql语句和oracle的sql语句有啥不同吗?