Add new column to the existing table based on specific row value conditions - sql

For the below table sample, I want to add and additional column lets say " Amount difference" and it should have value only if the successive rows of D column has "Success" and "Waiting" as value and A B and c column value matches exactly. IN such scenario the new coulmn F (Amount difference) should have (15-0) as the value and rest of the column values should be null. If the successive rows doenst have Waiting then the value of row 2 in New column should be 15 and rest all rows should be null.
A Id
B session Id
C customer Type
D ticket status
E Amount
ORDINAL
First
A
vip
Failed
10
0
First
B
ordinary
Success
15
1
First
B
ordinary
Waiting
0
2
How can this be achieved with a query please?

Related

WHERE condition is true but not all rows are returned? (SQLite db)

When I try to use WHERE word = "a" there is only 1 row returned when in fact there are 2 rows with the word "a". As you can see in the second picture there is also another "a" with the id = 1. How can I query both of them?
Only 1 row is returned with WHERE word = "a"
There are actually 2 rows with for the word "a"
UPDATE: They actually have different length,
one 1 and the other 2.

VBA code to sum total of like account numbers

I have a code that matches 2 columns A VS C for like account numbers. If numbers match than it finds if the amounts in Column B & D match. If it does it pastes the lines of data to another spreadsheet.
I am coding a statement to find the like account numbers in column A VS C, but if the amounts don't match in column B & D than it needs to find the like account numbers in column A and sum there amounts to see if it matches the amount in column B.
How would I write the code to collect the sum of the same account numbers in column A?
If Acct = MatchAcct Then
If Amount <> MatchAmount Then
If Acct = nonmatchacct Then
(sum the values in column B for the matched account #s in column A)
The code above says if the Acct (Column A account #'s) = MatchAcct (Column C account #'s) then if Amount (Column B value) doesn't equal MatchAmount (Column D value) then the code sees if Acct (Column A account #'s) equals nonmatchacct (any other account # in Column A). If the account # matches then I want it to sum those amounts in column B.

INDEX/MATCH only finds the first value in a column with multiple entries

Column A has a list of values which need to be matched with Column B. If matched, the adjacent values in Column C should be returned. I'm using INDEX/MATCH to perform this action. However, if Column A has a value which is repeated, the INDEX/MATCH function returns the first value from Column B rather than the 2nd or 3rd value being looked-up in Column B. Is it possible to find the other values (except the 1st one) in Column B using the INDEX/MATCH function?
Here's the sample table:
Column A Column B Column C
lookup0x16e663000 lookup0x16e663000 1461178793193312
lookup0x16e663001 lookup0x16e663000 1461178793193508
lookup0x16e663000 lookup0x16e663001 1461178793193704
lookup0x16e663001 lookup0x16e663001 1461178793193891
lookup0x16e663000 lookup0x16e663005 1461178793194110
lookup0x16e663005 lookup0x16e663005 1461178793194312
lookup0x16e663005 lookup0x16e663000 1461178793194538
lookup0x16e663000 lookup0x16e663000 1461178793194760
So here, if I look for the 4th value from Column A, it matches with the 3rd value of Column B rather than the 4th.
You would use the AGGREGATE() Function in place of the MATCH:
=INDEX($C$1:$C$8,AGGREGATE(15,6,ROW($A$1:$A$8)/($A$1:$A$8=B1),COUNTIF($B$1:$B1,B1)))
The AGGREGATE(15,6,...) simulates the SMALL(IF()) Function. The ROW($A$1:$A$8)/($A$1:$A$8=B1) creates an array of row numbers where B1 = $A$1:$A$8.
The COUNTIF($B$1:$B1,B1) will tell the AGGREGATE() Function which of that array of row numbers to return. As it is dragged down the count will change from 1 to 2 to 3 and so on.
As per your comment this formula Is dynamic to limit the reference data to the extents of Column A.
The INDEX(C:C,MATCH("ZZZ",A:A)) returns the last row in column C where there is data in Column A
Put this in D2 and copy down.
=INDEX($C$1:INDEX(C:C,MATCH("ZZZ",A:A)),AGGREGATE(15,6,ROW($A$1:INDEX(A:A,MATCH("ZZZ",A:A)))/($A$1:INDEX(A:A,MATCH("ZZZ",A:A))=B2),COUNTIF($B$1:$B2,B2)))
Edit 2
I apparently had my column reversed.
Put this in D2 and copy down.
=INDEX($C$1:INDEX(C:C,MATCH("ZZZ",A:A)),AGGREGATE(15,6,ROW($B$1:INDEX(B:B,MATCH("ZZZ",A:A)))/($B$1:INDEX(B:B,MATCH("ZZZ",A:A))=A2),COUNTIF($A$1:$A2,A2)))

Need to add sequentional data rows

I have a table with 2 columns. In the first column I have a counter to 30,000. In the second column I would like to add to 2nd column the rows '1-30000' to test for a conversion.
I am trying to make it look like this
COL A Col B
1 1-1
2 1-2
3 1-3
I am a newbie at this and no matter how I try I can get the first row to do my bidding but the Col B starts its row at the end of Col A
I don't know what is your RDBMS, but you can try:
update table
set colB = colB+'-'+colA

Check if "MAN_ABC_COMMENT_CHANGE" doesn't occurs in Column D

In Column A, it has ID and it is random.
Sometimes ID repeat 2 times, sometimes 10 times, it is different all the time.
I need to check if "MAN_ABC_COMMENT_CHANGE" doesn't occur.
If for example in image below for ID = 12345 in column D "MAN_ABC_COMMENT_CHANGE" never occurred, so in new sheet or somehow show that for id 12345 MAN_ABC_COMMENT_CHANGE never occurred.
I have over 5000 rows of data and over 1000 ID
One method...
1) set up a Pivot table of ID from the data
2) add a formula in B2 =countifs(Sheet2!A:A,A2,Sheet2!D:D,"MAN_ABC_COMMENT_CHANGE")
3) copy down (double click the bottom right of cell to autofill)
4) the rows with zero are the ones you want (can filter them as required)