SQL distinct rows and count - sql

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`

Related

Order By Id and Limit Offset By Id from a table

I have an issue similar to the following query:
select name, number, id
from tableName
order by id
limit 10 offset 5
But in this case I only take the 10 elements from the group with offset 5
Is there a way to set limit and offset by id?
For example if I have a set:
|------------------------------------|---|---------------------------------------|
| Ana | 1 | 589d0011-ef54-4708-a64a-f85228149651 |
| Jana | 2 | 589d0011-ef54-4708-a64a-f85228149651 |
| Jan | 3 | 589d0011-ef54-4708-a64a-f85228149651 |
| Joe | 2 | 64ed0011-ef54-4708-a64a-f85228149651 |
and if I have skip 1 I should get
|------------------------------------|---|---------------------------------------|
| Jana | 2 | 589d0011-ef54-4708-a64a-f85228149651 |
| Jan | 3 | 589d0011-ef54-4708-a64a-f85228149651 |
I think that you want to filter by row_number():
select name, number, id
from (
select t.*, row_number() over(partition by name order by id) rn
from mytable t
) t
where
rn >= :number_of_records_per_group_to_skip
and rn < :number_of_records_per_group_to_skip + :number_of_records_per_group_to_keep
The query ranks records by id withing groups of records having the same name, and then filters using two parameters:
:number_of_records_per_group_to_skip: how many records per group should be skipped
:number_of_records_per_group_to_skip: how many records per group should be kept (after skipping :number_of_records_per_group_to_skip records)
This might not be the answer you are looking for but it gives you the results your example shows:
select name, number, id
from (
select * from tableName
order by id
limit 3 offset 0
) d
where id > 1;
Best regards,
Bjarni

Get row which matched in each group

I am trying to make a sql query. I got some results from 2 tables below. Below results are good for me. Now I want those values which is present in each group. for example, A and B is present in each group(in each ID). so i want only A and B in result. and also i want make my query dynamic. Could anyone help?
| ID | Value |
|----|-------|
| 1 | A |
| 1 | B |
| 1 | C |
| 1 | D |
| 2 | A |
| 2 | B |
| 2 | C |
| 3 | A |
| 3 | B |
In the following query, I have placed your current query into a CTE for further use. We can try selecting those values for which every ID in your current result appears. This would imply that such values are associated with every ID.
WITH cte AS (
-- your current query
)
SELECT Value
FROM cte
GROUP BY Value
HAVING COUNT(DISTINCT ID) = (SELECT COUNT(DISTINCT ID) FROM cte);
Demo
The solution is simple - you can do this in two ways at least. Group by letters (Value), aggregate IDs with SUM or COUNT (distinct values in ID). Having that, choose those letters that have the value for SUM(ID) or COUNT(ID).
select Value from MyTable group by Value
having SUM(ID) = (SELECT SUM(DISTINCT ID) from MyTable)
select Value from MyTable group by Value
having COUNT(ID) = (SELECT COUNT(DISTINCT ID) from MyTable)
Use This
WITH CTE
AS
(
SELECT
Value,
Cnt = COUNT(DISTINCT ID)
FROM T1
GROUP BY Value
)
SELECT
Value
FROM CTE
WHERE Cnt = (SELECT COUNT(DISTINCT ID) FROM T1)

How to take unique IDs of the following DISTINCT query?

I have this table:
id | description | URL | role
1 | desc_1 | url_1 | 1
2 | desc_1 | url_1 | 2
3 | desc_2 | url_2 | 1
4 | desc_2 | url_2 | 2
I want to get the following result
id | description | URL
1 | desc_1 | url_1
3 | desc_2 | url_2
I already tried this but to no avail
SELECT DISTINCT(description), id FROM table GROUP BY description
SELECT MAX(id),descritpion,URL GROUP BY description,URL
or
SELECT MIN(id),descritpion,URL GROUP BY description,URL
depends if u want higher or lowest id.
SELECT MIN(id)
,descritpion
,URL
FROM myTable
GROUP BY description
,URL
Group By clause is generally used with aggregate functions. There are five aggregate functions in SQL. MIN , MAX , COUNT , SUM and AVG. In your situation you can use MIN or MAX functions to accomplish your work.
select MIN(id),descritpion,URL from table GROUP BY description , URL
or
select MAX(id),descritpion,URL from table GROUP BY description , URL
SELECT t.id, t.description, t.URL
FROM
(
SELECT id, description, URL,
ROW_NUMBER() OVER (PARTITION BY description, URL ORDER BY role) rn
FROM yourTable
) t
WHERE t.rn = 1
Using MIN Clause to get result :
SELECT MIN(id),description,URL
FROM table GROUP BY description,URL

SQL: keep one record from n-records with same foreign key

I have some records and want only to keep the lowest (min) number from a customer:
This:
Customer | Number
1 | 2
1 | 4
2 | 1
1 | 3
2 | 2
should be tranformed via sql to:
Customer | Number
1 | 2
2 | 1
using Sybase ADS local table.
You should be able to use min() and group by the customer:
select customer, min(number)
from yourtable
group by customer
See SQL Fiddle with Demo
SELECT Customer, MIN(Number)
FROM your_table
GROUP BY Customer;

Select Count Distinct

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