Grand total in Google spreadsheet pivot queries - sum

I have a sample Google spreadsheet here. In Sheet2 of that spreadsheet, I created a summary using the query:
=Query(Sheet1!A1:E24, "Select A, Count(E) GROUP BY A Pivot C")
But I want to sum of each surveyors count at the next row and its grand total like in PivotTable shown in 3rd sheet. Is it possible to calculate the grand total using queries itself? Can anyone please help.

This is an old question, but I just figured this out. You can use ={} to join the arrays resulting from multiple queries into one function. Creating the array with {} is described here: Using arrrays in Google Sheets
The easiest way to set it up is to do each part separately, then recombine them into one formula.
1st part is the query you did first.
=Query(Sheet1!A1:E24, "Select A, Count(E) GROUP BY A Pivot C")
2nd part is the column of totals. Put this formula in the top cell of the column just to the right of the 1st query results.
=Query(Sheet1!A1:E24, "Select Count(E) GROUP BY A label Count(E) 'Grand Total'")
3rd part is the bottom row totals. Put this under the first query results. There's no way to remove the headers row on a pivot, so instead, transpose the results of a non-pivot query.
=transpose(Query(Sheet1!A1:E24, "Select Count(E) GROUP BY C label Count(E) 'Grand Total'"))
4th part is the final Total cell at the bottom right. The '' for the label is to remove any label and just have the total in one cell.
=Query(Sheet1!A1:E24, "Select Count(E) Label Count(E) ''")
If it all looks the way you want it, now you put it together into one formula to replace the original query and delete the others. All contained within {}, separate columns with , and rows with ;.
={Query(Sheet1!A1:E24, "Select A, Count(E) GROUP BY A Pivot C"),Query(Sheet1!A1:E24, "Select Count(E) GROUP BY A label Count(E) 'Total'");transpose(Query(Sheet1!A1:E24, "Select Count(E) GROUP BY C label Count(E) 'Total'")),Query(Sheet1!A1:E24, "Select Count(E) Label Count(E) ''")}

1/2 the answer .... the total at the side, but not along the bottom
in sheet 1 add a Col F
Grand Total
=COUNTIF(A$2:A$24,A2)
=COUNTIF(A$2:A$24,A3)
Change you query to
=Query(Sheet1!A1:F24, "Select A, Count(B),F GROUP BY A, F pivot C ")

Related

Query function Google Sheet - Aggregation + (unwanted) Sorting

I am trying to run what it started as a simple task but it turned out to be more complicated.
I must run a local sum of a column over different elements of another column with a query function.
The issue arises because the query performs an unwanted sorting of the grouped column (it is in the format of working weeks - strings) and I cannot get it to unsort or re-sort in the original format.
Initial query is:
=query(A1:B350,"select A, sum(B) group by A")
See the example:
click here to see example
Subsequently I tried with:
=query(A1:B350,"select A, sum(B) where A matches '"&join("|", query(G2:G, "select G where G is not null"))& "' group by A")
like so:
click here to see example
but the unwanted sorting remains.
Any idea on how to force the initial sorting or preventing it from changing?
Thank you in advance
To sort correctly, you need to align single digits. You can do this either in the source data or using a formula:
=QUERY({INDEX(REGEXREPLACE(A:A,"-(\d)$","-0$1")),B:B},"SELECT Col1, SUM(Col2) GROUP BY Col1")
try:
=INDEX(IFNA(VLOOKUP(G2:G,
QUERY(A1:B350, "select A,sum(B) group by A label sum(B)''"), {1, 2}, 0)))

Retrieving Columns with count greater than 1 - Google Sheet Query

I'm using Google sheets, and I want to get the data from one sheet to another where I want only the columns with count > 1.
Let's say we have 3 columns A, B, and C. I tried the following (the first sheet name is "Form Responses 1"):
I thought about using a query in the second sheet as: =query('Form Responses 1'!A1:Z, "Select A having count (A) >1 union select B having count (B) >1 union select C having count (C) > 1"). But I got a parse error where it seems that union and having are not supported in google sheets query.
How can I achieve this (whether it's using query or any other Google sheets function that can work)?
More details:
The first sheet contains info about exercises conducted during a lecture and it gets its data from a Google Form (so the responses are fed in this sheet). Here is a screenshot of it:
Please note that the form is divided into sections. When the user selects the course, the attendance, the participation, and adds a comment, then they go to the next section, the next section will be based on the selected course, the newly opened section will have the exercise name and rating questions (the exercise name is a dropdown list with items that are prefilled and specific to the selected course). That's why, you can see that "exercise name" and "rate the exercise" columns are repeated because we have 2 sections in this form.
The second sheet should contain the data of a selected course only (either mobile dev or web dev) which can be achieved easily through a query with a where clause. But, in addition to that, it shouldn't contain the empty columns of "exercise name" and "rate the exercise" as they correspond to another section. So, it should have only one exercise name column and one rating column that correspond to the selected course. Here is a screenshot if we only use a query with where clause without removing the extra name and rating columns:
Here is a screenshot with the desired result:
Thanks.
why not use just:
=QUERY('Form Responses 1'!A1:Z, "select A,B,C,D,E,F,G where F is not null", 1)
Use "OR" condition
Eg:-
QUERY(Data!A:R,"select A, N, P where N>0 or P>0")
where A column has country and N, P columns have population values

Access - SQL Query Date wise with selection of column summarized value

Below is my source Data
by using below query I can get summarized data for '17-09-2016'
SQL Query :-
SELECT key_val.A, key_val.B, key_val.C, key_val.D, Sum(IIf(key_val.Store_date=#9/17/2016#,key_val.Val,0)) AS [17-09-2016]
FROM key_val
GROUP BY key_val.A, key_val.B, key_val.C, key_val.D;
but I am looking output suppose to look like this way.
Specifically= I need summarized data for column a,b,c and for '17-09-2016' dateIn excel we will apply sumifs formula to get desired output but in Access - SQL I am not getting how to form the query to get the same data.
Can any one assist me how to acheive above result by using Access Query?
Specifically= I need summarized data for column a,b,c and for '17-09-2016' date
I'm not sure where you get the 34 figure from - the sum of the first two rows even though the values in A, B, C & D are different (so the grouping won't work)?
Making an assumption that you want the values summed where all the other fields are equal (A, B, C, D & Store_Date):
This query will give you the totals, but not in the format you're after:
SELECT A, B, C, D, SUM(val) As Total, Store_Date
FROM key_val
WHERE Store_date = #9/17/2016#
GROUP BY A,B,C,D, Store_Date
This SQL will give you the same, but for all dates (just remove the WHERE clause).
SELECT A, B, C, D, SUM(val) As Total, Store_Date
FROM key_val
GROUP BY A,B,C,D, Store_Date
ORDER BY Store_Date
This will give the exact table shown in your example:
TRANSFORM Sum(val) AS SumOfValue
SELECT A, B, C, D
FROM key_val
WHERE Store_date = #9/17/2016#
GROUP BY A,B,C,D,val
PIVOT Store_Date
Again, just remove the WHERE clause to list all dates in the table:

Non blank cell count for a particular row in dynamic colums in ssrs with matrix

I have a SQL query like this
SELECT sam.AREATOPICID,
sec.SURVEYSECTION,
us.USERSURVEYID,
us.SURVEYAREATOPICID,
us.USERID,
usr.SURVEYUSERNAME,
us.COMMENT
FROM USERSURVEY us
INNER JOIN USERS usr
ON us.USERID = usr.USERID
INNER JOIN SURVEYAREATOPICMAPDEMO sam
ON sam.AREATOPICID = us.SURVEYAREATOPICID
INNER JOIN SECTION sec
ON sam.SURVEYSECTIONID = sec.SURVEYSECTIONID
ORDER BY us.USERID,
sam.AREATOPICID,
SURVEYSECTION
Now, in SSRS I want to display report like this
Now for comment, I want in a particular row how many users have comment on it. This field only have to show count of comments that are not blank and for a particular row(i.e. question).
I have done for user1, user2 .., type of column but I am not able to count the comment.
When you create your matrix, there are row (your question), column (your user) and a data cell (sum(COMMENT))in it. Right-click it and you should get Add Total menu item. Use it to add a column total. Then you can position that column to an appropriate location in your table.
EDIT
For counting records you use COUNT() like COUNT(Fields!Comment.Value)
For counting non-empty/null values use:
SUM(IIF(IsNothing(Fields!Comment.Value) OR (Fields!Comment.Value = ""),0,1))

How to add a rdlc group's totals row for an outer group's totals row

I have a 2-level group report (rdlc). The inner group (GroupA) has in its totals row, totals of all record values, except one value which is the same for all records and for which that is the value I need to show(let's call that X). So the totals row looks like
Sum,Sum,....X
To do this I use Sum(Fields!,"GroupA") for all cells except this one, which is Fields!X.Value;
The outer group (Group B) must add all these values and display their sums in its totals row. The desired output should be:
Sum,Sum,.....Sum
So for all other fields, I use Sum(Fields!,"Group2").
What must I use for the X field? If I use Sum(Fields!X.Value,"Group2") it will add X as many numbers as there are records, where I want to add X for each group.
I really would appreciate a quick answer on this as I have to fix this problem yesterday :)
thanks
c.