Forum Discussion
ScottAllison
Mar 10, 2021Iron Contributor
Need help with a query with multiple if/thens
So I'm having a hard time coming up with a query that will get me the intended results. Any help would be appreciated.
My data looks like this:
Computer | ID |
SERVER-A | 12345 |
SERVER-A | |
SERVER-A | 67890 |
SERVER-B | |
SERVER-C | |
SERVER-C | 34567 |
What I'm trying to get in a result:
- If Computer has an ID or multiple IDs, return those rows but not the blanks
- If Computer has ONLY blank ID, then return that row.
Expected Result:
Computer | ID |
SERVER-A | 12345 |
SERVER-A | 67890 |
SERVER-B | |
SERVER-C | 34567 |
I'm sure I'm missing something simple...
ScottAllison
I'm not sure if I missed the 'simple' as well, but this works, using the datatable!datatable (Computer:string,ID:string) [ 'SERVER-A','12345', 'SERVER-A','', 'SERVER-A','67890', 'SERVER-B','', 'SERVER-C','', 'SERVER-C','34567' ] | summarize countif(toreal(ID) > 1), make_set(Computer),make_set_if(ID, isnotempty(ID)) by Computer | summarize by Computer, tostring(set_ID) | extend set_ID = iif(set_ID == '[]',' ',set_ID) | mv-expand ID=todynamic(set_ID) to typeof(string) | project-away set_ID
2 Replies
- CliveWatsonFormer Employee
ScottAllison
I'm not sure if I missed the 'simple' as well, but this works, using the datatable!datatable (Computer:string,ID:string) [ 'SERVER-A','12345', 'SERVER-A','', 'SERVER-A','67890', 'SERVER-B','', 'SERVER-C','', 'SERVER-C','34567' ] | summarize countif(toreal(ID) > 1), make_set(Computer),make_set_if(ID, isnotempty(ID)) by Computer | summarize by Computer, tostring(set_ID) | extend set_ID = iif(set_ID == '[]',' ',set_ID) | mv-expand ID=todynamic(set_ID) to typeof(string) | project-away set_ID
- ScottAllisonIron ContributorThis is exactly what I needed (not the datatable part, though 🙂 )!
Thanks again as always, Clive!