Sorting Counts in Access - ms-access

I am using Access 2013 and I have many Yes/No fields in a table named Survey. I have 25 survey questions that people check if true (yes), and I am querying the fields across the whole database to get a count of how many people entered Yes for each field. A subset of my table is:
SurveyID AutoNumber
MemberID Number
Question1 Yes/No
Question2 Yes/No
Question3 Yes/No
etc...
The following query runs against the table above and is a subset of what I have, in that it only represents three fields:
SELECT Count(IIf([Survey]![Question1]=True,1,Null)) AS CountOfQuestion1, Count(IIf([Survey]![Question2]=True,1,Null)) AS CountOfQuestion2, Count(IIf([Survey]![Question3]=True,1,Null)) AS CountOfQuestion3
FROM Survey;
This works fine (the IIF bit is due to Access and its strangeness). My question is, how do I now order this? Each result comes out as a separate field in the query, and I need to order on the results of all the fields, not just one field. For example, I might get the following:
CountOfQuestion1 34
CountOfQuestion2 7
CountOfQuestion3 11
I need to be able to sort this based on the numbers, so I know which count was the highest. I hope to get:
CountOfQuestion1 34
CountOfQuestion3 11
CountOfQuestion2 7
I feel like I'm missing something obvious, but any help would be much appreciated.
Thanks!

It would seem you want the results in rows rather than in columns. One way to accomplish this is to use separate queries for each question count and then use the union operator to merge the results together like this:
SELECT
'Q1' AS Question, Count(IIf([Survey].[Question1]=True,1,Null)) AS QuestionCount
FROM Survey
UNION ALL
SELECT
'Q2' AS Question, Count(IIf([Survey].[Question2]=True,1,Null)) AS QuestionCount
FROM Survey
UNION ALL
SELECT
'Q3' AS Question, Count(IIf([Survey].[Question3]=True,1,Null)) AS QuestionCount
FROM Survey
ORDER BY QuestionCount DESC
This would give output looking like this:
Question QuestionCount
-------- -------------
Q2 4
Q1 3
Q3 2

In Access Yes/No fields can be summed, so the following should work.
Select ABS(Sum(Question1)) as CountOfQuestion1, ABS(Sum(Question2)) as CountOfQuestion2
FROM Survey

Related

what does this sql query do? SELECT column_1 FROM table_1,table_2;

SELECT column_1 FROM table_1,table_2;
When I ran this on my database it returned huge number of rows with duplicate column_1 values. I could not understand why I got these results. Please explain what this query does.
it gives you a cross product from table 1 and table 2
In more layman's terms, it means that for each record in Table A, you get every record from Table B (all possible combinations).
TableA with 3 records and Table B with 3 records gives 9 total records in the result:
TableA-1/B-1
TableA-1/B-2
TableA-1/B-3
TableA-2/B-1
TableA-2/B-2
TableA-2/B-3
TableA-3/B-1
TableA-3/B-2
TableA-3/B-3
Often used as a basis for Cartesian Queries (which themselves are the means to generate, say, a list of future dates based on a recurrence schedule: give me all possible results for the next 6 months, then restrict that set to those whose factor matches my day of the week)
This is 'valid' way of cross joining two tables; it is not the preferred way though. Cross Join would be much clearer. An on condition would then be helpful to limit results,
Imagine that i have 3 friends named Jhon, Ana, Nick; then i have in the other table 2 are T-shirts a red and a yellow and i wanna know witch is from.
So in the query being tableA:Friends and tableB:Tshirts returns:
1|JHON | t-shirt_YELLOW
2|JHON | t-shirt_RED
3|ANA | t-shirt_YELLOW
4|ANA | t-shirt_RED
5|NICK | t-shirt_YELLOW
6|NICK | t-shirt_RED
As you see this join has no relational logic between friends and Tshirts so by evaluating all the posible combination generates what you call duplicates.

MySQL: How to get TOP visited product for each user in a table?

I have a system with products. Everytime a user enters a product, I insert a record into my database.
I have a table with users and id_products, like this:
users id_product
____________________________
jondoe 2
george 9
jondoe 5
jondoe 2
george 9
george 9
george 2
I need a result (query) wich shows what is TOP visited product id for each user, so the result would be something like this:
jondoes most visited product is ID 2
georges most visitedproduct is ID 9
I was looking for the answer but I am not able to figure it out. Thanks a lot for your help, I appreciate it a lot.
Jan
This is a pain because it involves aggregation. One way to solve this uses a very complicated query. Another uses variables. A third method uses an aggregation trick that works under many circumstances:
select user,
substring_index(group_concat(id_product order by cnt desc), ',', 1) as mostCommonProduct
from (select user, id_product, count(*) as cnt
from t
group by user, id_product
) t
group by user;
One danger when using this method is that the intermediate result might be too long. You can set the group_concat_max_len system variable to get around that particular problem.

MySQL compare data from date ranges prior to the current row and count() them

This is in reference to the still-open question here - I want to try to approach it differently.
I have a MySQL table with two pieces of information: order_date and email_address. This is exhaustive and non-distinct, meaning that there are duplicates if someone happened to make more than one purchase per-day.
I need to get the following report in as few queries as possible, hopefully one:
YYYY-MM | number_emails_this_month | numer_emails_repeated_prior
Where some sample output from the query result would look like this:
YYYY-MM | number_emails_this_month | numer_emails_repeated_prior
2010-02 23423 1231
2010-03 4422 2234
2010-04 1424 650
Any help is greatly appreciated!
I am not sure I understand what is number_emails_repeated_prior. If you could post a short example of data and a corresponding example of wanted results it would be helpful.
Taking a guess about what you are aiming for, to get the number of emails from a specific user per month all you need is:
SELECT DATE_FORMAT(order_date, '%Y-%m') as `YYYY-MM`,
COUNT(email_address) as `number_emails_this_month `
FROM table_name
WHERE email_address = 'some#address'
GROUP BY 1
ORDER BY 1
This question was answered in a subsequent related question here:
MySQL Subquery with User-Defined Variables
Ultimately the solution was to create a table with the ranges as-requested and join on that instead of using a subquery to define the ranges in question. In retrospect, use of the user-defined vars in MySQL aided the search for the subquery-less solution.

Mysql Covert rows to columns

I have a table with order numbers, first name, last name, question and answers. There are 5 questions asked to the user, each answer to a question generates 1 row of data, which produces 5 rows per user. I need a query that returns order number, first name, last name and the questions and answers converted to columns, returning 1 row per user.
Any help would be appreciated
Thanks,
Larry
Seems like you want to join the table to itself 5 times.
Something like
select q1.first_name, q1.last_name, max(q1.question), max(q1.answer), max(q2.question), max(q2.answer),max(q3.question), max(q3.answer),...
from questions q1
join questions q2 on q1.first_name=q2.first_name and q1.last_name=q2.last_name
join questions q3 on q1.first_name=q3.first_name and q1.last_name=q3.last_name
where q1.order_number = 1 and q2.order_number = 2 and q3.order_number = 3 ...
group by q1.first_name, q1.last_name
Using max will collapse down the rows into unique first name/last name pairs.

Grouping timestamps in MySQL with PHP

I want to log certain activities in MySql with a timecode using time(). Now I'm accumulating thousands of records, I want to output the data by sets of hours/days/months etc.
What would be the suggested method for grouping time codes in MySQL?
Example data:
1248651289
1248651299
1248651386
1248651588
1248651647
1248651700
1248651707
1248651737
1248651808
1248652269
Example code:
$sql = "SELECT COUNT(timecode) FROM timecodeTable";
//GROUP BY round(timecode/3600, 1) //group by hour??
Edit:
There's two groupings that can be made so I should make that clearer: The 24 hours in the day can be grouped but I'm more interested in grouping over time so returning 365 results for each year the tracking is in place, so total's for each day passed, then being able to select a range of dates and see more details on hours/minutes accessed over those times selected.
This is why I've titled it as using PHP, as I'd expect this might be easier with a PHP loop to generate the hours/days etc?
Peter
SELECT COUNT(*), HOUR(timecode)
FROM timecodeTable
GROUP BY HOUR(timecode);
Your result set, given the above data, would look as such:
+----------+----------------+
| COUNT(*) | HOUR(timecode) |
+----------+----------------+
| 10 | 18 |
+----------+----------------+
Many more related functions can be found here.
Edit
After doing some tests of my own based on the output of your comment I determined that your database is in a state of epic fail. :) You're using INT's as TIMESTAMPs. This is never a good idea. There's no justifiable reason to use an INT in place of TIMESTAMP/DATETIME.
That said, you'd have to modify my above example as follows:
SELECT COUNT(*), HOUR(FROM_UNIXTIME(timecode))
FROM timecodeTable
GROUP BY HOUR(FROM_UNIXTIME(timecode));
Edit 2
You can use additional GROUP BY clauses to achieve this:
SELECT
COUNT(*),
YEAR(timecode),
DAYOFYEAR(timecode),
HOUR(timecode)
FROM timecodeTable
GROUP BY YEAR(timecode), DAYOFYEAR(timecode), HOUR(timecode);
Note, I omitted the FROM_UNIXTIME() for brevity.