COUNTIF for rows which contain a given value in another column - countif

My table lists every character from all 5 of George R. R. Martin's currently published A Song of Ice and Fire novels. Each row contains a record indicating which book in the series the character is from (numbered 1-5) and a single letter indicating the character's gender (M/F). For example:
A B C
1 Character Book Gender
------------------------------
2 Arya Stark - 1 - F
3 Eddard Stark - 1 - M
4 Davos Seaworth - 2 - M
5 Lynesse Hightower - 2 - F
6 Xaro Xhoan Daxos - 2 - M
7 Elinor Tyrell - 3 - F
I can use COUNTIF to find out that there are three females and three males in this table, but I want to know, for example, how many males there are in book 2. How could I write a formula that would make this count? Here is a pseudocode of what I'm trying to achieve:
=COUNTIF(C2:C7, Column B = '2' AND Column C = 'M')
This would output 2.
I'm aware that this task is far better suited to databases and a SELECT query, but I'd like to know how to solve this problem within the constraints of a LibreOffice Calc spreadsheet, without using a macro. Excel-based solutions are fine, so long as they also work in Calc. If there's no solution that uses COUNTIF, it doesn't matter, so long as it works.

I worked it out, thanks to a prompt by assylias. The COUNTIFS formula produces the result I want by counting multiple search criteria. For example, this formula works out how many male characters are in Book 1 (A Game of Thrones).
=COUNTIFS($A$2:$A$2102, "=1", $L$2:$L$2102, "=M")

Related

How to sum only one of repeated values from joined data in RDLC

I'm not sure if SSRS is dumb, or I am (I'm leaning towards both).
I have a dataset that (as a result of joins etc) has some columns with the same values duplicated across every row (fairly standard database stuff):
rid cnt bid flg1 flg2
-------------------------------
4 2882 1 17 3
5 2784 1 17 3
6 1293 1 17 3
18 9288 2 4 9
20 762 2 4 9
Reporting based on cnt is straightforward enough. I can also make a tablix that shows the following:
bid flg1 flg2
------------------
1 17 3
2 4 9
(Where the tablix is grouped by Fields!bid.Value and the columns are just Fields!flg1.Value and Fields!flg2.Value respectively.)
What I can't figure out is how to display the sum of these values -- specifically I want to show that the sum of flg1 is 21 and the sum of flg2 is 12 -- not the sum of every row in the dataset (counting each value more than once).
(Note that I'm not looking for a sum of distinct values, as they may not be unique. I want a sum of one value from each bid group, because it's from a table join so they will always have the same value.)
If possible, I'd also like to be able to do a similar calculation at the top level of the report (not in any tablix); although I'd settle for hiding the detail row if that's the only way.
Obviously, Sum(Fields!flg1.Value) isn't the answer, as this either returns 51 (if on the first row inside the group) or 59 (if outside it).
I also tried Sum(Fields!flg1.Value, "bid") but this wasn't considered a valid scope.
I also tried Sum(First(Fields!flg1.Value, "bid")) but apparently you're not allowed to sum first values for some weird reason (and may have had the same scope problem anyway).
Using Sum(Max(Fields!flg1.Value, "bid")) does work, but feels wrong. Is there a better way to do this?
(Related: is there a good way to save the result of that calculation so that I can later also show a Sum of those totals without an even hairier expression?)
There are two basic ways to do this.
Do what you have already done (Sum(Max(Fields!flg1.Value, "bid")))
Sum the rendered values. To do this check the name of the cell containing the data you want (check it's properties) and then use something like =SUM(ReportItems!flg1.Value) where flg1 is the name of the textbox, which is not necessarily always the same name as the field.

Is there some kind of way to import data that consists of multiple rows?

In RapidMiner the data table I usually see is like this:
Row Age Class
1 19 Adult
2 10 Minor
3 15 Teenager
In the data table above this sentence, one row refers to one complete information.
But how do I input a data table to RapidMiner where more than one row refers to one complete information?
For example:
Row Word Rho Theta Phi
1 Hello 0.9384 0.4943 1.2750
2 Hello 1.2819 0.8238 1.3465
3 Hello 1.3963 0.1758 1.4320
4 Eat 1.3918 0.3883 1.1756
5 Eat 1.4742 0.0526 1.2312
6 Eat 0.6698 0.2548 1.4769
7 Eat 0.3074 1.2214 0.2059
In the data table above this sentence, rows 1-3 refers to one complete information where the combinations of rho, theta, and phi from rows 1-3 means the word hello. Same goes for rows 4-7 which is one complete information also that means the word eat. For further explanation of the information I'm talking about, take a look at the table below this sentence.
Row Rho Theta Phi Word
----------------------------
1 |0.9384 0.4943 1.2750|
2 |1.2819 0.8238 1.3465| HELLO
3 |1.3963 0.1758 1.4320|
----------------------------
4 |1.3918 0.3883 1.1756|
5 |1.4742 0.0526 1.2312|
6 |0.6698 0.2548 1.4769| EAT
7 |0.3074 1.2214 0.2059|
----------------------------
Again my problem is, how do I insert this kind of data table to RapidMiner where it understands that multiple rows refer to one complete information? Is there some kind of table like what I displayed below this sentence?
Row Word Rho Theta Phi
1 Hello 0.9384 0.4943 1.2750
. Hello 1.2819 0.8238 1.3465
1 Hello 1.3963 0.1758 1.4320
2 Eat 1.4742 0.0526 1.2312
. Eat 0.6698 0.2548 1.4769
. Eat 0.3074 1.2214 0.2059
2 Eat 0.3074 1.2214 0.2059
you can try to use the Pivot operator to group your result by word.
To do so, I would set the group attribute parameter to "Word" and the index parameter to "Row". It's not exactly the same representation, but close enough, depending on your use case, as multiple format tables are not part of RapidMiner's design.

Using two columns to obtain count on another in SQL

I have a simple question that I wasn't really sure how to search for (or title!). I apologize if this has been asked a million times. For the following table, how do I generate a report that will detail the number of companies that a person has worked for and how many people have also worked for that same number? So, for example, this table should return:
people, companiesperperson
1, 1
2, 2
1, 3
for the following table called personalinfo:
id_number first last company
1 John Doe Intel
2 John Doe Microsoft
3 Phil Jenkins Amgen
4 Phil Jenkins Bayer
5 Phil Jenkins Sanofi
6 Josh Edwards Walgreens
7 Amy Dill URS
8 Amy Dill ARCADIS
Let me know if this is still confusing and if I can further clarify what I am looking to do.
Thanks!
This is a rough estimate of the query but
SELECT count as companiesperperson, COUNT(first, last) as people FROM
(SELECT COUNT(company) as count, first, last FROM personalinfo GROUP BY (first, last)) as a
GROUP BY count
To explain the query first in the subquery we are asking for the names and count of companies after splitting up all the rows by names
Then in the outer query we split up all the rows by their count and ask how many unique names can be found in each group.
There may be a few syntax errors I've left straggling but the group by feature is really what's essential to understanding how to solve this question.

MS Access 2007 Rows to columns in recordset

I have a table which is like a questionnaire type ..
My original table contains 450 columns and 212 rows.
Slno is the person's id who answer the questionaire .
SlNo Q1a Q1b Q2a Q2b Q2c Q2d Q2e Q2f .... Q37c <450 columns>
1 1
2 1 1
3 1
4 1 1
5 1
I have to do analysis for this data , eg Number of persons who is male (Q1a) and who owns a boat (Q2b) i.e ( select * from Questionnaire where Q1a=1 and Q2b=1 ).. etc .. many more combinations are there ..
I have designed in MS access all the design worked perfectly except for a major problem ( Number of table columns is restricted to 255 ).
To be able to enter this into access table i have inserted in as 450 rows and 212 columns (now am able to enter this into access db). Now while fetching the records i want the record set to transpose the results into the form that i wanted so that i do not have to change my algorithm or logic .... How to achieve this with the minimum changes ? This is my first time working with Access Database
You might be able to use a crosstab query to generate what you are expecting. You could also build a transpose function.
Either way, I think you'll stil run into the 255 column limit and MS Access is using temporary table, etc.
However, I think you'll have far less work and better results if you change the structure of your table.
I assume that this like a fill-in-the-bubble questionnaire, and it's mostly multiple choice. In which case instead of recording the result, I would record the answer for the question
SlNo Q1 Q2
1 B
2 B
3 A
4 A C
5 A
Then you have far fewer columns to work with. And you query for where Q1='A' instead of Q1a=1.
The alternative is break the table up into sections (personal, career, etc.) and then do a join, and only show the column you need (so as not to exceed that 255 column limit).
An way to do this that handles more questions is have a table for the person, a table for the question, and a table for the response
Person
SlNo PostalCode
1 90210
2 H0H 0H0
3
Questions
QID, QTitle, QDesc
1 Q1a Gender Male
2 Q1b Gender Female
3 Q2a Boat
4 Q2b Car
Answers
SlNo QID Result
1 2 True
1 3 True
1 4 True
2 1 True
2 3 False
2 4 True
You can then find the question takers by selecting Persons from a list of Answers
select * from Person
where SlNo in (
select SlNo from Answers, Questions
where
questions.qid = answers=qid
and
qtitle = 'Q1a'
and
answers.result='True')
and SlNo in (
select SlNo from Answers, Questions
where
questions.qid = answers=qid
and
qtitle = 'Q2a'
and
answers.result='True')
I finally got the solutions
I created two table one having 225 columns and the other having 225 column
(total 450 columns)
I created a SQL statement
select count(*) from T1,T2 WHERE T1.SlNo=T2.SlNo
and added the conditions what i want
It is coming correct after this ..
The database was entered wrongly by the other staff in the beginning but just to throw away one week of work was not good , so had to stick to this design ... and the deadly is next week .. now it's working :) :)

Need help with Sql Server 2008 and Full Text Searching

I've setup a FTS on a single field, in a single table.
Field: Name NVARHCHAR(350) NOT NULL
Now, when i search for the following
1 ave
10 ave
i don't get back the results i expect.
Firstly, the search query 1 ave is transformed into "1*" AND "ave*". Now i run my CONTAINS(..) query...
SELECT FooId, Name
FROM [dbo].[Names]
WHERE CONTAINS(Name, #SearchQuery)
Then, along with the correct results, i also get these incorrect results back...
2 Ave (a couple of entries .. but they are all unique entires).
So, how did this get retrieved? there is no 1* in that piece of text? Its like .. the number is ignored?
Also - and this is important - i've removed an reference to a stop list AND rebuilt the catalog.
Hmm. I'm so confused. anyone have any suggestions?
The "1" can occur anywhere within the full text search indexed column it doesn't have to be directly before (or even before) the "Ave", is there a 1 somewhere else in that row?
Full text indexing will find derivitaves of words - like if you search for RUN, it might find RUNNING, RAN, RUN, etc.
I wonder if its deciding that 2 is near 1, and returning that as a near match. You should try switching your query to a CONTAINSTABLE query so that you can also evaluate the RANK to determine which of the answers is a closer match. You could then decide on a threshold and filter out any rows that don't meet your criteria as to how close of a match they are.
EDIT: Its not doing the inflection thinking 1 is near 2. I ran a test query on a sample table that looked like this...
PK Name
1 1 ave
2 10 ave
3 2 ave
4 12 avenue
5 13 avenue
6 100 ave.
7 200 ave
8 210 avenue
Here's the query I ran...
select *
from Table_1
where contains(name, '"1*" and "ave*"')
And here's the results I get...
PK Name
2 10 ave
4 12 avenue
5 13 avenue
6 100 ave.
The interesting thing here is that the first record in the table isn't found. (I ran this on SQL 2008 Dev edition). Based on those results (where nothing starting with 2 was found) - I'd double-check your query. Maybe post the full text of your entire query, including where the search variable is being set.