This seems like it should be easy. I have a data set consisting of binary survey answers, "yes" and "no". I also demographic data such as gender, "male" and "female". I am trying to generate a single pivot table in Excel 2007 that shows the answer counts of multiple questions by a single demographic field. I can do one at a time but when I try to do more, they seem to get nested under each other. I have tried putting gender in the rows and the questions in the labels and vice versa to no avail. The closet I have gotten is by putting gender in the rows and a question under that but that's it. For the values I am simply using the count of responses.
Here is an example of what my data....
Gender Q1 Q2 Q3
Female yes yes yes
Male yes no yes
Male no no yes
Female no yes no
Here is what I want to get....
Gender Q1 Q2 Q3
yes no yes no yes no
Female 1 1 2 0 1 1
Male 1 1 0 2 2 0
Any tips would be great. Thanks in advance.
The structure of your data is not what the PivotTable needs.
You'd need to re-order the data in the following way, so that you have one column for each field you want to use in the results: Gender, Quartal, Answer (see screenshot).
Then your PivotTable will work perfectly.
For re-arranging the data you could use a VBA macro which splits the contents of one line in your original table in 3 lines.
Another way would be to use MS Access to re-arrange the data - you'll find a solution here: http://www.meadinkent.co.uk/accqueryjournal.htm . This might be also possible with MS Query in MS Excel(I have not tried it with Excel yet, but the access solution works fine).
Guess it can't be done. I have tried several different approaches and could get none of them to work. I ended up breaking up the data manually using the old fashioned way, cell equations. Too bad, I thought this would be a pretty basic thing to accomplish.
If you place gender for the first row label, use the filter tool to show what you want. i.e. filter one gender out or the other.
Related
Update: 1,2,3 are just examples it can also be 4,24,53
I have the following setup:
I store Data in BigQuery and use BigQuery as data source for my Data Studio project.
I have a column called Alarms and the data inside that column is as follow: it can be empty or 1 or 1,2 or 1,2,3 or 5,43,60 and so on. If it's empty or has 1 value then there is nothing to worry about, but if there are 2 or more values I have to do something.
name
Alarm
Mark
John
1
Eddie
1,2
Peter
1,2,3
What I need is to be able to put every value in a separate column or create a dropdown or something.
For example something like the table below or two drop down menus one to select the name and the other shows the alarms. (I prefer the drop downs).
name
Alarm
Mark
John
1
Eddie
1
2
Peter
1
2
3
Here I select Peter and the alarm drop down shows 3 alarms. or for Eddie it just shows 2 alarms and so on.
I read something about regex but I don't really understand how to put it to the test.
I found this online: (.+?)(?:,|$) but I don't know how to capture the output.
What I need is to be able to put every value in a separate column
Consider below approach
select * from (
select * except(alarm)
from your_table,
unnest(split(alarm)) flag with offset
)
pivot (min(flag) as alarm for offset in (0,1,2,3,4))
If applied to sample data in your question -output is
I have a quick question regarding writing a SQL query to obtain a complete entry from two or more entries where the data is missing in different columns.
This is the example, suppose I have this table:
Client Id | Name | Email
1234 | John | (null)
1244 | (null) | john#example.com
Would it be possible to write a query that would return the following?
Client Id | Name | Email
1234 | John | john#example.com
I am finding this particularly hard because these are 2 entires in the same table.
I apologize if this is trivial, I am still studying SQL and learning, but I wasn't able to come up with a solution for this and I although I've tried looking online I couldn't phrase the question in the proper way, I suppose and I couldn't really find the answer I was after.
Many thanks in advance for the help!
Yes, but actually no.
It is possible to write a query that works with your example data.
But just under the assumption that the first part of the mail is always equal to the name.
SELECT clients.id,clients.name,bclients.email FROM clients
JOIN clients bclients ON upper(clients.name) = upper(substring(bclients.email from 0 for position('#' in bclients.email)));
db<>fiddle
Explanation:
We join the table onto itself, to get the information into one row.
For this we first search for the position of the '#' in the email, get the substring from the start (0) of the string for the amount of characters until we hit the # (result of positon).
To avoid case-problems the name and substring are cast to uppercase for comparsion.
(lowercase would work the same)
The design is flawed
How can a client have multiple ids and different kind of information about the same user at the same time?
I think you want to split the table between clients and users, so that a user can have multiple clients.
I recommend that you read information about database normalization as this provides you with necessary knowledge for successfull database design.
Hopefully you can help!
I have a single data source in my SSRS report. With this data source, I have populated a tablix. The tablix looks something like this:
SalesPerson ID Group Sales
Sarah 1 1 1234
Ross 2 1 555
Gemma 3 2 678
Jill 4 2 345
Jack 5 3 987
Peter 6 2 432
Henry 7 2 356
The report is set up to create a different page for each of the sales people. for example, on the first page of the report, only first record would be shown (the record that holds Sarah's information, the second page would show the record for Ross' information ,etc..)
The issues I face is this:
At the bottom of the report, I need to include a textbox that displays the group number that the specific employee belongs to (the employee who is currently being displayed on the page).
I think that I need to do some sort of lookup on the IDReportItem to return the group ID in order to do this, but have had no luck in my attempts.
I understand that this is a horrible way of doing things, but I am limited to using this single dataset for performing this task.
Any help you can provide will be greatly appreciated,
Thanks you!
Unfortunately there doesn't seem to be a way to do this in a single textbox, but you can do it with a second tablix that uses the same dataset.
Create your second tablix and position it at the bottom of the page, then set your grouping to be the same on both tablixes and use the second tablix to only display the group ID, plus whatever label you want.
Create a new row group for each tablix (grouping on group ID), then right click the group and browse to Group Properties -> Page Breaks and check the box that says "Between each instance of a group". Do this for both tablixes.
This is what grouping is designed for. Build your table, and set the page break attribute to true.
You can have multiple rows under your group. Since your group is a field, simply add it to the detail row.
Your grouping is obviously set up right to get the report paging correctly.
You could add a List to the report, set the grouping on that (with page break between groups)
Inside the list - Add a RECTANGLE. (this be important!)
Once you've added the rectangle, you can add another as many objects as you like. In your case I think that may be a matrix and a text box
eg
Then it just becomes as spacing issue (to get the page looking right)
I am new to VBA and from what I have seen it will be the best way to go about doing what I want to do. I have an excel spreadsheet with Column A being the name of a company and Column B being the state which it is in. Since companies are in multiple states there are repeated company names, I would like to make it so that Column B has a list of the states so that Column A does not repeat. I know that similar questions have been asked but like I said I am new to this and have never used VBA before so some of the code without an explanation is not all that helpful.
For example I would like to go from:
Company 1 | MI
Company 1 | IA
Company 1 | MD
Company 2 | MI
Company 2 | OH
To this:
Company 1 | MI, IA, MD
Company 2 | MI, OH
Any help would be greatly appreciated or suggestions of ways other than VBA would be appreciated.
I suggest you put both columns in the ROWS area of a PivotTable and ensure that has no subtotals and is in tabular format.
You may not need vba at all. One way to get the results you want is to put the following equation in Column C (assuming Company is in A and States are in B):
"=IF(A2<>A1,A2,"")"
Without the outside quotes. Then just drag the formula to fill to the bottom of the data. Also, if you use this formula, start the data on A2, as the formula will look at the cell above.
The D column is simply =B2.
The VBA code will follow this logic as well, if you still want to use VBA.
I need to construct a query in Django, and I'm wondering if this is somehow possible (it may be really obvious but I'm missing it...).
I have a normal query Model.objects.filter(x=True)[:5] which can return results like this:
FirstName LastName Country
Bob Jones UK
Bill Thompson UK
David Smith USA
I need to only grab rows which are distinct based on the Country field, something like Model.objects.filter(x=True).distinct('Country')[:5] would be ideal but that's not possible with Django.
The rows I want the query to grab ultimately are:
FirstName LastName Country
Bob Jones UK
David Smith USA
I also need the query to use the same ordering as set in the model's Meta class (ie. I can't override the ordering in any way).
How would I go about doing this?
Thanks a lot.
I haven't tested this, but it seems to me a dict should do the job, although ordering could be off then:
d = {}
for x in Model.objects.all():
d[x.country] = x
records_with_distinct_countries = d.values()
countries = [f.country in Model.objects.all()]
for c in countries:
try:
print Model.objects.filter(country=c)
except Model.DoesNotExist:
pass
I think that #skrobul is on the right track, but a little bit off.
I don't think you'll be able to do this with a single query, because the distinct() method adds the SELECT DISTINCT modifier to the query, which acts on the entire row. You'll likely have to create a list of countries and then return limited QuerySets based on iterating that list.
Something like this:
maxrows = 5
countries = set([x.country for x in Model.objects.all()])
rows = []
count = 0
for c in countries:
if count >= maxrows:
break
try:
rows.append(Model.objects.filter(country=c)[0])
except Model.DoesNotExist:
pass
count += 1
This is a very generic example, but it gives the intended result.
Can you post the raw SQL that returns what you want from the source database? I have a hunch that the actual problem here is the query/data structure...