Select Count Distinct - sql

I would like to count the number of installations of each Member in a table similar to this. But this count distinct drives me nuts...
MemberID | InstallDate
1 | Yesterday
2 | Today
1 | Today
3 | Today
The above table should produce something like this one..
MemberID | CountNumberOfInstallations
1 | 2
2 | 1
3 | 1
p.s. I know it sound's like homework, but it isnt.

It looks like the query you are looking for is:
SELECT MemberID, COUNT(*)
FROM Table
GROUP BY MemberID
The DISTINCT keyword is not required. If order is required, you can use:
SELECT MemberID, COUNT(*)
FROM Table
GROUP BY MemberID
ORDER BY MemberID ASC

Related

SQL Query find users with only one product type

I solemnly swear I did my best to find an existing question, may I'm not sure how to phrase it correctly.
I would like to return records for users that have quota for only one product type.
| user_id | product |
| 1 | A |
| 1 | B |
| 1 | C |
| 2 | B |
| 3 | B |
| 3 | C |
| 3 | D |
In the example above I'd like a query that only returns users who carry quota for only one product type - doesn't really matter which product at this point.
I tried using select user_id, product from table group by 1,2 having count(user) < 2 but this does not work, nor does select user_id, product from table group by 1,2 having count(*) < 2
Any help is appreciated.
Your having clause is good; the issue's with your group by. Try this:
select user_id
, count(distinct product) NumberOfProducts
from table
group by user_id
having count(distinct product) = 1
Or you could do this; which is closer to your original:
select user_id
from table
group by user_id
having count(*) < 2
The group by clause can't take ordinal arguments (like, e.g., the order by clause can). When grouping by a value like 1, you're in fact grouping by the literal value 1, which would just be the same for any row in the table, and thus will group all the rows in the table to one group. Since there are more than one product in the entire table, no rows will be returned.
Instead, you should group by the user_id:
SELECT user_id
FROM mytable
GROUP BY user_id
HAVING COUNT(*) = 1
If you want the product, then do:
select user_id, max(product) as product
from table
group by user_id
having min(product) = max(product);
The having clause could also be:
having count(distinct product) = 1

Users that returned

I have a table with [EVENT_DATE] and [USER_ID], I would like to know how to count the users who came back in the same month.
Table_01
-------------------------------------------
EVENT_DATE | USER_ID
-------------------------------------------
2017-03-28 00:00:25.000 | 0006235012201
2017-03-04 23:00:00.000 | 0006235012201
2017-03-19 00:25:15.000 | 0006235012201
2017-02-03 10:00:02.000 | 0006235012202
2017-01-18 00:15:00.000 | 0006235012202
2017-03-28 11:00:15.000 | 0006235012202
2017-03-23 15:20:02.000 | 0006235012203
2017-02-18 12:00:06.000 | 0006235012203
2017-03-21 16:05:09.000 | 0006235012203
The answering being 2, because users 0006235012201 & 0006235012203 both came back within the same month.
EDIT: Sorry
I am looking to get the count by month.
-----------------------
Month | Users Returned
-----------------------
01/17 | 70
02/17 | 60
03/17 | 10
This is what I have, but it isn't correct as it seems to be listing users.
SELECT A.[USER_ID], A.[EVENT_DATE], COUNT(*)
FROM(
SELECT [USER_ID], [EVENT_DATE], COUNT(*)
FROM Table_01
GROUP BY [USER_ID], [EVENT_DATE]
HAVING COUNT(*) > 1
) A
GROUP BY A.[USER_ID], A.[EVENT_DATE]
Microsoft SQL Server 2016. Compatibility level: SQL Server 2005 (90)
select user_id
from your_table
group by user_id
having count(distinct year(event_date) * 100 + month(event_date)) > 1
Try the below script
SELECT DISTINCT USER_ID
FROM Table_01
GROUP BY USER_ID, (YEAR(EVENT_DATE)*100)+MONTH(EVENT_DATE)
HAVING COUNT(*) > 1
Use below query to get users returned in same month
SELECT USER_ID
FROM #Table
GROUP BY USER_ID,(YEAR(EVENT_DATE)*100)+MONTH(EVENT_DATE)
HAVING COUNT(*) > 1
Using this you can get month wise result
select month(EVENT_DATE),year(EVENT_DATE), USER_ID from tableName
where year(EVENT_DATE)=2017
Group by USER_ID,year(EVENT_DATE),month(EVENT_DATE)
Having HAVING COUNT(*) > 1
if you want some specific month wise you can add where condition like month(EVENT_DATE) =3
You need two levels of aggregation. First to get the users who have more than one row in a month. Then grouping by year,month to get the counts of users for a specific month.
select cast(mth as varchar(2))+'/'+cast(yr as varchar(4)) as mth,count(*)
from (select user_id,month(event_date) as mth,year(event_date) as yr
from tablename
group by user_id,month(event_date),year(event_date)
having count(*) > 1
) t
group by cast(mth as varchar(2))+'/'+cast(yr as varchar(4))

SQL QUERY USING POSTGRESQL

I have the following query:
SELECT DISTINCT "stylists".* FROM "stylists"
INNER JOIN "category_stylists" ON "category_stylists"."stylist_id" = "stylists"."id"
WHERE category_stylists.category_id IN (1,2)
But I want to order the stylists by categories.
For example:
Stylists
id | Name
1 Sebastian
2 Jhon
Categories
id | Name
1 Wedding
2 Office
Stylist_Categories
id | stylist_id | category_id
1 1 1
2 2 1
3 2 2
So If I apply my query, I got both Stylists, but I need always order by how many categories has the stylist. I mean in this example Jhon will be the first row because has the category_id 1 and category_id 2.
Result expected:
Stylists
id | Name
2 Jhon
1 Sebastian
Thanks in advance!
Try counting categories for each stylist and order by it:
SELECT "id","name" from(
SELECT "stylists"."id", "stylists"."Name",count(stylists.id) as cnt FROM "stylists"
INNER JOIN "category_stylists" ON "category_stylists"."stylist_id" = "stylists"."id"
WHERE category_stylists.category_id IN (1,2)
group by "stylists"."id", "stylists"."Name")
order by cnt desc

SQl - select columns and group them

I have a table like this below and i need the result to be like this when i run the query
Results
title | count
----------------
foo | 3
bar | 2
Table
customer_id | title
-------------------
55 | foo
22 | foo
55 | bar <-- duplicate
23 | bar
55 | bar <-- duplicate
23 | foo
UPDATE Thank you all for the quick response!
The trick is to count the distinct customer ids, so you won't count the double Foo for customer 55.
If you need to, you can order the results by that count too, or you can just leave out the order by clause.
select
title,
count(DISTINCT customerid) as `count`
from
yourTable
group by
title
order by
`count` desc
It's as easy as this:
select A.title, count(*) as count -- an usual count(*)
from
(select distinct customer_id, title -- this query removes the duplicates
from table) A
group by A.title -- an usual group by
For SQL Server you can do it like this.
select
t.title ,
count(*)
from your_table as t
group by
t.title
order by count(*) DESC

SQL distinct rows and count

I have a table like this:
Id CookieId
--------------------------------
4 13ab1dc1bac-ef74565ea9ff5ba8
4 13b474728b3-6cf7bf445e311c59
4 13b474728b3-6cf7bf445e311c59
4 13a1b545ebf-20c86b23c91ad2c5
4 13a1b545ebf-20c86b23c91ad2c5
The result should be only the distinct rows:
Id CookieId
--------------------------------
4 13ab1dc1bac-ef74565ea9ff5ba8
4 13b474728b3-6cf7bf445e311c59
4 13a1b545ebf-20c86b23c91ad2c5
More of that if I can get the count of the rows would be also good.
Id count(CookieId)
-----------------------
4 3
How can I achieve this two result?
To get unique records in result use this:
SELECT DISTINCT ID, CookieId
FROM Table1
Result:
| ID | COOKIEID |
-------------------------------------
| 4 | 13ab1dc1bac-ef74565ea9ff5ba8 |
| 4 | 13b474728b3-6cf7bf445e311c59 |
| 4 | 13a1b545ebf-20c86b23c91ad2c5 |
To get count of unique CookieId use DISTINCT inside COUNT function like this:
SELECT ID, COUNT(DISTINCT CookieId) AS `COUNT`
FROM Table1
GROUP BY ID
Result:
| ID | COUNT |
--------------
| 4 | 3 |
See this SQLFiddle
First to select the distinct values use
SELECT DISTINCT CookieId, Id
FROM YourTable;
GO
This is only working because you have duplicates across two columns. To get the count of those values use
SELECT Id, COUNT(DISTINCT CookieId)
FROM YourTable;
GROUP BY Id;
GO
I hope this helps.
you can try this query
select Id,count(CookieId) from table_name group by CookieId;
select Id, CookieId, count(1) as Amount
from Table1 group by Id, CookieId
SELECT DISTINCT(`CookieId`),Id, COUNT(CookieId) FROM
`tablename` GROUP BY `CookieId`