我是SQL新手,主要使用Power BI中查询中的数据。
我根据Status将我的日期列拆分为SQL中的两个日期列。 (在状态发生变化的日子里)
我有一个SQL查询,它给我以下简单的结果
SERIAL NO ENTITY_CODE COMPANY_NAME STATUS TRANSACTION_DATE
AB:CD:EF:GH:IJ:KL FRANCIS MADICAL Activation 27-04-12 18:30
AB:CD:EF:GH:IJ:KL DEADPOOL FAST TRANSPORT Disconnection 27-12-14 15:01
12345678901 DEADPOOL FAST TRANSPORT Activation 27-04-12 18:30
12345678901 DEADPOOL FAST TRANSPORT Disconnection 17-01-16 16:35
PQ:RS:TU:VW:XY:ZA DEADPOOL FAST TRANSPORT Activation 27-04-12 18:31
PQ:RS:TU:VW:XY:ZA DEADPOOL FAST TRANSPORT Disconnection 02-12-13 13:26
78901234567 BANDHU BANDHU CABS Activation 27-04-12 19:12
78901234567 BANDHU BANDHU CABS Disconnection 27-10-13 19:42
78901234567 DEADPOOL FAST TRANSPORT Reconnection 25-02-14 13:53
78901234567 DEADPOOL FAST TRANSPORT Disconnection 08-04-16 17:14
这是一个简单的'选择'查询,包含一些条件和'Order By'。
为了查找此序列号处于活动状态和已断开连接的天数,我需要以下述格式提供此数据。我需要Power BI。
SERIAL NO ENTITY_CODE COMPANY_NAME STATUS FromDate ToDATE
AB:CD:EF:GH:IJ:KL FRANCIS MADICAL Activation 27-04-12 18:30 27-12-14 15:01
AB:CD:EF:GH:IJ:KL DEADPOOL FAST TRANSPORT Disconnection 27-12-14 15:01 TODAY'S DATE
12345678901 DEADPOOL FAST TRANSPORT Activation 27-04-12 18:30 17-01-16 16:35
12345678901 DEADPOOL FAST TRANSPORT Disconnection 17-01-16 16:35 TODAY'S DATE
PQ:RS:TU:VW:XY:ZA DEADPOOL FAST TRANSPORT Activation 27-04-12 18:31 02-12-13 13:26
PQ:RS:TU:VW:XY:ZA DEADPOOL FAST TRANSPORT Disconnection 02-12-13 13:26 TODAY'S DATE
78901234567 BANDHU BANDHU CABS Activation 27-04-12 19:12 27-10-13 19:42
78901234567 BANDHU BANDHU CABS Disconnection 27-10-13 19:42 25-02-14 13:53
78901234567 DEADPOOL FAST TRANSPORT Reconnection 25-02-14 13:53 08-04-16 17:14
78901234567 DEADPOOL FAST TRANSPORT Disconnection 08-04-16 17:14 TODAY'S DATE
请注意ToDate专栏中的“今天的日期”。 这个数据最终会给我这个数字生命中每一天的每个序列号的状态。
我需要根据每个交易日期划分“交易历史记录”,以便我能够发现某个序列号从date1到date2处于活动状态,然后从日期2到日期3断开连接,依此类推< / p>
有人可以帮忙吗?
答案 0 :(得分:1)
您的FromDate
似乎与TransactionDate
相同,ToDate
是下一个TransactionDate
,但序列号相同。
您应该能够在Power BI内部使用DAX创建计算列,以获取ToDate
。类似于以下内容,其中Status
是您的表名。
ToDate =
VAR EndDate = MINX(
FILTER('Status',
'Status'[Serial No] = EARLIER('Status'[Serial No]) &&
'Status'[TransactionDate] > EARLIER('Status'[TransactionDate])),
'Status'[TransactionDate])
RETURN IF(ISBLANK(EndDate), TODAY(), EndDate)
使用CALCULATE
代替MINX
的另一个版本:
ToDate =
VAR EndDate = CALCULATE(
MIN('Status'[TransactionDate]),
ALL('Status'),
'Status'[Serial No] = EARLIER('Status'[Serial No]),
'Status'[TransactionDate] > EARLIER('Status'[TransactionDate]))
RETURN IF(ISBLANK(EndDate),TODAY(),EndDate)
答案 1 :(得分:0)
SELECT
SERIAL NO
,ENTITY_CODE COMPANY_NAME
,STATUS
,TRANSACTION_DATE
,CASE STATUS
WHEN 'Activation' THEN <WHAT EVER YOU WANT TO HAPPEN>
WHEN 'Disconnection' THEN <WHAT EVER YOU WANT TO HAPPEN>
WHEN 'Reconnection' THEN <WHAT EVER YOU WANT TO HAPPEN>
ELSE <WHAT EVER YOU WANT TO HAPPEN>
END as ToDATE
FROM
<Table Name>
答案 2 :(得分:0)
看起来您想要选择大于当前交易日期且具有相同序列号的最小交易日期。如果没有匹配(在此示例中返回NULL),则返回今天的日期。
我使用了datetime数据类型,但看起来示例代码选择了varchar或char。我假设您正在使用正确的数据类型。
如果您有SQL Server Management Studio,可以尝试以下完整示例:
DECLARE @mytable TABLE ([SERIAL NO] varchar(100), ENTITY_CODE varchar(100), COMPANY_NAME varchar(100), [STATUS] varchar(100), TRANSACTION_DATE smalldatetime)
INSERT INTO @mytable VALUES
('AB:CD:EF:GH:IJ:KL', 'FRANCIS', 'MADICAL', 'Activation', '2012-04-27 18:30:00')
,('AB:CD:EF:GH:IJ:KL', 'DEADPOOL', 'FAST TRANSPORT', 'Disconnection', '2014-12-27 15:01:00')
,('12345678901', 'DEADPOOL', 'FAST TRANSPORT', 'Activation', '2012-04-27 18:30:00')
,('12345678901', 'DEADPOOL', 'FAST TRANSPORT', 'Disconnection', '2016-01-17 16:35:00')
,('PQ:RS:TU:VW:XY:ZA', 'DEADPOOL', 'FAST TRANSPORT', 'Activation', '2012-04-27 18:31:00')
,('PQ:RS:TU:VW:XY:ZA', 'DEADPOOL', 'FAST TRANSPORT', 'Disconnection', '2013-12-02 13:26:00')
,('78901234567', 'BANDHU', 'BANDHU CABS', 'Activation', '2012-04-27 19:12:00')
,('78901234567', 'BANDHU', 'BANDHU CABS', 'Disconnection', '2013-10-27 19:42:00')
,('78901234567', 'DEADPOOL', 'FAST TRANSPORT', 'Reconnection', '2014-02-25 13:53:00')
,('78901234567', 'DEADPOOL', 'FAST TRANSPORT', 'Disconnection', '2016-04-08 17:14:00')
SELECT M1.[SERIAL NO], M1.ENTITY_CODE, M1.COMPANY_NAME, M1.[STATUS], M1.TRANSACTION_DATE AS FromDate
,COALESCE(
(SELECT MIN(TRANSACTION_DATE)
FROM @mytable M2
WHERE M2.[SERIAL NO] = M1.[SERIAL NO]
AND M2.TRANSACTION_DATE > M1.TRANSACTION_DATE
)
, getDate()
) AS ToDATE
FROM @mytable M1