Update database from same table on like matches + reference column - sql

I have a database where I would like to update rows in column B based upon data from like matches in column A and exact matches in column C.
Column A is a SKU. Column C is a pricing category (A - retail, B - small warehouse, c- big warehouse). Row 1, 7 and 10 are all the same SKU in the same pricing category (A) and I need the pricing data from Column B to match. Row 1 is the correct pricing data which I want to copy to rows 7 and 10.
The reason Rows 1, 7 and 10 are the same SKU is our ERP utilizes options within the X1234F and X1234P configurations. The main pricing data will always come from SKUs without the X/F/P configuration options.
Sample:
Column A
Column B
Column C
1234
2000
A
1234
1900
B
1234
1800
C
2355
1000
A
2355
900
B
2355
800
C
X1234F
1900
A
X1234F
1800
B
X1234F
1700
C
X1234P
1900
A
X1234P
1800
B
X1234P
1700
C
X2355F
900
A
X2355F
800
B
X2355F
700
C
X2355P
900
A
X2355P
800
B
X2355P
700
C
The data from Column B rows 1-3 should update rows 7-12 and rows 4-6 should update 13-18

UPDATE my_table AS u
SET "Column B" = ref."Column B"
FROM my_table AS ref
WHERE ref."Column A" ~ ^\d*$' -- the ref."Column A" has only digits
AND u."Column A" ~ '^\D+' || ref."Column A" || \D+$' -- the u."Column A" is like the ref."Column A" with one or more non-digit character before and after
AND u."Column C" = ref."Column C"

Related

Performing a Crosstab Query against a Dynamic data list

I have a query that pulls a field IncCode from Table A and arranges it by column headers. 1, 2, 3, 4, 5. That query also pulls an address list as shown below:
Query 1:
Address
1
2
3
4
5
100 Street
A
B
C
D
E
200 Street
A
B
C
D
E
300 Street
A
B
C
D
E
This information is then compared against another query in a crosstab to pull in a different Address based on matching values:
(Crosstab) Query 2:
Address
1
2
3
4
5
400 Street
A
B
C
D
E
500 Street
A
B
C
D
E
600 Street
A
B
C
D
E
The problem is IncCode only outputs a column if there is something to output. If there are no entries for 5, for example, then there is no column 5. When put into a crosstab, which requires me to Select the columns manually, it fails outputting an error. Is there a way to pre-empt this without manually removing the columns that are not present in the crosstab SQL? I realize a Select * would handle pulling in the output regardless of which columns are present, but I need the updated addresses from the crosstab comparison as well. An example of an output like this would incorporate an empty column to represent the field not present as shown in Example 1: Any help would be appreciated.
Example 1 (Column 5 not pulled from Query 1):
Address
1
2
3
4
5
400 Street
A
B
C
D
500 Street
A
B
C
D
600 Street
A
B
C
D

Dynamic transpose of rows to column without pivot (Number of rows are not fixed all the time)

i have a table like
a 1
a 2
b 1
b 3
b 2
b 4
i wanted out put like this
1 2 3 4
a a
b b b b
Number of rows in output may vary.
Pivoting is not working as it is in exasol, and case cant work as it is dynamic

SQL JOIN for Duplicate Values

I have following two tables:
A.
A_ID Amount GL_ID
------------------
1 100 10
2 200 11
3 150 10
4 20 10
5 369 12
6 369 11
7 254 12
B.
B_ID Name GL_ID
-----------------
1 A 10
2 B 10
3 C 11
4 D 11
5 E 12
6 F 12
I want to join these tables. They have GL_ID column in common (ID of another table). Table A store transactions along with GL_ID while table B defines document type (A, B, C, D etc.) with reference to GL_ID.
A & B don't have any common column except GL_ID. I want the following result, relevant document type (A, B, C, D etc.) for each transaction in table A.
A.A_ID A.Amount B.Name
-----------------------
1 100 A
2 200 B
3 150 B
4 20 B
5 369 A
6 369 D
7 254 D
But when I apply to join (LEFT, RIGHT, FULL JOIN) keyword, query shows repeated values. But I only want to have relevant Doc Type for each line in table A.
try this.
select distinct A.A_ID, A.Amount, B.Name
from A inner join B on A.GL_ID=B.GL_ID

Group By and get top N in Simple SQL

I have following table in SQLite
BANK:
user-id sender-name receiver-name amount
----------------------------------------
1 A B 200
2 A C 250
3 A B 400
4 A B 520
4 A D 120
4 A D 130
4 A B 110
4 A B 300
4 A B 190
4 A C 230
4 A B 110
4 A C 40
4 A C 80
I need to find out top 3 transaction from each receiver. There are multiple solutions provided for several other database which is not compatible with SQLite cause of the use of certain functions like PARTITION and RANK and even user-defined variables.
I need the solution in simple SQL queries to allow use with SQLite.
Expected result:
receiver-name amount
--------------------
B 560
C 1220
D 250
I managed to do it with using only simple function with self-join.
Now you can just update N with your preferred value, for my case top 3, it would be LIMIT 3.
SELECT receiver-name ,(
SELECT SUM(amount) as sum_amount
FROM (
SELECT amount
FROM bank as b2
WHERE b2.receiver-name = b.receiver-name
ORDER BY b2.amount DESC
LIMIT 3
)
) as sum_amount
FROM bank as b
GROUP BY receiver-name

Custom order in TSQL: page of N records with max X of a value

I'm trying to order a table such that every 'page' with N number of records (i.e page size) includes a maximum of X records of each field value.
Example table values:
Value
-----
a
a
c
a
b
b
a
c
b
a
c
c
c
d (and so on..)
(Value can be any text, in random order. For simplicity, I've used alphabets.)
If page size is 5 and max of each field value is 2, the results can be as listed below (or in a different random order), as long as every 5 consecutive records have a max of 2 records of each character:
Value
-----
a ┐
a |
c |── first page of size 5 with max 2 records of a value
b |
b ┘
a ┐
a |
c |── second page of size 5 with max 2 records of a value
b |
c ┘
a ┐
c |── last page of size 5 (or less) with max 2 records of a value
c |
d ┘
Any help is appreciated.