I have the following table
id Desc User
1 Print 14
2 Print 7
3 Copy 14
4 Print 19
5 Copy 7
6 Copy 19
7 Attach 19
What I'm trying to do is make a column that tells the number of rows per user.
Like this
id User Count
1 14 2
2 7 2
4 19 3
The Point of the report is to show how many activities each user has done.
I need to group by user and get the number of rows within each user.
The problem is, I'm not exactly sure how to do that, is it a unique statement somewhere?
Here's my query so far.
Select id
,User
From Table
Group By User
I am unsure how to implement the count though.
You should be able to get your result with the COUNT function:
SELECT
MIN(id),
User,
COUNT(User) AS Count
FROM
`table`
GROUP BY
User
Because you can get only one id value per User I assumed from your data that you want the minimum one.
Related
The table I'm trying to query records the outcome of a specific phone call that was made. So each row has a case_id, last_called (date/timestamp), an outcome (SIQdisp1_a), and then a few other pieces of info (batch number, employee number).
Here is the syntax I'm using currently:
SELECT
DATE(last_called), COUNT(SIQdisp1_a), Access.batch_number
FROM
disposition_log
LEFT JOIN
Access ON disposition_log.case_id = Access.identifier
WHERE
SIQdisp1_a = 14
AND case_id NOT LIKE 'test%'
AND batch_number = 171205
GROUP BY DATE(last_called);
So this will count the number of outcomes per day that = 14 (and are in the appropriate batch). What I need to do, is to modify it such that it only counts the outcome if it is the most recent outcome for that case_id. for instance, if a person was called 3 times in one day, and all 3 times the outcome is equal to 14, I only need that to count once. The syntax I have currently would add all 3 instances of a 14 to the final count.
I've tried using
SELECT MAX(DATE(last_called), COUNT(SIQdisp1_a)
but that returns the same results as the original syntax. I feel like I'm missing something basic here. . . any suggestions are appreciated!
Edit (Including Sample Data):
case_id last_called SIQdisp1_a
1002175 2018-02-16 12:42:36 14
1002175 2018-02-16 13:20:11 14
1005695 2018-02-15 12:00:00 14
1003018 2018-02-15 12:00:00 13
1003018 2018-02-15 11:59:00 14
1005974 2018-02-15 14:33:33 14
Sorry that I didn't include sample data the first time around. With this sample data, each row is a call to somebody (case_id), the date/time of the call (last_called) and the outcome of that call (SIQdisp1_a). What I need, is a count of the number of 14's per day, but the 14 must be the most recent entry for that ID. So in the dataset above, the result would be something like:
Date(last_called) COUNT(SIQdisp1_a)
2018-02-16 1
2018-02-15 2
So for 2-16, the count would be 1 because I ONLY want to count the most recent record. If that most recent record is a 14, it gets added to the total. For 2-15, it's a total of 2 because the most recent outcome for cases 1005974 and 1005695 is 14. Case 1003018 should not be counted because even though there was a 14 at 11:49, there was a call at 12:00 with an outcome of 13. The syntax I posted at the top counts EVERY instance of 14 it sees, it doesn't look for the most recent. Thank you for all your help!
1 option with a subquery:
select date(last_called), count(case_id) from <table> where (case_id,last_called) in (select case_id, max(last_called) from <table> group by case_id,date(last_called)) and SIQdisp1_a=14 group by date(last_called);
I have a table that I use to record the activity log of an application. Now I have to get all records from entity X that have been published in some range of dates. So if a record has been published and later unpublished, it doesn't have to appear in the results.
I don't know how to explain it really, it's find the last appearance of each one and then catch values that are "1" or "0", on depends that I need in each case.
A simplified example (the real table has more fields and more data):
id user_id date model main_relation_id field new_value
1 24 2017-03-21 A 1 publish 1
2 24 2017-03-21 A 2 publish 1
3 24 2017-03-22 A 3 publish 0
4 24 2017-03-22 A 2 update some text
5 24 2017-03-23 A 1 publish 0
6 24 2017-03-23 A 1 update some text
7 24 2017-03-24 A 3 publish 1
8 24 2017-03-24 A 2 publish 0
9 24 2017-03-24 A 2 update some text
10 24 2017-03-25 A 1 publish 1
11 24 2017-03-25 A 2 publish 1
11 24 2017-03-26 A 3 publish 0
I need to get main_relation_id, filtering by model and date, so if I want to get all registers from model A that have been published between 2017-03-21 and 2017-03-24 I'll get:
model_main_relation_id
1
3
and if I want to get all registers that have been unpublished in the same dates, the result have to be:
model_main_relation_id
2
How can I get this result?
So, you would like to filter on the latest status by main_relation_id, whether the particular main_relation_id has been published (field='publish'; new_value=1) or unpublished (field='publish'; new_value=0) within a period.
Since the dates within the date field of the sample data are equal for multiple records, therefore I must assume that a higher value in the id field means later event. Therefore the max(id) per main_relation_id would yield the latest even record.
What I would do is to get the max(id) per main_relation_id within a date range for a particular model in a derived table where field is 'publish' and join this back to your table to find out whether the particular main_relation_id was published:
select table.main_relation_id
from table
inner join
(select main_relation_id, max(id) as maxid
from table
where date>=... and date<=... and `field`='publish' and model='...'
group by main_relation_id) t on table.id=t.maxid
where table.new_value=1
You need to substitute the filter criteria in place of the .... If you would like to get the unpublished data, then replace table.new_value=1 criterion with table.new_value=0.
At first I would like greet all Users and apologize for my english :).
I'm new user on this forum.
I have a question about MySQL queries.
I have table Items with let say 2 columns for example itemsID and ItemsQty.
itemsID ItemsQty
11 2
12 3
13 3
15 5
16 1
I need select itemsID but duplicated as many times as indicated in column ItemsQty.
itemsID ItemsQty
11 2
11 2
12 3
12 3
12 3
13 3
13 3
13 3
15 5
15 5
15 5
15 5
15 5
16 1
I tried that query:
SELECT items.itemsID, items.itemsQty
FROM base.items
LEFT OUTER JOIN
(
SELECT items.itemsQty AS Qty FROM base.items
) AS Numbers ON items.itemsQty <=Numbers.Qty
ORDER BY items.itemsID;
but it doesn't work correctly.
Thanks in advance for help.
SQL answer - Option 1
You need another table called numbers with the numbers 1 up to the maximum for ItemsQuantity
Table: NUMBERS
1
2
3
4
5
......
max number for ItemsQuantity
Then the following SELECT statement will work
SELECT ItemsID, ItemsQty
FROM originaltable
JOIN numbers
ON originaltable.ItemsQty >= numbers.number
ORDER BY ItemsID, number
See this fiddle -> you should always set-up a fiddle like this when you can - it makes everyone's life easier!!!
code answer - option 2
MySQL probably won't do what you want 'cleanly' without a second table (although some clever person might know how)
What is wrong with doing it with script?
Just run a SELECT itemsID, ItemsQty FROM table
Then when looping through the result just do (pseudo code as no language specified)
newArray = array(); // new array
While Rows Returned from database{ //loop all rows returned
loop number of times in column 'ItemsQty'{
newArray -> add 'ItemsID'
}
}//end of while loop
This will give you a new array
0 => 11
1 => 11
2 => 12
3 => 12
4 => 12
5 => 13
etc.
Select DISTINCT items.itemsID, items.itemsQty From base.items left outer join (select items.itemsQty as Qty from base.items) As Numbers On items.itemsQty <=Numbers.Qty
order by items.itemsID;
Use DISTINCT to remove duplicates. Read more here - http://dev.mysql.com/doc/refman/5.0/en/select.html
It seems like I understood what you asked differently than everyone else so I hope I answer you question. What I would basically do is -
create a new table for those changes.
Create a mysql procedure which given a line in the original table add new lines to the new table - http://dev.mysql.com/doc/refman/5.6/en/loop.html
Run this procedure for each line in the original table.
try this to get distinct values from both columns
SELECT DISTINCT itemsID FROM items
UNION
SELECT DISTINCT itemsQty FROM items
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 :) :)
Not sure how to ask this so it makes sense, but I'm trying to do a query like:
SELECT * FROM PAGES WHERE an attached record exists in PAGE_FILTERS and that record has a FilterTypeID of 22 AND another attached record exists in PAGE_FILTERS for the same page ID and that record has a filter type id of 27.
I have a structure like this:
PAGES table
PageID PageName
1 Page 1
2 Page 2
3 Page 3
PAGE_FILTERS table
PageID FilterTypeID FilterValueID
1 22 1
1 27 2
2 22 0
2 24 1
3 22 1
3 27 1
3 28 2
So, given FilterTypeID's of 22 and 27, my query should return PageID's 1 and 3. Page 2 doesn't get selected since 22 matches, but there's no record matching FilterTypeID 27.
Page 3 matches, even though there's an extra filter.
In other words, I know what filter types a page has to have, and I need to get all the pages that each have all the required types.
I'm not opposed to changing database structure if it makes more sense, but each page could have none, one, or many filter sets attached.
SELECT p.*
FROM pages p
JOIN PAGE_FILTERS pf
ON p.PageID = pf.PageID
AND pf.FilterTypeID IN (22,27)
GROUP BY p.PageID
HAVING COUNT(DISTINCT pf.FilterTypeID) = 2