Normalizing a table by rank - sql-server

I have a Table which looks like this:
Rank Account
| 1 | | A1 |
| 2 | | A2 |
| 3 | | A3 |
| 1 | | A4 |
| 2 | | A5 |
| 1 | | A6 |
In it all the Accounts which are linked together are ranked, done in a previous stored procedure. I want to now break down this table so that whenever a rank goes back down to 1 it takes the previous rows and writes it into a new table. for example Accounts A1,A2,A3 are all written into a separate table as are Accounts A4,A5 and Account A6. How would I do this.
The eventual goal being too right each table into a CSV file. Thanks!

In order to do this, you will need to add a third column to your table. This column would need to either be:
A grouping column that shows which Accounts are linked together, so that it will have the same value for A1, A2, A3. And another common value for A4 & A5, and another value for A6. Or
An ordinal column, such as an IDENTITY column, that you can use to tell in what order the Accounts were inserted, so that you can use that to figure out which items are grouped together.
Without a third column, there is NO WAY to know that A2 goes with A1 and not A4 or A6.

Related

ArrayFormula in google sheets SUM if values match criteria

Ok, so I've been stuck on this for a while now trying to create an array formula that will do the following:
Let's pretend the data I'm working with appears as such:
SHEET 1 SHEET 2
| A | B | C | D | E | F | A | B |
1 | Up | Down | Down | Up | Down | 1 | Red | $50 |
2 | Red | Green| White| Black | Blue | 2 | Green | $100 |
3 | $50 | $200 | $15 | Hidden| | 3 | White | $10 |
4 | | $200 | | Hidden| $100 | 4 | Black | $70 |
5 | $50 | $200 | $15 | Hidden| | 5 | Blue | $100 |
Using that data, I need a formula in column F (that I can copy and paste for subsequent cells in the column).
My actual sheet has about 45 columns across and about 25 rows down with sensitive financial information, so I'm using pretend data above on a smaller scale.
So the formula should look something like this (this would be posted in column F of Sheet 1). It needs to check if $A$1:$E$1="Up" (disregard anything that is not "Up") in addition, IF that array = "up" then check to make sure $A3:$E3>$0 (I intend to exclude blank values and "hidden" values), then IF those 2 criteria are met, then return the corresponding value from $A$2:$E$2 and use that as a search key in a lookup formula (HLookup, Vlookup, offset+match, etc). So for example, the Formula in F3 would match those 2 criteria (which would be A2 [red] and then sum together all of the matching values - in this case, there is only 1 matching value, but usually there will be 3-5).
Attempt 1:
=ArrayFormula(SUM(OFFSET('Sheet 2'!$B$1,MATCH(IF(AND($A$1:$E$1="Up",$A3:$E3>"$0"),$A$2:$E$2,""),'Sheet 2'!$A$1:$A$5,0),0)))
Attempt 2:
=ArrayFormula(SUM(IF(AND($A$1:$E$1="Up",$A3:$E3>"$0"),OFFSET('Sheet 2'!$B$1,MATCH($A$2:$E$2,'Sheet 2'!$A$1:$A$5,0),0),"")))
My first attempt attempts to create an array for the Match search key that uses the IF statements to determine the results to be used in the match condition (In the example above, the only result would be "Red", but in the actual sheet, it could be an array like "Red, Blue, Yellow").
My second attempt attempts to first determine which cells we are using (based on IF conditions), then if the cell matches the IF then we will OFFSET the particular cell.
The errors I have noticed is that match doesn't seem to like my array in the search key. The first attempt results with an error saying it cannot find " in the range. (I guess I don't understand why it was searching for that...?). The 2nd attempt seems to fail because it uses the ENTIRE range ($A$2:$E$2), instead of only the cells that match the criteria.
I have tried such things as: VLookUp, HLookUp, LookUp (but it is not a sorted list). I've tried some other weird tactics, but non seem to give me what I need.
So far, the best I have come up with is:
=ArrayFormula(SUM(IF($A$1:$E$1="Up",IF($A3:$E3>"$0",0,OFFSET('Sheet 2'!$B$1,MATCH($A2:$E2,'Sheet 2'!$A$1:$A$5,0),0)),0)))
In my sheet, it found 3 cells that matched the criteria, but instead of adding Cell 1 value, Cell 2 value, and Cell 3 value, it just added Cell 1 value 3 times. So it seems it was only using $A2 (the first option in the range for the match search key) and repeated it for every cell that matched.
I have also tried to include the Filter function (instead of IF) to eliminate cells that do not have "up" as their value ($A$1:$E$1).
Use SUMPRODUCT:
=SUMPRODUCT(($A$1:$E$1="Up")*(VLOOKUP($A$2:$E$2,'Sheet 2'!$A$1:$B$5,2,FALSE))*($A3:$E3>0)*(ISNUMBER($A3:$E3)))

How to define range in importxml formula in Google Sheets

In my google sheet, I have URL in A1 and other data in B1, C1, D1, E1, etc... I want to use importxml formula but want to this work automatic, I mean in column B2 I try this
=ARRAYFORMULA( IMPORTXML($A1&$B1:$CZ1, "//suggestion/#data"))
so its do the rest of the job automatic I mean fetch data for A1and B1 in B2 and do A1 and C1 in C2 and A1 and D1 in D2, is it possible? I mean now I have to do manually enter formula in every column. hope you understand what I mean.
Column A | Column B | Column C
=================================
site.com | Name 1 | Name 2
| |
| |
| |
Column A | Column B | Column C
=================================
site.com | Name 1 | Name 2
| =ARRAYFORMULA( IMPORTXML($A1&$B1:$CZ1, "//name/#data"))
| |
| |
IMPORTXML is not supported in ARRAYFORMULA as you expect it. your options are:
paste in B2 and drag the formula to the right
=IMPORTXML($A1&B1, "//suggestion/#data")
paste in B2 the hardcoded formula for all columns:
={IMPORTXML(A1&B1, "//suggestion/#data"),
IMPORTXML(A1&C1, "//suggestion/#data"),
IMPORTXML(A1&D1, "//suggestion/#data")}
or scripted solution:
function onOpen() {
var sheet = SpreadsheetApp.getActiveSheet();
var length = sheet.getRange("B2:2").getValues().filter(String).length;
sheet.getRange("C3:3").clearContent();
sheet.getRange("C3:3" + length).setFormula(sheet.getRange("B3").getFormula());
sheet.getRange("C4:1000").clearContent();
}
where cell B2 is:
=ARRAYFORMULA(A1&B1:D1)
and cell B3 is:
=ARRAYFORMULA(IFERROR(IMPORTXML(B2:2, "//suggestion/#data")))

Google spreadsheets query range get item to the right

I am trying to get data from a "flexible" sheet.
The idea is to add in a row a string, and next to it add a formula that from another sheet, it looks up the string and returns the cell just at the right.
Example:
Data Sheet: DataCollection
| | A | B | C | D |
| 1 | Pepper | 2 | Sugar | 5 |
| 2 | Carbon | 3 | Toy | 34 |
so if in my other sheet, the "Summary" I add to A1 Sugar I would like to see in A2 a 5.
What I have tried so far
VLOOKUP function
=VLOOKUP(A1,'DataCollection'!A2:B&'DataCollection'!C2:D,1,false)
didn't work! I keep receiving a parse formula error.
QUERY function
=QUERY(DataCollection, "SELECT B WHERE A = A1")
that does not work either, I need many different Named Ranges and add a different query for each of them.
Here is a test spreadsheet in case it explains better that my wall of text:
https://docs.google.com/spreadsheets/d/15L5nPGfZ8OXS5Rhl3PdIVhtF7D3QzerkARskflDiJL4/edit?usp=sharing
you almost had it. try:
=VLOOKUP(A1, {'DataCollection'!A2:B; 'DataCollection'!C2:D}, 2, 0)
for an array use:
=ARRAYFORMULA(IFERROR(VLOOKUP(A1:A, {'DataCollection'!A2:B; 'DataCollection'!C2:D}, 2, 0)))

Find max of multiple columns (row-wise) and columns' names where max. values occurred (T-SQL)

I am fairly new to T-SQL and advanced concepts/queries and I have difficulty trying to figure out the situation described further. Lets say, I have a table like this:
--------------------
| K | C1 | C2 | C3 |
--------------------
| 1 | 14 | 24 | 48 |
| 2 | 10 | 65 | 37 |
| 3 | 44 | 90 | 22 |
--------------------
I need to find maximums row-wise, which has already been asked many times on SO, AND what makes difference, in addition, I also want the columns' names where found maximums occurred. The result has to be like this:
-- how to get Col column?
-----------------
| K | Max | Col |
| 1 | 48 | C3 |
| 2 | 65 | C2 |
| 3 | 90 | C2 |
-----------------
I learned how to find maximum rows-wise based on this and this threads, but I cannot understand how I could add another column containing columns' names where max. values occurred. I know I kinda have to show efforts but actually I cannot show anything since I just don't quite understand how to do it and all my attempts just return errors. Any ideas, hints? Thanks in advance!
The simplest way in SQL Server is to use apply:
select t.k, v.col, v.val
from t cross apply
(select top 1 v.col, v.val
from (values ('C1', t.C1), ('C2', t.C2), ('C3', t.C3)
) v(col, val)
order by val desc
) v(col, val)
I would approach this problem by joining a temporary maximum table back to the original and then writing a long "case when", assuming you have a reasonable number of columns. It would look like the following.
Select a.K
, b.Max_Value
, Case When a.Column_1 = b.Max_Value Then C1
When a.Column_2 = b.Max_Value Then C2
When a.Column_3 = b.Max_Value Then C3
... As Col
From Your_Table as a Left Join Max_Table as b on a.K = b.K
I believe this should get you what you want if I understood the question correctly. If you have a lot of columns where this would get too long then there's probably a better approach.

Excel: Sum up values of a table based on a condition or grouping from another table

I could not find exactly this problem, though it seems to be similar to this one: Conditional SUM using multiple tables in EXCEL except the fact that the rows will not always have the same sequence in the tables.
My results table (RES) shall be populated based on the Groups (A, B, C) of the Group Table and the values of Value Table.
I am grouping several Products into Groups A, B, C according to the
Group Table:
Products | Group
------------------------
P1 | A
P2 | A
P3 | B
P4 | B
P5 | A
P6 | C
For several periods I have the values that these products generate in different tables. These tables have the same column structure, for example:
Value Table Period 1 # Value Table Period 2
Products | Value # Products | Value
------------------------ # ------------------------
P3 | 40 # P2 | 60
P5 | 10 # P5 | 20
P2 | 60 # P1 | 10
P6 | 50 # P3 | 30
P1 | 20 # P6 | 40
P4 | 30 # P4 | 50
These values I am trying to consolidate into a Results table, which would sum up the different products according to their groups within the respective periods, as follows:
Result | Period 1 | Period 2
-----------------------------------
A | 90 | 90
B | 70 | 80
C | 50 | 40
I tried to apply the array concept of the other post, but I was not successful in adapting it to my tables, which differ in some structural aspects. I´d be happy if I could avoid the use of VBA.
Thanks a lot in advance!
after some additional efforts I found the right array formula, which should be put into the B2 cell of the Result table:
=SUM(SUMIF(Period1!$A$2:$A$7;IF(Result!$I$17:$I$22=Result!$A2;Result!$H$17:$H$22);Period1!$B$2:$B$7))
confirm with CTRL+SHIFT+ENTER and copy down and across.
I hope that this helps you, too ;-)
Here's another way...
=SUM(IF(ISNUMBER(MATCH(Period1!$A$2:$A$7,IF(Result!$I$17:$I$22=Result!$A2,Result!$H$17:$H$22),0)),Period1!$B$2:$B$7))
Using the semi-colon as a separator...
=SUM(IF(ISNUMBER(MATCH(Period1!$A$2:$A$7;IF(Result!$I$17:$I$22=Result!$A2;Result!$H$17:$H$22);0));Period1!$B$2:$B$7))
Note that the formula needs to be confirmed with CONTROL+SHIFT+ENTER.

Resources