Pivot table in Google Data Studio - pivot-table

I have such pivot table in Google Data Studio:
How to write a formula to sum columns Count1 and Count2 in new a column:
SUMC1-C2
I have no idea how to do this. I tried to Add Metric or Add Column dimension and write there some formulas like SUM(Count1+Count2) or Count1+Count2 simply. In any case I've got:
Unknown dimension or metric ID: Count1

Using the sample data added based on the comments:
id
datetime
counter
1124
2020-05-26 14:49:07
C1
1164
2020-05-26 14:49:07
C2
1163
2020-05-27 14:49:07
C3
1124
2020-05-27 14:49:07
C3
1165
2020-05-28 14:49:08
C3
1166
2020-05-28 14:49:11
C4
1167
2020-05-29 14:49:14
C2
1168
2020-05-29 14:49:17
C1
1169
2020-05-30 14:49:20
C4
It can be achieved using a Table with each Metric represented by an individual Calculated Field, a CASE statement:
C1:
COUNT(CASE
WHEN REGEXP_MATCH(counter, "(C1)") THEN id
ELSE NULL END)
C2:
COUNT(CASE
WHEN REGEXP_MATCH(counter, "(C2)") THEN id
ELSE NULL END)
C3:
COUNT(CASE
WHEN REGEXP_MATCH(counter, "(C3)") THEN id
ELSE NULL END)
C4:
COUNT(CASE
WHEN REGEXP_MATCH(counter, "(C4)") THEN id
ELSE NULL END)
C1PLUSC2:
COUNT(CASE
WHEN REGEXP_MATCH(counter, "(C1|C2)") THEN id
ELSE NULL END)
Google Data Studio Report to and a GIF to elaborate:

Related

GROUP BY and create columns for each unique value grouped on (output as 1 row always), then use the output as a JOIN - SQL Server

I need to GROUP data BY 2 columns (BillingId and PaymentType) - no issues with that AND have output of 1 row with columns of unique PaymentType - issue with this step. So each BillingId has only 1 row and that table will be used for joining another Tables in the Database.
Billing Table:
BillingId (unique)
12345
67890
Payment Table:
PaymentId PaymentType BillingId PaymentAmount
(unique)
12 electronic 12345 62.29
14 electronic 12345 73.28
56 electronic 12345 -62.29
6 electronic 67890 83.58
2 adjustment 67890 30.43
MY CODE:
SELECT GroupedTable.*
FROM (SELECT b.BillingId,
p.PaymentType,
SUM(p.PaymentAmount) AS AmountPaid
FROM Billing AS b
LEFT JOIN Payment AS p
ON (b.BillingId = p.BillingId)
GROUP BY b.BillingId, p.PaymentType) AS GroupedTable
OUTPUT (obviously incorrect):
BillingId PaymentType AmountPaid
67890 electronic 83.58
12345 electronic 73.28
67890 adjustment 30.43
OUTPUT I NEED:
BillingId AmountPaid AmountAdjusted
(electronic) (adjustment)
67890 83.58 30.43
12345 73.28 0
It looks easier if you use Case When expressions as follows:
Select B.BillingId, Sum(Case When P.PaymentType='electronic' Then P.PaymentAmount End) As [AmountPaid (electronic)],
Sum(Case When P.PaymentType='adjustment' Then P.PaymentAmount End) As [AmountAdjusted (adjustment)]
From Billing As B Left Join Payment As P On (B.BillingId=P.BillingId)
Group by B.BillingId
db<>fiddle
BillingId
AmountPaid (electronic)
AmountAdjusted (adjustment)
12345
73,28
NULL
67890
83,58
30,43
You should group by BillingId only and use conditional aggregation:
SELECT b.BillingId,
SUM(CASE WHEN p.PaymentType = 'electronic' THEN p.PaymentAmount ELSE 0 END) AS AmountPaid,
SUM(CASE WHEN p.PaymentType = 'adjustment' THEN p.PaymentAmount ELSE 0 END) AS AmountAdjusted
FROM Billing AS b LEFT JOIN Payment AS p
ON b.BillingId = p.BillingId
GROUP BY b.BillingId;
See the demo.

SQL Count Distinct User IDs based on Column String Value

I am trying to create a new data table that has total counts of users for each week in two separate columns, a count for distinct users from Platform = 'Web' and a count of distinct users from Platform = 'iOS' or 'Android'
My attempt below did not work. Can someone advise?
DATA SOURCE:
DATE USER_ID Platform
1/1/2020 1223 Web
1/2/2020 2032 iOS
1/3/2020 2432 Android
1/4/2020 20311 iOS
1/4/2020 2443 Android
SQL ATTEMPT
SELECT DATE_TRUNC(week, d.DATE) as DATE,
COUNT(DISTINCT d.USER_ID WHERE d.PLATFORM = 'Web') AS USER_COUNT_WEB,
COUNT(DISTINCT d.USER_ID WHERE d.PLATFORM = 'Android' OR 'iOS') AS USER_COUNT_PHONE
FROM data d
GROUP BY 1;
Target table:
DATE User_Count_Web User_count_Phone
1/1/2020 12 230
1/8/2020 20 442
1/15/2020 24 533
here is one way :
SELECT
DATE_TRUNC(week, d.DATE) as DATE,
COUNT(DISTINCT case when d.PLATFORM = 'Web' then d.USER_ID end) AS USER_COUNT_WEB,
COUNT(DISTINCT case when d.PLATFORM in ('Android','iOS') then d.USER_ID end) AS USER_COUNT_PHONE
FROM data d
GROUP BY 1;
The sum/case pattern is also useful for this kind of "pivot query" -
SELECT
WEEK(date),
SUM(CASE WHEN platform='Web' THEN 1 ELSE 0 END) AS count_web,
SUM(CASE WHEN platform IN ('iOS','Android') THEN 1 ELSE 0 END) AS count_phone
FROM data
GROUP BY WEEK(date)
(The week() function is specific to mysql, and this answer gives the week as an integer offset from the start of the year rather than as an actual date, but the idea is the same.)

T-SQL: group date ranges and set end date based on, on/off column

I'm struggling to find an effective, concise way without a loop to produce groups of active dates where activity (grant/rescind activity) is based on the Switch column which has values 0 off and 1 on and start and end dates.
TransactionDate EffectiveDate TerminationDate Switch
-------------------------------------------------------------------
2013-06-14 2013-05-29 NULL 1
2013-06-14 2013-06-05 2013-06-05 0
2013-10-03 2013-05-29 2013-05-29 0
2013-10-12 2013-05-29 NULL 1
2013-10-12 2013-06-06 2013-06-06 0
The final output should be but one row:
2013-05-29 to 2013-06-06
The output is one row because the the last two transactions were switch on for 5/29/2013 and the last switch off was for 2013-06-06, which becomes the end date for the span.
Even more, the dates should also be grouped by active spans. If there were another year record in here it would need to be on a separate row.
Can I please get some help with a query to solve this issue?
Is this what you want?
select max(case when switch = 1 then effective_date end) as on_date,
(case when max(case when switch = 1 then effective_date end) <
max(case when switch = 0 then effective_date end)
then max(case when switch = 0 then effective_date end)
end) as off_date -- if any
from t;
This gets the last date the switch is on. And then the last date after that it is off.

How to make group by query with sum function

I have inventoryTransaction table in SQL Server Like this:
productID StockLocationID TransactionType Quantity
1046 1 "in" 100
1046 1 "out" 20
1046 2 "in" 70
1046 2 "out" 65
...
How to make a query, output like this:
productID StockLocationID stock
1046 1 80
1046 2 5
You can try:
SELECT
productID,
StockLocationID,
SUM(CASE
WHEN TransactionType = 'out' THEN -1
ELSE 1
END) * Quantity) AS stock
GROUP BY
productID,
StockLocationID
The same but a little shortened eliminating unnecessary multiplication:
SELECT
productID,
StockLocationID,
SUM(CASE TransactionType
WHEN 'out' THEN -Quantity
ELSE Quantity
END) AS stock
GROUP BY
productID,
StockLocationID

Aggregating data from multiple columns (similar to a PIVOT table)

I have a table that has the following data (shortened for this example):
C1 C2 C3 C4
=========================
1 0 1 1 0
2 1 1 0 1
3 1 0 1 1
4 1 1 1 1
5 0 0 1 1
6 0 0 0 1
I want to create a create a query that gives me the following result:
C3 C4
=============
C1 2 3
C2 2 2
That is, the combination of those four columns when:
C1 = 1 & C3 = 1
C1 = 1 & C4 = 1
C2 = 1 & C3 = 1
C2 = 1 & C4 = 1
I've been able to get make some progress, but not ultimately what I want. I've managed to get this far: http://sqlfiddle.com/#!3/7d3d4/2/0
but I can't figure out how to get my query to format like my desired output above. I was going to try to use a PIVOT table, but quickly abandoned that idea because it seemed to get extremely convoluted once I needed to add a 2nd PIVOT statement (and I'd end up having to add more as my example above is a really basic example of the number of columns I need to perform this on).
What am I missing?
Thank you.
try this:
SELECT 'C1',SUM(CASE WHEN C1=1 AND C3=1 THEN 1 END)AS C3,
SUM(CASE WHEN C1=1 AND C4=1 THEN 1 END)AS C4
FROM T1
UNION ALL
SELECT 'C2',SUM(CASE WHEN C2=1 AND C3=1 THEN 1 END)AS C3,
SUM(CASE WHEN C2=1 AND C4=1 THEN 1 END)AS C4
FROM T1
SQL Fiddle Demo
I think this gives you the result set as asked
SELECT COL1, C3, C4
FROM (
SELECT SUBSTRING(col,1,2) col1, SUBSTRING(col,3,2) col2, value
FROM (
SELECT SUM(c1*c3) as c1c3, SUM(c1*c4) as c1c4,
SUM(c2*c3) as c2c3, SUM(c2*c4) as c2c4
FROM t1
) t
UNPIVOT (value FOR col IN (c1c3, c1c4, c2c3, c2c4)) AS u
) data
PIVOT (SUM(value) FOR col2 IN ([c3], [c4])) pvt
I started writing a query that gives the totals, the resultset looks like this
c1c3 c1c4 c2c3 c2c4
2 3 2 2
At this moment you could probably just use this query and will get the result as wanted
SELECT 'c1' AS col, c1c3 AS C3, c1c4 AS C4
UNION ALL
SELECT 'c2' AS col, c2c3 AS C3, c2c4 AS C4
The first solution is just an example of how to UNPIVOT and then PIVOT again to order the columns in the way you want

Resources