SQL Qliksense 中的逻辑
SQL logic in Qliksense
我想将客户端 IP table 和 PEP table 合并为两个字段。
|=========================|
| Client ID | PEP |
|=========================|
| 1 | Yes |
| 1 | No |
| 2 | Yes |
| | |
==========================
在上面的 qliksense SQL table 中,每当客户端 ID 重复时,我应该能够 select 'Yes' 值并丢弃 'No'
经过逻辑,我的回答应该只有1是2否
这可以实现吗?
如果你想要的是仅在所有值都为 no 时显示 No,否则在 SQL 中显示 Yes 我会这样做
SELECT ID
CASE WHEN SUM(CASE WHEN PEP = 'Yes' THEN 1 ELSE 0 END) > 0 THEN 'Yes'
ELSE 'No END AS PEP
FROM Some_table_you_did_not_name
GROUP BY ID
希望这对你有用:
select clientId, max(PEP)
from ip
group by clientId
不确定您是否正在查看仅限 Qlik 的解决方案,但下面的脚本是如何在 Qlik 脚本中实现的示例
作为这些脚本的结果,将有 2 个表(由 Client ID
字段链接):
- RawData - 每个客户多行
- MaxData - 每个客户一行多一个字段
表示客户端是否重复
选项 1:统计客户
RawData:
Load * Inline [
Client ID, PEP
1 , Yes
2 , Yes
1 , No
];
MaxData:
Load
[Client ID],
if(ClientCount > 1, 'Yes', 'No') as Repeat
;
Load
[Client ID],
count([Client ID]) as ClientCount
Resident
RawData
Group By
[Client ID]
;
选项 2:使用 peek
函数
RawData:
Load * Inline [
Client ID, PEP
1 , Yes
2 , Yes
1 , No
];
// Load the same data but order it by the Client ID column
// create new field (RepeatsTemp) which compares the current Client ID value
// with the previous one. If they are equal assign 1 else 0
TempData:
Load
[Client ID],
PEP,
if([Client ID] = Peek([Client ID]), 1, 0) as RepeatsTemp
Resident
RawData
Order By
[Client ID]
;
// Load the TempData table and get the max of RepeatsTemp for
// each Client ID (group by by Client ID)
// in the preceeding load "replace" 1 and 0 from the repeat field
// with 'yes' and 'no'
MaxData:
Load
[Client ID],
if(MaxRepeats = 1, 'Yes', 'No') as Repeats
;
Load
[Client ID],
max(RepeatsTemp) as MaxRepeats
Resident
TempData
Group by
[Client ID]
;
// we dont need this table anymore
Drop Table TempData;
我想将客户端 IP table 和 PEP table 合并为两个字段。
|=========================|
| Client ID | PEP |
|=========================|
| 1 | Yes |
| 1 | No |
| 2 | Yes |
| | |
==========================
在上面的 qliksense SQL table 中,每当客户端 ID 重复时,我应该能够 select 'Yes' 值并丢弃 'No'
经过逻辑,我的回答应该只有1是2否
这可以实现吗?
如果你想要的是仅在所有值都为 no 时显示 No,否则在 SQL 中显示 Yes 我会这样做
SELECT ID
CASE WHEN SUM(CASE WHEN PEP = 'Yes' THEN 1 ELSE 0 END) > 0 THEN 'Yes'
ELSE 'No END AS PEP
FROM Some_table_you_did_not_name
GROUP BY ID
希望这对你有用:
select clientId, max(PEP)
from ip
group by clientId
不确定您是否正在查看仅限 Qlik 的解决方案,但下面的脚本是如何在 Qlik 脚本中实现的示例
作为这些脚本的结果,将有 2 个表(由 Client ID
字段链接):
- RawData - 每个客户多行
- MaxData - 每个客户一行多一个字段 表示客户端是否重复
选项 1:统计客户
RawData:
Load * Inline [
Client ID, PEP
1 , Yes
2 , Yes
1 , No
];
MaxData:
Load
[Client ID],
if(ClientCount > 1, 'Yes', 'No') as Repeat
;
Load
[Client ID],
count([Client ID]) as ClientCount
Resident
RawData
Group By
[Client ID]
;
选项 2:使用 peek
函数
RawData:
Load * Inline [
Client ID, PEP
1 , Yes
2 , Yes
1 , No
];
// Load the same data but order it by the Client ID column
// create new field (RepeatsTemp) which compares the current Client ID value
// with the previous one. If they are equal assign 1 else 0
TempData:
Load
[Client ID],
PEP,
if([Client ID] = Peek([Client ID]), 1, 0) as RepeatsTemp
Resident
RawData
Order By
[Client ID]
;
// Load the TempData table and get the max of RepeatsTemp for
// each Client ID (group by by Client ID)
// in the preceeding load "replace" 1 and 0 from the repeat field
// with 'yes' and 'no'
MaxData:
Load
[Client ID],
if(MaxRepeats = 1, 'Yes', 'No') as Repeats
;
Load
[Client ID],
max(RepeatsTemp) as MaxRepeats
Resident
TempData
Group by
[Client ID]
;
// we dont need this table anymore
Drop Table TempData;