How to automatically sort google spreadsheets by a row - sorting

I'm trying to make my google spreadsheet automatically sort the order of columns in my spreadsheet by the value in a row
For example, the following spreadsheet:
1
header
header2
header3
2
filler
filler2
filler3
3
4
1
7
4
testA
testB
testC
5
testD
testE
testF
--
---------
---------
---------
would become the following spreadsheet:
1
header
header2
header3
2
filler3
filler
filler2
3
7
4
1
4
testC
testA
testB
5
testF
testD
testE
--
---------
---------
---------
How would I go about accomplishing this?

try:
=TRANSPOSE(SORT(TRANSPOSE(A1:C10), 2, 1))

Related

Oracle - how to insert if not exists?

Example dB : https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=49af209811bce88aa67b42387f1bb5f6
I'd like to add insert this line
1002 9 1 UNKNOWN
Because of the line exists
1002 5 1 JIM
I was thinking about something like
select codeclient from STATS_CLIENT_TEST where CODEAXESTAT=5
and insert codeclient, 9,1,UNKNOWN.
but not sure how to do it? And simple query or a PL/SQL?
What's the best way to get it?
Thanks
Use an INSERT .. SELECT statement with a PARTITIONed outer join:
INSERT INTO stats_client_test (
codeclient, codeaxestat, codeelementstat, valeuraxestatistiqueclient
)
SELECT cc.codeclient,
s.codeaxestat,
s.codeelementstat,
'UNKNOWN'
FROM (SELECT DISTINCT codeclient FROM stats_client_test) cc
LEFT OUTER JOIN stats_client_test s
PARTITION BY (s.codeaxestat, s.codeelementstat)
ON (s.codeclient = cc.codeclient)
WHERE s.rowid IS NULL;
or a MERGE statement:
MERGE INTO stats_client_test dst
USING (
SELECT cc.codeclient,
s.codeaxestat,
s.codeelementstat,
s.ROWID AS rid
FROM (SELECT DISTINCT codeclient FROM stats_client_test) cc
LEFT OUTER JOIN stats_client_test s
PARTITION BY (s.codeaxestat, s.codeelementstat)
ON (s.codeclient = cc.codeclient)
) src
ON (dst.ROWID = src.rid)
WHEN NOT MATCHED THEN
INSERT (codeclient, codeaxestat, codeelementstat, valeuraxestatistiqueclient)
VALUES (src.codeclient, src.codeaxestat, src.codeelementstat, 'UNKNOWN');
db<>fiddle here
Here's one option: using the MINUS set operator, find missing codeclient values and then insert appropriate row(s).
Before:
SQL> select * From stats_client_Test order by codeaxestat, codeclient;
CODECLIENT CODEAXESTAT CODEELEMENTSTAT VALEURAXESTATISTIQUECLIENT
-------------------- ----------- --------------- ----------------------------------------
1000 5 1 JOHN
1001 5 1 ALICE
1002 5 1 JIM
1003 5 1 BOB
1000 9 1 MAN
1001 9 1 WOMAN
1002 9 1 unknown
1003 9 1 MAN
8 rows selected.
Query:
SQL> insert into stats_client_test
2 (codeclient, codeaxestat, codeelementstat, VALEURAXESTATISTIQUECLIENT)
3 select x.codeclient, 9, 1, 'unknown'
4 from (select codeclient from stats_client_Test
5 where codeaxestat = 5
6 minus
7 select codeclient from stats_client_Test
8 where codeaxestat = 9
9 ) x;
0 rows created.
After:
SQL> select * From stats_client_Test order by codeaxestat, codeclient;
CODECLIENT CODEAXESTAT CODEELEMENTSTAT VALEURAXESTATISTIQUECLIENT
-------------------- ----------- --------------- ----------------------------------------
1000 5 1 JOHN
1001 5 1 ALICE
1002 5 1 JIM
1003 5 1 BOB
1000 9 1 MAN
1001 9 1 WOMAN
1002 9 1 unknown --> here it is
1003 9 1 MAN
8 rows selected.
SQL>

How to flip values of two rows?

ID TYPE ORDER_INDEX CITY_ID
-----------------------------------
1 CAT 1 1
2 DOG 2 1
3 CAT 4 2
4 DOG 5 2
5 BEE 9 1
For each city I need to swap the order_index of cat and dog.
So city with ID 1 should have cat=2 and dog=1, city with ID 2 would have cat=5 and dog=4.
How can this be done with pure SQL?
Use an update:
UPDATE yourTable t1
SET ORDER_INDEX = (SELECT t2.ORDER_INDEX FROM yourTable t2
WHERE t2.CITY_ID = t1.CITY_ID AND
t2.TYPE IN ('CAT', 'DOG') AND
t2.TYPE <> T1.TYPE)
WHERE TYPE IN ('CAT', 'DOG');
The above update logic assumes that you wish to do the ORDER_INDEX swap between dog/cat pair records having the same CITY_ID.
Well, if you don't care about ID, then there's a simple option which does what you wanted:
Before:
SQL> select * from test order by id;
ID TYP ORDER_INDEX CITY_ID
---------- --- ----------- ----------
1 CAT 1 1
2 DOG 2 1
3 CAT 4 2
4 DOG 5 2
5 BEE 9 1
Update:
SQL> update test set
2 type = decode(type, 'DOG', 'CAT', 'DOG')
3 where type in ('CAT', 'DOG');
4 rows updated.
After; requirement was
city with ID 1 should have cat=2 and dog=1, city with ID 2 would have cat=5 and dog=4.
SQL> select * from test order by id;
ID TYP ORDER_INDEX CITY_ID
---------- --- ----------- ----------
1 DOG 1 1 --> city with ID = 1 has DOG = 1 ...
2 CAT 2 1 --> ... and CAT = 2
3 DOG 4 2 --> city with ID = 2 has DOG = 4 ...
4 CAT 5 2 --> ... and CAT = 5
5 BEE 9 1
SQL>

How do I add alias based on values of table

As my title, for ex I have a table A and it has values from 1 to 10.
I want to Select value 1 and 2 as "First" column name, 3 and 4 as "Second" column name v.v.
Look like this:
|First| |Second|
1 3
2 4
1 4
Thanks!
Using CASE, perhaps?
SQL> with test as
2 (select level val from dual
3 connect by level <= 5
4 )
5 select case when val <= 2 then val end first,
6 case when val > 2 then val end second
7 from test;
FIRST SECOND
---------- ----------
1
2
3
4
5
SQL>
It would help, though, if you provided sample data and explained what to do with values that aren't contained in (1, 2, 3, 4).

SQL Getting Non-Repeating Values [Oracle SQL*Plus]

I have a table(order_t) that contains CustomerIDs and SalespersonIDs and I want to display SalespersonIDs with all the CustomerIDs associated with them.
I tried using SELECT DISTINCT
SELECT DISTINCT salespersonid, customerid
FROM order_t;
But that will repeat salespersonids.
Here is a table sample
salespersonid customerid
--------------- ---------------
15 1
15 2
15 3
16 4
16 5
17 6
And I want to get this dataset as the result
salespersonid customerid
--------------- ---------------
15 1
2
3
16 4
5
17 6
18 4
5
9
This worked perfectly
BREAK ON sid
SELECT DISTINCT salespersonid sid, customerid cid
FROM ORDER_T
ORDER BY salespersonid;
In SQL*Plus, you would do the following:
SQL> BREAK ON sid
SQL> SELECT salespersonid sid, customerid cid
FROM order_t
ORDER BY salespersonid;
Should give you output that looks something like this (untested):
SID CID
----- ------
1 1
2
3
2 1
7
3 1
...
EDIT:
If you wanted a single row for each salespersonid, see this Stackoverflow question on how to do that. It's not trivial to do.
SELECT DISTINCT will return the non-repeated combination of salespersonid and customerid.
In other words it can return same salespersonid but with different customerid.
e.g.
1,1
1,2
1,3 ...
but it wont return 1,1 | 1,2 | 1,3 again

Oracle - Tree - PL/SQL - Copying data with mapping

I have following table structure
ID Name Parent_ID
1 abc 0
2 efg 1
3 hij 1
4 klm 2
5 nop 3
and so on....id is generated in a sequence
I want a PL/SQL to coy this same data in same table but id should be generated by seq and Parent_ID should be mapped accordingly...that means..after PL/SQL it should look like
ID Name Parent_ID
1 abc 0
2 efg 1
3 hij 1
4 klm 2
5 nop 3
6 abc 0
7 efg 6
8 hij 6
9 klm 7
10 nop 8
can any1 help me in this...thnx
So, here is your original data:
SQL> select * from t23
2 /
ID NAM PARENT_ID
---------- --- ----------
1 abc 0
2 efg 1
3 hij 1
4 klm 2
5 nop 3
SQL>
This procedure populates a PL/SQL collection with the extant rows. It loops through those rows, populating an associative array with a new ID which is indexed by the original ID. (Note the assignment uses the 11g syntax for getting a sequence value, rather than the traditional selecting from DUAL). The ID is then chnaged to the new value, and the PARENT_ID is updated with the value stored in the associative array. Lastly the new rows are inserted into the table using the bulk FORALL syntax,
SQL> declare
2 type num_lookup is table of pls_integer
3 index by pls_integer;
4 id_translate num_lookup;
5
6 type t23_nt is table of t23%rowtype;
7 new_rows t23_nt;
8 begin
9 select *
10 bulk collect into new_rows
11 from t23
12 order by id asc;
13
14 for i in new_rows.first()..new_rows.last()
15 loop
16 id_translate(new_rows(i).id) := s23.nextval;
17 new_rows(i).id := s23.currval;
18 if new_rows(i).parent_id != 0
19 then
20 new_rows(i).parent_id := id_translate(new_rows(i).parent_id);
21 end if;
22 end loop;
23
24 forall j in new_rows.first()..new_rows.last()
25 insert into t23 values new_rows(j);
26
27 end;
28 /
PL/SQL procedure successfully completed.
SQL>
And, lo!
SQL> select * from t23;
ID NAM PARENT_ID
---------- --- ----------
1 abc 0
2 efg 1
3 hij 1
4 klm 2
5 nop 3
6 abc 0
7 efg 6
8 hij 6
9 klm 7
10 nop 8
10 rows selected.
SQL>

Resources