We have age columns and in that we have single values or 15+ values we need to have single value or 15+ - sql

If source value is 3 or 4 then target value is 3 or 4. If source having any minus value then -1 and if source value is 15 or more than 15 then 15+.
Table 1 Table 2
Age column. Age column
3 3
4 4
15 15+
-2 -1
-3 -1
100 15+

you can use the CASE WHEN THEN END syntax for problems like that (https://www.w3schools.com/sql/sql_case.asp)
i assume you have 3 conditions:
negative values are always -1
up to 14 it is the original value
15 and above is 15+
that means, you have to cover these 3 cases with the CASE WHEN THEN END clause. Just evaluate what comes out from your select to the first table and then transform it to the wanted outcome

Related

Creating a column in metabase query tool depending on if another column contains unique value

I am trying to use the matabase query tool to create a new column of 1s and 0s.
example data
id value
1 10
2 0
2 15
2 10
I want to create a new column that contains a 1 if the id is unique and value > 0 and 0 otherwise. Using the query tool in Metabase I can create a new column that is 1 or 0 depending on if value > 0 using
case([value] > 0, 1, 0)
But this gives,
id value new_col
1 10 1
2 0 0
2 15 1
2 10 1
As I want to calculate sum([new_col]) later in the query this gives the wrong answer as I would get 3 instead of 2 (as only 2 unique id's have value > 0)
How can my case statement also check if the id is unique?

Can I split a dynamic semicolon delimited string into columns using T-SQL?

I have a table that looks like so:
Id SubNumber Values
1 1 1;4;8;3
2 2 8;9;7;10
3 3 41;45;23;0
I will not always only have 4 values and the number of "SubNumbers" can be greater than 3. Is there any way I can query this table to look like this?
Id SubNumber 1 2 3 4
1 1 1 4 8 3
2 2 8 9 7 10
2 3 41 45 23 0
The rows will always have the same number of values delimited by a semicolon but the amount separated by a semicolon can vary. So a table may even have 10 values or 1 or more.
The 2nd table doesn't have to have numbers to represent the values. It can even be blank or the default that is given by SQL when no name is provided.
This is not a duplicate of the example provided because this deals with separating a dynamic number of values into columns.

ARRAYFORMULA ON SPECIFIC CELLS

So I have this formula, it basiclly mulitiplies every value in a row for every row and sums up the products, and while thats awesome and all
=sum(ArrayFormula(iferror(A1:A*B1:B*C1:C)))
I would like if there was a way to choose what rows it multiplies and sums up, if I can put a specific letter or like tag those cells in any way and like "filter" them out so it only sums up lets say row 1,2,4 and so on and for infinity, how ever many rows Ill like to add and whichever rows I want to include!
EXAMPLE:
1: 100 4 10
2: 120 2 12
3: 125 5 10
4: 105 3 15
Not sure I fully understand the question but I believe you could solve the problem by introducing a fourth column, using "1" to indicate rows to add to the sum and "0" for rows to ignore. By extending your formula to include the new column D each row is multiplied with 1 (adding the value, since N*1=N) or 0 (ignoring the value, since N*0=0):
=sum(ArrayFormula(iferror(A1:A*B1:B*C1:C*D1:D)))
The below example data would sum row 1, 2 and 4:
1: 100 4 10 1
2: 120 2 12 1
3: 125 5 10 0
4: 105 3 15 1

In MSSQL filter rows based on an ID exists in a column as comma separated string

I've Benchmarking table like this
BMID TestID BMTitle ConnectedTestID
---------------------------------------------------
1 5 My BM1 0
2 6 My BM2 5
3 7 My BM3 5,6
4 8 My BM4 10,12,8
5 9 My BM5 0
6 10 My BM6 3,6
7 5 My BM7 8,3,12,9
8 3 My BM8 7,10
9 8 My BM9 0
10 12 My BM10 9
---------------------------------------------
Explaining the table a little
Here the TestID and the connected TestID is playing the roles. If the user wants all the benchmarks for the TestID 3
It should return rows where testID=3 and also if any rows having connectedTestID column having that testID in it among the comma separated values
That means if the user specify the value 3 as the testID, it should return
---------------------------------------------
8 3 My BM8 7,10
7 5 My BM7 8,3,12,9
6 10 My BM6 3,6
--------------------------------------------
Hope its clear how those 3 rows returned. Means First row is because the testID 3 is there. the other two rows because 3 is in their connectedIDs cell
You should fix the data structure. Storing numeric ids in a comma-delimited list is a bad, bad, bad idea:
SQL Server doesn't have the best string manipulation functions.
Storing numberings as character strings is a bad idea.
Having undeclared foreign key relationships is a bad idea.
The resulting queries cannot make use of indexes.
While you are exploring what a junction table is so you can fix the problem with the data structure, you can use a query such as this:
where testid = 3 or
',' + ConnectedTestID + ',' like '%,3,%'

How to calculate the number of pairs in an Excel spreadsheet?

I have two columns of integers between 1 and 16 in an excel file. I'd like to count the number of pairs of integers in these columns. There are 256 cases and I'd like to have a column which tells me how many pairs exist for each case. For instance, I have a table like below:
1 2
1 1
1 3
1 4
1 1
1 8
1 1
16 16
1 2
...
And I'd like to calculate a column like this:
3 (number of 1 1s)
2 (number of 1 2s)
1 (number of 1 3s)
1 (number of 1 4s)
0 (number of 1 5s)
0 (number of 1 6s)
0 (number of 1 7s)
1 (number of 1 8s)
...
1 (number of 16 16s)
I'd appreciate if someone can help me with the calculation.
First you need to create two columns with all possible combinations:
1 1
1 2
1 3
...
2 1
2 2
...
16 16
Let's assume these are in columns C,D and your data are in columns A, B, in rows 1 to 1000. Then you can use an array formula:
=SUM(IF(($A$1:$A$1000=C1)*($B$1:$B$1000=D1);1;0))
You must press Shift+Ctrl+Enter when entering array formula.