I am trying to count unique values on a per user basis and end up with a combined count. They may exist more than once per user, but should only be counted once per user.
Example:
user value
1 AAA
1 AAA
1 BBB
1 CCC
2 AAA
2 CCC
2 CCC
3 AAA
3 BBB
3 BBB
3 BBB
Expected result with count:
AAA 3
BBB 2
CCC 2
So values should only be counted once per user, no matter how many times they are present.
I have gotten as far as counting the total number of values with this:
SELECT value, COUNT(value) FROM table GROUP BY value")
But this counts all instances of each value, I cannot work out how to count only the unique values per user and the combine. Hope this makes sense! Many thanks!
Try this:
SELECT value, COUNT(distinct user) FROM table GROUP BY value
Related
table
id text
1 aaa
121 bbb
4 ccc
1 ddd
new table
id text2
1 aaaddd
121 bbb
4 ccc
I do not think I can use PIVOT since I never know how many and what id and text values would be so I cannot hardcode them in a PIVOT instruction.
use group by with string_agg
select id,string_agg(text,'') as text2
from table
group by id
I am trying to get a simple recount of how many penalties each user accumulates in my company's website, but avoiding the users that have been deleted (my rows are static, but the new rows added each week have updated user info)
So I have this:
penalty_id date user user_status
1 22/09 aaa active
2 23/09 bbb active
3 23/09 ccc active
4 01/10 bbb active
5 02/10 aaa deleted
And I would like to have this:
user pentaly_count
bbb 2
ccc 1
So far I have come up with this
=query(Penalties!A:D;"select C, count(A) where D !='deleted' group by C label C 'user', count(A) 'penalty_count'")
So I end up with this:
user pentaly_count
bbb 2
aaa 1*
ccc 1
The query skips the count of penalty with id 5, but I want it to skip user aaa entirely.
I could have managed this in SQL doing a simple subquery or even a join, but everything I come up with, Google Sheets says it does not work.
Please try this
select C, count(A)
group by C
having min(case when D ='deleted' then 1 end) is null
label C 'user', count(A) 'penalty_count'
I have the following problem.
Imagine I get the following return table from a select statement
Column A Column B
100 aaa
100 bbb
100 ccc
200 ddd
300 eee
So the question is, how can I change my SQL Select statement to add a new column that shows the numbers of times the Column A has a repeat value. The problem is that I need to get some subgrups with an order.
For example, it should return something like:
Column A Column B Column C
100 aaa 1
100 bbb 2
100 ccc 3
200 ddd 1
300 eee 1
Thank you very much for your support!
This is the classic usecase for the analytic RANK() function:
SELECT a, b, RANK() OVER (PARTITION BY a ORDER BY b) AS c
FROM my_table
Add ROW_NUMBER() OVER (PARTITION BY ColA ORDER BY SomethingElse) as ColC. That gives you a sequential row number per "group" in ColA.
I have table below:
SerialNumber Name Product
1 aaa a
2 bbb b
3 ccc c
I would like to convert to table below:
serialNumber PropertyName value
1 Name aaa
1 Product a
2 Name bbb
2 Product b
3 Name ccc
3 Product c
How can i achieve this in SSIS 2012?
You have Pivot and Unpivot Data Flow Transformations, but even simpler it will be for you to use Multicast, to make two streams out of one. Then in one stream you'll be passing Names and values, an in the other Products and values. Then use Union All to join the streams again.
i have one table, let's call it 'TBL'.
i have one column that have only 3 values available.(let's say 'AAA', 'BBB', 'CCC')
the values can return multiple times.
for example:
TBL
---
Column1
-------
AAA
AAA
BBB
CCC
BBB
CCC
BBB
CCC
AAA
i want to create a table result that looks like this:
TBL-RESULT
----------
AAA+BBB 60%
CCC 40%
i want to show AAA and BBB in one result and there precentage from all values in one line,
and CCC in a second line as well.
the big problem is also that i need to do so in sql of ACCESS (2007).
can someone help me?
thank you,
gady m
Assume table is called MyTable and column is MyColumn
select IIF(MyColumn<>'CCC', 'AAA+BBB', 'CCC'),
100*count(MyColumn='CCC')/(select count(*) from MyTable) from MyTable
group by MyColumn='CCC'