I am doing a Snowflake query where I get RANK from each column.
Output below:
| Rank_A | Rank B |
| -------- | -------- |
| 1 | 3 |
| 2 | 4 |
| 5 | 4 |
Then I do the LEAST function to get the Minimum across those columns
https://docs.snowflake.com/en/sql-reference/functions/least.html
Output below:
| Rank_A | Rank B | LEAST |
| -------- | -------- | -------- |
| 1 | 3 | 1 |
| 2 | 4 | 2 |
| 5 | 4 | 4 |
Lastly, (This is where I am not sure of) How can I make another column that returns what column the Least function came from - so what I am trying to return in this new column is like so:
Expected output below:
| Rank_A | Rank B | LEAST | Column Name of Least Value |
| -------- | -------- | -------- | -------------------------- |
| 1 | 3 | 1 | Rank_A |
| 2 | 4 | 2 | Rank_A |
| 5 | 4 | 4 | Rank_B |
I have no idea how I would go about this? any and all help would be greatly appreciated.
I do not know where to begin. I was contemplating doing this all in python before ingesting the data table?
You could use DECODE to get the first LEAST value column name:
SELECT *
,LEAST(Rank_A, Rank_B)
,DECODE(LEAST(Rank_A, Rank_B),
Rank_A, 'Rank_A',
Rank_B, 'Rank_B') AS column_name
FROM tab;
Output:
We can use a CASE expression along with the LEAST() function:
SELECT
RANK_A,
RANK_B,
LEAST(Rank_A, Rank_B) AS LEAST,
CASE WHEN RANK_A = LEAST(Rank_A, Rank_B)
THEN 'RANK_A' ELSE 'RANK_B' END AS LEAST_VALUE
FROM yourTable;
In the event that RANK_A and RANK_B have the same value, the above query would arbitrarily return RANK_A as the column with the least value.
What is the best way for storing enumerated fields with ability to change its order?
Lets say my database looks like this:
| Table |
|---------------------|
| id | name | order|
| 1 | 1st | 1 |
| 2 | 2nd | 2 |
| 3 | 3rd | 3 |
| 4 | 4th | 4 |
Now, when user change order in such a away
| Table |
|---------------------|
| id | name | order|
| 1 | 1st | 1 |
| 4 | 4nd | 2 |
| 2 | 2nd | 3 |
| 3 | 3rd | 4 |
Here I would have to update all rows in this table.
I consider 2 solutions
Solution 1)
When inserting row X between for example order 2 and order 3, I would change row's X order field to 3.5, So I would choose number in the middle between adjacent orders.
Above table would look like this
| Table |
|---------------------|
| id | name | order|
| 1 | 1st | 1 |
| 4 | 4nd | 2.5 |
| 2 | 2nd | 2 |
| 3 | 3rd | 3 |
Then, after for example 16 changes I would update table and normalize all order fields, so table after normalization would be like this:
| Table |
|---------------------|
| id | name | order|
| 1 | 1st | 1 |
| 4 | 4nd | 2 |
| 2 | 2nd | 3 |
| 3 | 3rd | 4 |
Solution 2)
I also consider adding fields "next" (or "next" and "prev") to each row, but it looks for me like waste of memory.
I really dont want to update whole table every time somebody change order. What is the best way of solving this problem?
I am wondering if it is possible to reshape the following have table in SAS not using SAS/IML to produce the want table.
Have:
+--------+------+------+------+
| NAME | var1 | var2 | var3 |
+--------+------+------+------+
| Q1_ID1 | 1 | 2 | 3 |
| Q1_ID2 | 4 | 5 | 6 |
| Q2_ID1 | 7 | 8 | 9 |
| Q2_ID2 | 10 | 11 | 12 |
| Q3_ID1 | 13 | 14 | 15 |
| Q3_ID2 | 16 | 17 | 18 |
+--------+------+------+------+
Want:
+----------+----+----+----+
| NAME | Q1 | Q2 | Q3 |
+----------+----+----+----+
| var1_ID1 | 1 | 7 | 13 |
| var1_ID2 | 4 | 10 | 16 |
| var2_ID1 | 2 | 8 | 14 |
| var2_ID2 | 5 | 11 | 17 |
| var3_ID1 | 3 | 3 | 15 |
| var3_ID2 | 6 | 6 | 18 |
+----------+----+----+----+
The code to reproduce the have table is the following:
data have;
infile datalines delimiter=",";
input NAME :$8. var1 :8. var2 :8. var3 :8.;
datalines;
Q1_ID1,1,2,3
Q1_ID2,4,5,6
Q2_ID1,7,8,9
Q2_ID2,10,11,12
Q3_ID1,13,14,15
Q3_ID2,16,17,18
;
run;
Two transposes are needed, with some tearing apart and putting together in between.
data have;
infile datalines delimiter=",";
input NAME :$8. var1 :8. var2 :8. var3 :8.;
datalines;
Q1_ID1,1,2,3
Q1_ID2,4,5,6
Q2_ID1,7,8,9
Q2_ID2,10,11,12
Q3_ID1,13,14,15
Q3_ID2,16,17,18
;
run;
proc transpose data=have out=stage;
by name;
var var:;
run;
data stage2(keep=name col1 qtr);
set stage;
qtr = scan(name,1,'_'); * tear apart;
id = scan(name,2,'_');
name = catx('_', _name_, id); * put together;
run;
proc sort data=stage2;
by name qtr;
run;
proc transpose data=stage2 out=want;
by name;
id qtr;
run;
I need to make 2 database constraints that connect two different tables at one time.
1. The total score of the four quarters equals the total score of the game the quarters belong to.
2. The total point of all the players equals to the score of the game of that team.
Here is what my tables look like.
quarter table
+------+--------+--------+--------+
| gNum | Period | hScore | aScore |
+------+--------+--------+--------+
| 1 | 1 | 13 | 18 |
| 1 | 2 | 12 | 19 |
| 1 | 3 | 23 | 31 |
| 1 | 4 | 32 | 18 |
| | | Total | Total |
| | | 80 | 86 |
+------+--------+--------+--------+
Game Table
+-----+--------+--------+--------+
| gID | hScore | lScore | tScore |
+-----+--------+--------+--------+
| 1 | 86 | 80 | 166 |
+-----+--------+--------+--------+
Player Table
+-----+------+--------+--------+
| pID | gNum | Period | Points |
+-----+------+--------+--------+
| 1 | 1 | 1 | 20 |
| | | 2 | 20 |
| | | 3 | 20 |
| | | 4 | 20 |
+-----+------+--------+--------+
So Virtually I need to use CHECK I think to make sure that players points = score of their team ie (hScore, aScore) and also make sure that the hScore and aScore = the total score in the Game table.
I was thinking of creating a foreign key variable on one of the tables and setting up constraints on that would this be the best way of going about it?
Thanks
I was wondering if you can help me with the following problem in spss syntax.
My dataset has nested structure.
Data are nested in companies, then each company has 1 or 2 bosses, but in this case I care only about boss 1. At a previous stage in time the boss graded the workers (not all of them). Now, the ID and the grade of the workers is on the row each worker.
I would like to move the information that was obtained during worker's assessment and create new sets of variables for each (worker ID and grade) on the line/row of the boss.
+---------+------+--------+--------------+---------+---------+--------+---------+
| company | boss |workerID|worker's grade|N:workID1|N:grade1 |N:work2 |N:grade2 |
+---------+------+--------+--------------+---------+---------+--------+---------+
| A | 1 | 1 | | 3 | A | 4 | A |
| A | 2 | 2 | | | |
| A | 0 | 3 | A | | |
| A | 0 | 4 | A | | |
| A | 0 | 5 | | | |
| B | 1 | 1 | | 3 | B | 4 | A |
| B | 0 | 2 | | | |
| B | 0 | 3 | B | | |
| B | 0 | 4 | A | | |
| C | 1 | 1 | | 2 | D | -1 | -1 |
| C | 0 | 2 | D | | |
I would like to move the worker's id and the grade that to the row of the boss in the NEW variables, without loosing the existing variables on workerID and worker's grade.
Basically, I will need to feed forward the information into the new variables and to the row of boss EQ 1 separately for each company.
I have no idea how to proceed with this. I assume that I need a loop that creates new variable for each worker ID that has a valid grade and then feeds forward the information from the worker's row to the boss' newly generated variables.
Any suggestions are very wellcome :-)
Take a look at VARSTOCASES (Data > Restructure)