I've a table in PostgreSQL 9.4 database in which one of the column contains data both integer and alphabets in following format.
1
10
10A
10A1
1A
1A1
1A1A
1B
1C
1C1
2
65
89
Format is, it starts with a number then an alphabet then number then alphabet and it goes on. I want to sort the field like below,
1
1A
1A1
1A1A
1B
1C
1C1
2
10
10A
10A1
65
89
But when sorting 10 comes before 2. Please suggest a possible query to obtain desired result.
Thanks in advance
Try this
SELECT *
FROM table_name
ORDER BY (substring(column_name, '^[0-9]+'))::int -- cast to integer
,coalesce(substring(column_name, '[^0-9_].*$'),'')
Related
I have a table as shown below:
cell_id
area
a-1-2-0
34
a-1-2-1
42
a-1-2-2
45
a-1-2-3
42
b-1-5-0
47
b-1-5-1
40
I want to convert it to this one to make groups while splitting it for test and train sets:
cell_id
area
a-1-2
34
a-1-2
42
a-1-2
45
a-1-2
42
b-1-5
47
b-1-5
40
for i in range(df.shape[0]):
k=df['cell_id'][i].split("-")
l="{}-{}-{}-{}".format(k[0],k[1],k[2],k[3])
df['cell_id'][i]=l
I used the code above but it takes so long and i wonder if there is any faster way doing this. Thanks in advance
I have a query which results are
fruit street inventory need to_buy
banana 123 15 99 22
apple 4 32 68 44
banana 789 01 32 11
apple 9832 0 99 94
apple 85 839 12 48
banana 832 77 05 55
I want to get the minimum values for inventory, and need, and get the max to_buy value. but only have one record of each 'fruit'. the 'street' column is irrelevant and is not needed in the final result. The final result should look like
fruit inventory(min) need(min) to_buy(max)
banana 01 05 55
apple 0 12 94
Also the initial records may not be ordered at first so there are more 'fruits' inserted at random How can i achieve the desired result above?
Try this:
SELECT MIN(inventory), MIN(need), MAX(to_buy)
FROM tableName
GROUP BY fruits
This one should work:
SELECT fruits, MIN(inventory), MIN(need), MAX(to_buy)
FROM <table_name>
GROUP BY fruits
id varname 1area 2area 3area 4area
------------------------------------
1 abc 345 3.7 34 87
1 pqr 46 67 78 55
1 lmn 67 99 33 44
2 xyz 78 78 33 32
I need to calculate SUM of column query.
Is it possible to get column count using while loop?
You probably want to do a SUM() of the Narea column group by your id column like
select id, sum(1area),
sum(2area), sum(3area), sum(4area)
from tbl1
group by id;
I have a database table like this:
C1 C2 C3
---------------------
81 1 10
81 2 20
81 3 30
82 1 40
82 2 50
82 3 60
Note that it has no primary key.
I want to run a query which prints C1 and the various occurrences of C3 values with it. It basically gives me the output in a serialised format. I mean something like this :
81 10 20 30
82 40 50 60
The one approach I can think of is using a rownum but am not sure if that;s the way to go about it. Is there a better way for doing this ?
The query will depend on DBMS you use.
In MySQL, you can use group_concat function:
select c1, group_concat(c3 separator ' ')
from t
group by c1;
Sorry guys! I just dont know what to do with this task.
The data is the same as in this question here, but what to do in case when I insert new data in big_table.bench_id and i want this data be visible also in BATCH_ID table?
I have tried to bound them with keys, but big_table already has main key so dont know what to do. Please any advice will be appreciated.
Big_table.bench_id:
**bench_id**
31
51
51
61
61
61
71
71
I have created another BATCH_ID table with two columns:
**distinct_bench** **number**
-----------------------------
31 1
51 2
61 3
71 2
So for example, if i will add new code to the big_table.bench_id like '111':
**bench_id**
31
51
51
61
61
61
71
71
111
so it will also appears in another table:
**distinct_bench** **number**
-----------------------------
31 1
51 2
61 3
71 2
111 1
Do you really need another table? You can create a view to achieve that.
create table xxTemp (bench_id int) ;
insert into xxTemp (bench_id)
values (31)
,(51)
,(51)
,(61)
,(61)
,(61)
,(71)
,(71) ;
create view xxTempCount as
Select bench_id
, COUNT(1) number
From xxTemp
Group By bench_id ;
select *
from xxTempCount ;
insert into xxTemp (bench_id)
values (111) ;
select *
from xxTempCount ;
Elmer
Instead of creating a table for that purpose, you can create a view that will return you the desired information. For example, try following:
CREATE VIEW vwBigTable
AS
SELECT bench_id AS [**distinct_bench**], COUNT(*) AS [**number**]
FROM big_table
GROUP BY bench_id
And then:
SELECT * FROM vwBigTable