I am trying to sort a query pivot table in order that the columns should be in descending order.
I tried:=QUERY({Data!A1:C24},"Select Col1, Sum(Col2) group by Col1 pivot Col3 order by Col1 Desc, Col2 Desc") but it tells me that "COL_IN_ORDER_MUST_BE_IN_SELECT: Col2".
Is there a way to sort the columns of the pivot table in addition to the rows?
Here is a sample sheet: https://docs.google.com/spreadsheets/d/1W8T2BJvSRN_CMdTs1asONEWrDjT3c5yDTFRxh8mSh10/edit?usp=sharing
try:
=TRANSPOSE(QUERY(TRANSPOSE(QUERY({Data!A1:C24},
"select Col1,sum(Col2)
group by Col1
pivot Col3
order by Col1 desc")),
"order by Col1 desc", 1))
Related
How can I achieve this to Select to one row only dynamically since
the objective is to get the uniqueness even on multiple columns
select distinct
coalesce(least(ColA, ColB),cola,colb) A1, greatest(ColA, ColB) B1
from T
The best solution is to use UNION
select colA from your_table
union
select colB from your_table;
Update:
If you want to find the duplicate then use the EXISTS as follows:
SELECT COLA, COLB FROM YOUR_TABLE T1
WHERE EXISTS (SELECT 1 FROM YOUR_tABLE T2
WHERE T2.COLA = T1.COLB OR T2.COLB = T1.COLA)
If I correctly understand words: objective is to get the uniqueness even on multiple columns, number of columns may vary, table can contain 2, 3 or more columns.
In this case you have several options, for example you can unpivot values, sort, pivot and take unique values. The exact code depends on Oracle version.
Second option is listagg(), but it has limited length and you should use separators not appearing in values.
Another option is to compare data as collections. Here I used dbms_debug_vc2coll which is simple table of varchars. Multiset except does main job:
with t as (select rownum rn, col1, col2, col3,
sys.dbms_debug_vc2coll(col1, col2, col3) as coll
from test )
select col1, col2, col3 from t a
where not exists (
select 1 from t b where b.rn < a.rn and a.coll multiset except b.coll is empty )
dbfiddle with 3-column table, nulls and different test cases
I have six rows like
col1 col2
--------------
Apple 120
XApple 140
Banana 130
Xbanana 150
Car 110
XCar 160
I would like to sort these rows on col2 but leave the rows with 'X' alone.
so after sorting the rows should be like
col1 col2
--------------
Car 110
Apple 120
Banana 130
XCar 160
XApple 140
Xbanana 150
meaning, the rows with car apple and banana should be sorted but the rows with xcar, xapple and xbanana should be left alone and just be appended at the end.
I tried
select *
from table
where col1 not like 'X%' order by col2
union
select *
from table
where symbol like 'X%'
but sql server doesn't allow that. Could anybody point me to the right direction or tell me that this is not possible?
PS: any LINQ solution will also be fine.
thanks
Order by whether the first character of col1 is 'X' or not, and then by col2.
Example:
SELECT *
FROM table
ORDER BY CASE WHEN col1 LIKE 'X%' THEN 1 ELSE 0 END,col2
Although, this doesn't leave the LIKE 'X%' rows unordered, neither did your example.
There is no order without an explicit ORDER BY clause. While you can use UNION to group the rows, you cannot guarantee that the order of the unordered rows is stable. See here.
The following will split the list into two groups of rows, but each will be sorted by Col1:
select Col1, Col2
from (
select Col1, Col2, 1 as PleaseSort
from MyTable
where Col1 not like 'X%'
union
select Col1, Col2, 0
from MyTable
where Col1 like 'X%' ) as PlaceHolder
order by PleaseSort desc, Col1
If you know the sort of upper limit for how many x rows you'll get then you could do something like....
select * from (select top 5000 col1 from #tmp order by col1) a
union
select col1 from #tmp
I have a table which doesnot have any unique key column and I want to perform bulk update using self join.
Update
(
select t1.Col1 col1, t2.col1 col2
from table t1
inner join table t2 on <join condtn>
where <condtn>
)
Set col1 = col2
but as the table does not have unique key column, it gives error:
ORA-01779: cannot modify a column which maps to a non key-preserved
table.
Is there any solution other than adding unique constraint :)
You should be able to refactor the query to do a correlated update
UPDATE table t1
SET col1 = (SELECT col1
FROM table t2
WHERE t1.<<some column>> = t2.<<some column>>)
WHERE EXISTS( SELECT 1
FROM table t2
WHERE t1.<<some column>> = t2.<<some column>>)
I am having a simple table with 5 columns
id
id_user
col1
col2
col3
how can make Linq query to select an id_user and sum up all the col1, col2, col3 integers to make an average of those 3 columns ?
Thanks
Assuming you already have your table something along these lines should do it:
from row in table
let avgTotal = (new [] {row.col1, row.col2, row.col3}).Average()
select new {row.user_id, avgTotal}
I have two tables that have identical columns. One table houses imported data, the other table houses data specific to my application:
IMPORT_TABLE MY_TABLE
COL1 COL2 COL1 COL2
"A" "1" "A" "2"
"B" "1" "B" "1"
What I need to do is write a single query that will tell me, for a given value in COL1, I have differing values in COL2 across the tables. So, when I run the query I woud get back the value "A" for COL1. This tells me that I need to insert "A" "1" into MY_TABLE.
How can I accomplish the query? I know how to do a Group By on a single table but not across tables.
If you just want to get the rows in IMPORT_TABLE that don't exist in MY_TABLE
SELECT col1, col2
FROM import_table
MINUS
SELECT col1, col2
FROM my_table
If col1 is unique, you could also do
SELECT import.col1, import.col2 imported_col2, mytbl.col2 my_col2
FROM import_table import
FULL OUTER JOIN my_table mytbl ON (mytbl.col1 = import.col1)