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}
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 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))
Is there any way to compare values for one partition with another partition of the same table? Requirement is like I have a table and suppose there are 5 partitions, table having two columns(not null). Suppose Col1 having all the distinct values and in col2 there can be a duplicate values. So while comparing one partition with other or we can say rest of the 4 partitions on the basis of distinct col2 values according to the partition name, if the value match between two partition then a new table will create with union of the two partition.
And if there is no match between the col2 values of one partition and with rest of the partition then new table will create of same structure(without any union).
Note:
I want to automate this process through PLSQL code.
Currently what I am doing manually:
I have one table having five partition, for example Table structure:
create table PART_TEST1
(col1 int not null,
col2 int not null)
partition by range (col2)
(partition part1 values less than (10),
partition part2 values less than (20),
partition part3 values less than (30),
partition part4 values less than (40),
partition part5 values less than (maxvalue));
Data distribution:
col1 having distinct values like- 1, 2, 3....so on.
col2 having values like- 1, 2, -1, 1, 2, 3, 4, 1...so on
col2 has duplicate values and my goal is to find the distinct values according to the name of the partition like:
select distinct col2 from PART_TEST1 partition (part1);
For example output of above query is:
Col2
1
2
Again I am querying for finding matching values in other partition:
select distinct col2 from PART_TEST1 partition (part2);
For example output of above query is:
Col2
2
3
So now part 1 and part2 has one same value '2' and two non common values 1 and 3.
so my final query is:
create table 'TABLE_NAME' as select * from part_test1 where col2 = 1;
create table 'TABLE_NAME' as select * from part_test1 where col2 = 3;
create table 'TABLE_NAME' as
(select * from part_test1 where col2 = 2
union
select * from part_test1 where col2 = 2);
Hopefully now you will get some clarity about my problem. I am new to PLSQL and not able to compare the partition values. Also if I am able to compare the values then how can I store the output of the comparison query and then finally create the table? And also I am thinking that I need to compare one partition with rest of the partition like some kind of loop operation.
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 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)