Find average amount of times a value appears in table - sql

How do I count the average amount of times a given number appears in a database?
id | ...
----------
1 | ...
5 | ...
2 | ...
3 | ...
3 | ...
1 | ...
6 | ...
4 | ...
3 | ...
...| ...
id corresponds to the id of the user. Perhaps the table is for customer orders or donations made by a user. For the above table:
id 1 = 2 entries
id 2 = 1 entry
id 3 = 3 entries
id 4 = 1 entry
id 5 = 1 entry
id 6 = 1 entry
Average = (2+1+3+1+1+1)/6 = 1.5 entries per user
The average number of orders/donations made per user is 1.5 to give an example.
I could do something like the below:
$getTotalEntries = $db->prepare("
SELECT *
FROM table
");
$getTotalEntries->execute();
$totalEntries = $getTotalEntries->rowCount();
$getGroupedEntries = $db->prepare("
SELECT *
FROM table
GROUP BY id
");
$getGroupedEntries->execute();
$groupedEntries = $getTotalEntries->rowCount();
$average = $totalEntries/$groupedEntries;
I'm hoping for a single SQL request, however. Incidentally, the below gives me the number of occurances of a given id, but I cannot AVG() them.
$getAverageEntries = $db->prepare("
SELECT id, COUNT(*)
FROM table
GROUP BY id
"); // works, returns the 2,1,3,1,... from before
$getAverageEntries = $db->prepare("
SELECT AVG(COUNT(*))
FROM table
GROUP BY id
"); // won't find aggregate count

How about this?
select count(id) / count(distinct id) as avgEntriesPerUser
from table t;
The only issue with this would be a NULL value for id. If this occurred (and I find it highly unlikely for a column named id), then the above ignores those rows entirely. It can be modified to take this situation into account.

select avg(a.entryCount)
from (
select id, count(id) as entryCount
from <tablename>
group by id
) a;

in SQL you need to do a count to get the number of entries
select avg(entries) from(
Select Distinct Id.tableName, count(Id) As Entries
from tableName
group by ID)

You mean?
select avg(countPerID) from (
select id, count(*) as countPerID from table group by id) x

Related

How to get MAX Hike in Min month?

below is table:
Name | Hike% | Month
------------------------
A 7 1
A 6 2
A 8 3
b 4 1
b 7 2
b 7 3
Result should be:
Name | Hike% | Month
------------------------
A 8 3
b 7 2
Here is one way of doing this:
SELECT Name, [Hike%], Month
FROM
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY Name ORDER BY [Hike%] DESC, Month) rn
FROM yourTable
) t
WHERE rn = 1
ORDER BY Name;
If you instead want to return multiple records per name, in the case where two or more records might be tied for having the greatest hike%, then replace ROW_NUMBER with RANK.
use correlated subquery
select Name,min(Hike) as Hike,min(Month) as Month
from
(
select * from tablename a
where Hike in (select max(Hike) from tablename b where a.name=b.name)
)A group by Name
You can use something similar to the below:
SELECT Name, MAX(Hike), Month
FROM table
GROUP BY Name, Month
Hope this helps :)

Excluding records where criteria meets

I have a set of records where we identify several items connected to a customer.
My dilemma is that if a customer has both items then I would like to exclude that customer.
If they only have one specific item then I want to include it.
I will be using this code to create a view so i'm trying to find the best way. I could try Row_number() to identify different records, but I'm not sure that would be ideal in this situation.
Example data table:
Customer | ItemID | value1 | Value2
A 12 35 0
B 12 35 0
C 13 0 25
C 12 0 25
D 18 225 12
Desired Output:
Customer | ItemID | value1 | Value2
A 12 35 0
B 12 35 0
This is what I have so far:
select Customer, ItemID, Value1, Value2
from Table1
where itemID = 12
This would give me customer 'C', which I don't want.
If you want customers who have itemid = 12 but not itemid = 13 you can use NOT EXISTS:
select * from tablename t
where itemid = 12
and not exists (
select 1 from tablename
where customer = t.customer
and itemid = 13
)
If you want customers who have itemid = 12 and not any other itemid:
select * from tablename t
where itemid = 12
and not exists (
select 1 from tablename
where customer = t.customer
and itemid <> 12
)
or:
select * from tablename
where customer in (
select customer from tablename
group by customer
having min(itemid) = 12 and max(itemid) = 12
)
I think you need to clarify your question but, as I understand it, you're looking to return the all rows where:
1) A customer has a particular item (i.e. Item ID 12, which excludes customer D)
and
(2) They only have one item in total, which excludes customer C since they have two items.
If that is the case, then here's what I've got:
SELECT *
FROM Table1
WHERE ItemID == '12' AND
Customer in (
SELECT Customer
FROM Table1
GROUP BY Customer
HAVING Count(Customer) = 1
)
Edit: I clarified my interpretation of OP's question. I also tested my solution on SQL Fiddle (http://sqlfiddle.com/#!5/b5f1f/2/0) and updated the WHERE clause accordingly.

Find duplicate sql record

I have a database table in vbulletin, where a table has just 2 columns:
Col 1 : userid
Col 2 : relationid
Col1 may have multiple entries, like:
userid relationid
1 A
1 B
1 C
2 B
2 T
I would like to extract a csv or just manage to order them, so i could end up with some thing like this:
userid entries
1 100
2 12
3 44
4 33
5 33
Where userid is the repeating number in col1, and entries is how many times the userid is repeating itself.
Try like this;
select userid, count(*) as entries from table group by userid
SELECT userid, COUNT(userid) FROM TableName GROUP BY userid;
This query will return userid and a count of all userids from TableName. Because we are using COUNT, we also must GROUP BY any additional columns we are selecting.

SQL Selecting rows with multiple values

I have these 2 tables:
Table SW_ITEM:
ID SWID ITEM_ID
1 1 99
2 2 99
3 5 99
4 2 100
5 1 100
6 1 101
7 2 102
Table ITEM:
ID FILENAME
99 abc
100 def
101 geh
102 ijk
column ITEM_ID is a foreign key to the column ID of table ITEM.
So I want all filenames which have the SWID "1" AND "2" (that would be ITEMID 99 and 100, so their filenames are "abc" and "def")
Here I have to say that it is possible that ITEM_ID has more than one entry with the same SWID, so I cannot use this SQL:
SELECT ITEM_ID FROM SW_ITEM
WHERE SWID IN (1,2)
GROUP BY ITEM_ID
HAVING COUNT(ITEM_ID) = 2
So is there any other possibility to get all entries which have the SWID 1 and 2 (creating a join for every SWID is also not an option - because with many entries it would be really slow)
Kind regards
You need to use DISTINCT in COUNT and count SWID instead of ITEM_ID:
SELECT ITEM_ID FROM SW_ITEM
WHERE SWID IN (1,2)
GROUP BY ITEM_ID
HAVING COUNT(DISTINCT SWID) = 2;
Please checkout this demo.
To retrieve all filenames, try:
SELECT ITEM_ID, FILENAME
FROM ITEM JOIN SW_ITEM ON ITEM.ID = SW_ITEM.ITEM_ID
WHERE SWID IN (1,2)
GROUP BY ITEM_ID
HAVING COUNT(DISTINCT SWID) = 2;
Demo
I have a little different problem where I have to find a person with multiple entries in the same table based on email for that the above solution didn't work for me. You can try using the following,
SELECT person_id,
(ROW_NUMBER () OVER (PARTITION BY pers_email ORDER BY pers_name) person_count
from pers_table
WHERE person_count > 2;
Try this hope it works :)

SQL MAX(column) With Additional Criteria

I have a single table, where I want to return a list of the MAX(id) GROUPed by another identifier. However I have a third column that, when it meets a certain criteria, "trumps" rows that don't meet that criteria.
Probably easier to explain with an example. Sample table has:
UniqueId (int)
GroupId (int)
IsPriority (bit)
Raw data:
UniqueId GroupId IsPriority
-----------------------------------
1 1 F
2 1 F
3 1 F
4 1 F
5 1 F
6 2 T
7 2 T
8 2 F
9 2 F
10 2 F
So, because no row in groupId 1 has IsPriority set, we return the highest UniqueId (5). Since groupId 2 has rows with IsPriority set, we return the highest UniqueId with that value (7).
So output would be:
5
7
I can think of ways to brute force this, but I am looking to see if I can do this in a single query.
SQL Fiddle Demo
WITH T
AS (SELECT *,
ROW_NUMBER() OVER (PARTITION BY GroupId
ORDER BY IsPriority DESC, UniqueId DESC ) AS RN
FROM YourTable)
SELECT UniqueId,
GroupId,
IsPriority
FROM T
WHERE RN = 1