Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a table and it looks like this:
Num1 num2 num3
----------------
1 2 2<----- grouped numbers
1 2 3<----- another group
1 2 3<----- same numbers so I have a value of 2
2 4 4
2 2 3
2 4 3
3
3
4
I would like to know how to give groups of numbers, a number value.
Example1
1, 2, 2 are grouped horizontally
Example 2
1,2,2 this combination is shown x amount of times
1,2,2 times = 1
Example 3
1,2,3 = 2 times
2,3,3 = 4 times
This works but only on single numbers
select num, count(*)Times
from Numbers cross apply
(values (F2), (F3), (F4),(F5),(F6),(F7),(F8)) v(num)
where num is not null
group by num
order by num;
This also works but same problem
select value, count(*)
from Numbers
unpivot
(
value
for col in (F2, F3, F4,F5,F6,F7,F8,F9)
) u
group by value
ORDER BY 1;
The idea is to expand this to 16 columns and search all rows to find the matching
sets of numbers in each row.
Give an output of example 3, "times" being the column name
The amount of rows=2000,columns=16
if anyone can help please post
Oh, I think you want the count of each number. You can do this by unpivoting and aggregating:
select num, count(*)
from t cross apply
(values (num1), (num2), (num3)) v(num)
where num is not null
group by num
order by num;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I have these three different result set.
If typeId 7 is duplicated between two different Ids, then they count as two. Any TypeId other than 7, count should distinct of Ids.
First table count is 5 because Id 1 is unique between 4 & 7. Id 2 is 3 times but other than Type 7, it is distinct. So, it count as 2.
How can I write a query for this.
I tried Distinct(Id) + Distinct Iff(TypeId = 7) and of course this return wrong result.
Count those that are not 7 distinct. (NULL isn't counted)
Then add the total unique id's of 7.
COUNT(DISTINCT CASE WHEN TypeId = 7 THEN NULL ELSE Id END) +
COUNT(DISTINCT CASE WHEN TypeId = 7 THEN Id END)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to count the number of of occurences of integers in an array for each record.
My table is this:
customerid
groups
1
{324,299,523,534,565,212}
2
{324,299,523,534,565,212}
3
{324}
4
{324,299,523,212}
I would like to return the following:
groupid
count
324
4
299
3
523
3
etc
I have tried looping through the cursor and storing count in map but I would like something more performant.
select
groupid,
count(*)
from (
select unnest(groups) as groupid
from table_name
) data
group by groupid
order by count desc
you can use unnest:
select unnest(groups) groupid
,count(*) count
from customergroups
group by groupid
order by count desc
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I know how to do running sum but can't figure out how to do running subtract.
Example: we have single column of data and a new column will be formed for running subtraction.
Value
------
10
1
3
4
Output needs to be:
10
9
6
2
I need both these columns next to each other
Any suggestion please.
Your question only makes sense if you have a column that specifies the ordering. In that case:
select (case when row_number() over (order by <ordercol>) = 1
then col
else 2 * first_value(col) over (<ordercol>) - sum(col) over (order by <ordercol>)
end) as output
from t;
That is, return the column value on the first row. Otherwise, the math is a little tricky. But you want the first value minus the sum of the rest of the column. Arithmetically, this is the same as twice the first value minus the cumulative sum.
EDIT:
As Shawn points out, this can be simplified to:
select 2 * first_value(col) over (<ordercol>) - sum(col) over (order by <ordercol>) as output
from t;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have two questions, one is how I can put these two queries together (add just the right column). Example:
Query 1
Phase menCount
A 40
B 50
C 60
D 20
Query 2
Phase womenCount
A 60
B 50
C 40
Wanted query result
Phase menCount womenCount
A 40 60
B 50 50
C 60 40
D 20 NULL
The second question is, how I can put together columns from non-related queries. Is this possible at all? I have at least 10 different srcripts and if I can put them into one call, it would be easier. Example:
Query 1
Phase phaseCount
A 20
B 40
C 60
Query 2
Stage stageCount
W 90
X 120
Y 150
Z 190
Wanted query result
Phase phaseCount Stage stageCount
A 20 W 90
B 40 X 120
C 60 Y 150
Z 190
Thanks for answers.
To put two queries together you can treate them as "table expressions". For example
select
coalesce(a.phase, b.phase) as phase,
a.mencount,
b.womencount
from (
-- query #1 here
) a
full join (
-- query #2 here
) b on a.phase = b.phase
To put unrelated queries together you need to produce a joining condition somewhere. For example, you can modify each query to produce an artificial row number using the ROW_NUMBER() function; then you can use this value to join against each other, as in:
select
a.*, b.*
from (
-- modified query #1 now
select
...,
row_number() over(order by ...) as rn
from ...
) a
full join (
-- modified query #2 now
select
...,
row_number() over(order by ...) as rn
from ...
) b on a.rn = b.rn
order by coalesce(a.rn, b.rn)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Hi can anyone help me with sum up first two rows in table and then rest would be same. example is
ID SUM
12 60
0 20
1 30
2 50
3 60
I am expecting
ID SUM
0 80
1 30
2 50
3 60
I am doing this from memory - so if this doesnt work let me know and we can do it another way looking at the row number;
Assuming you have a unique ID to sort it by as you suggested, you could do something like this;
you may want to change the order to be desc if that's how you classify your 'top 2'
SELECT TOP 2 ID,
SUM(VALUE)
FROM [Table]
GROUP BY ID
ORDER BY ID
UNION
SELECT ID,
VALUE
FROM [Table]
WHERE ID NOT IN (SELECT TOP 2 ID
FROM [Table] ORDER BY ID)