Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何解决同一个月在data stage或sql中的速率是否不同?_Sql - Fatal编程技术网

如何解决同一个月在data stage或sql中的速率是否不同?

如何解决同一个月在data stage或sql中的速率是否不同?,sql,Sql,输入: SDO开始月底费率 1月10日至3月10日 101二月二十日三月二十日 101五月至七月三十日 输出: SDO开始月底费率 101五月至七月三十日 我需要拒绝在同一时期内具有不同速率的整个列,并需要获得具有唯一速率的列。以下是MSSQL Server中编写的标准逻辑。您可以根据DBMS使用相同的逻辑- WITH your_table(SDO,Startmonth,endmonth,rate) AS ( SELECT 101,'jan','mar',10 UNION ALL SELECT

输入:

SDO开始月底费率 1月10日至3月10日 101二月二十日三月二十日 101五月至七月三十日 输出:

SDO开始月底费率 101五月至七月三十日
我需要拒绝在同一时期内具有不同速率的整个列,并需要获得具有唯一速率的列。

以下是MSSQL Server中编写的标准逻辑。您可以根据DBMS使用相同的逻辑-

WITH your_table(SDO,Startmonth,endmonth,rate)
AS
(
SELECT 101,'jan','mar',10 UNION ALL
SELECT 101,'Feb','mar',20 UNION ALL
SELECT 101,'may','jul',30
)
,st_month 
AS(
    SELECT SDO, Startmonth
    FROM your_table
    GROUP BY SDO,Startmonth
    HAVING COUNT(DISTINCT Startmonth) <> COUNT(Startmonth)
)
,end_month AS (
    SELECT SDO, endmonth
    FROM your_table
    GROUP BY SDO,endmonth
    HAVING COUNT(DISTINCT endmonth) <> COUNT(endmonth)
)

SELECT A.* 
FROM your_table A
LEFT JOIN st_month B ON A.SDO = B.SDO
    AND A.Startmonth = B.Startmonth
LEFT JOIN end_month C ON A.SDO = C.SDO
    AND A.endmonth = C.endmonth
WHERE B.SDO IS NULL AND C.SDO IS NULL

如果正在使用Sqlserver,请尝试以下操作:

Select *
from
(
    select 
    case when tab2.SDO is null then tab1.SDO else tab2.SDO end as SDO,
    case when tab2.Startmonth is null then tab1.Startmonth else tab2.Startmonth end as Startmonth,
    case when tab2.endmonth is null then tab1.endmonth else tab2.endmonth end as endmonth,
    case when tab2.rate is null then tab1.rate else tab2.rate end as rate
    from your_table tab1
    left join your_table tab2
    on (tab2.Startmonth > tab1.Startmonth
        and tab2.Startmonth < tab1.endmonth ) or
        tab2.Startmonth is null
) as tab
group by SDO,Startmonth,endmonth,rate
having count(*) = 1

示例

假设月份列实际上是日期,则可以使用“不存在”:

这使用了基本的重叠逻辑,即如果两个间隔在另一端或之前开始,则两个间隔重叠


=表示起始月和结束月均包含在该期间内。如果不包括一个或两个月末月份,则可以调整代码。

您使用的是哪种产品?SQL只是一种查询语言,而不是特定数据库产品的名称。请为您正在使用的数据库产品添加。如果你将月份存储为数字而不是字符串,那会容易得多。我不确定你是否理解你的问题,你的数据中有3个不同的周期sample@Cid:我想巴尔加夫想要显示所有有效期。只有当不存在具有其他速率的重叠时段时,时段才有效。@bhargav:输入是表中的内容,输出是所需的查询结果?你试过什么?你被困在哪里?你在找算法吗?或者您是否有一个算法,但不知道如何将其转换为SQL?或者你有一个查询,但它没有按预期工作?
select t.*
from t
where not exists (select 1
                  from t t2
                  where t2.startmonth <= t.endmonth and
                        t2.endmonth >= t.startmonth and
                        t2.rate <> t.rate
                 );