Parse column B content into logical parts according to column A

Dmitri Ilin

I have a table like this

sessionId  |   hostname
------     |   ------
a1         |   domain1
a1         |   domain2
a2         |   domain1
a3         |   domain1
a3         |   domain2
a4         |   domain2

What I want is to build a logical table containing the follwoing

sessionId  |   only domain1 | only domain2 | domain1 OR domain2 | domain1 AND domain2 
-----------|----------------|--------------|--------------------|--------------------
a1         |   1            | 1            | 1                  | 1
a2         |   1            | 0            | 1                  | 0
a3         |   1            | 1            | 1                  | 1
a4         |   0            | 1            | 1                  | 0

I guess there's a simple solution for this, but I can't get my head over it :(

Gordon Linoff

You can use conditional aggregation:

select (case when sum(case when hostname = 'domain1' then 1 else 0 end) > 0 
             then 1 else 0
        end) as domain1,
       (case when sum(case when hostname = 'domain2' then 1 else 0 end) > 0
             then 1 else 0
        end) as domain2,
       (case when sum(case when hostname = 'domain1' then 1 else 0 end) > 0 or
                  sum(case when hostname = 'domain2' then 1 else 0 end) > 0
             then 1 else 0
        end) as either,
       (case when sum(case when hostname = 'domain1' then 1 else 0 end) > 0 and
                  sum(case when hostname = 'domain2' then 1 else 0 end) > 0
             then 1 else 0
        end) as both          
from t
group by sessionid;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to sum parts of a column according to value (dataframe)

Print column content a if column content b != 0

Add logical value inside new column according to 'NA' values

How to copy the text from Column A to Column B, according to font size

How to put 1 or 2 in A column according to the value of column B?

How to parse a pandas column of JSON content efficiently?

According to the data in column B corresponding to column A of different categories in table A, how to transfer to new data columns in table B

How to get the C column corresponding values according to B columns pandas?

Automatically number rows in Excel according the dates in column B

Hide rows in a column with vanilla JavaScript according to text content

Getting the logical column letters

Aggregate parts of a column in a matrix

Duplicate Column A on next spreadsheet according to column B - Google Apps Script / Google Sheets

R loop to copy content from two columns, according a third column, and output into a fourth column

Group by according to the column names

Show column according to parameter

MySql Join According to a Column

Fill column B with column A

Adding a factor column based on parts of another column

Column wise logical operation in numpy

In python is there a way to delete parts of a column?

Sum parts of a column separated by 0

cut important parts of string in a column

Split datetime column into parts in R

Extract parts of column names for renaming

How to reset id column according to another column

Splits one column to multiple column according to data

Update mysql id column according to timestamp column

How to change column in matrix according to another column