I will explain my problem with an sample example
create table foo(id int,idx int,idy int,fld int,fldx varchar);
insert into foo values (1,2,3,55,'AA'),(2,3,4,77,'AB'),(3,4,8,55,'AX'),(9,10,15,77,'AR'),
(3,4,8,11,'AX'),(3,4,8,65,'AX'),(3,4,8,77,'AX');
id,idx,idy, fld,fldx
1 2 3 55 AA
2 3 4 77 AB
3 4 8 55 AX
9 10 15 77 AR
3 4 8 11 AX
3 4 8 65 AX
3 4 8 77 AX
I need to select only column fld and its total count of each column(fld) in descending order
Expected Result :
fld count
---------
77 3
55 2
11 1
65 1
select fld
,count(fld) rw_count
from foo
group by fld
order by rw_count desc
Group By
select fld,count(*) from foo group by 1 order by 2 desc ;
Related
My data has the following Structure
ID
Month
Year
Revenue
1
1
20
860
1
2
20
22
1
5
20
339
2
3
20
12098
3
3
20
12
3
4
20
10
3
6
20
9
3
7
20
122
3
8
20
11
There are 1000s of IDs and I want to select a random sample of 100 IDs. So if I randomly select ID 3, I need all rows of data for ID 3. I have to use SQL for this. I welcome any suggestions.
You can use following query.
For MS-Sql
Select top 100 * from table_name where ID=$randomId ORDER BY NEWID(); //like ID=3
For My-Sql
Select * from table_name where ID=$randomId ORDER BY RAND() LIMIT 100; //like ID=3
i have this table
DEVICE_NAME AMOUNT_LEVEL1 DEVICE_NAME AMOUNT_LEVEL2
2477885 3
2479936 4 2479922 4
2478974 62 2478712 62
2478358 8 2478348 8
2477703 3
2477911 6 2477713 6
2477835 1
and i need the data order like
DEVICE_NAME AMOUNT_LEVEL1 AMOUNT_LEVEL2
2477885 3
2479936 4
2478974 62
2478358 8
2477703 3
2477911 6
2477835 1
2479922 4
2478712 62
2478348 8
2477713 6
i have tried to use union but did not succeed to get the required result
what query will do the trick ?
Thank you
This should work
SELECT
device_name_1,
amount_level_1,
NULL as amount_level_2
FROM
some_table
WHERE device_name_1 IS NOT NULL
UNION ALL
SELECT
device_name_2,
NULL as amount_level_1,
amount_level_2
FROM
some_table
WHERE device_name_2 IS NOT NULL
Using Teradata :I have two tables with 10 records and 3 variables. All columns and values are same expect for three values in one variable.
My task is to make code changes for table2 where both records are matched, by not hard coding any value.
The second table was created by the first table , so there is no way to pick values by join etc .
Code :
Create multiset table table2 as (
Select * from table1 )
With data primary index(var1);
Eg:
Var1
Var2
Var3
1
Abc
20
2
Cde
30
3
kgk
87
4
kjj
98
5
gvy
67
6
jbn
78
7
hvb
56
8
ihg
62
9
jhn
22
10
hbn
34
Var1
Var2
Var3
1
Abc
20
2
Cde
30
3
kgk
87
4
kjj
98
5
gvy
67
6
jbn
78
7
hvb
56
8
ihg
77
9
jhn
56
10
hbn
23
Not sure what you want but you can find all the matching records using exists as follows:
select t.* from table2 t
where exists
(select 1 from table1 tt
where t.var1 = tt.var1 and t.var2 = tt.var2)
Say I have two tables, geom_levels and taz_geoms where taz_geoms has the columns as follows:
taz_geoms
id(int) state(int) county(int) taz(int) geom(geometry(MultiPolygon,4326))
and geom_levels looks like this:
geom_levels
TAZ COUNTY STATE DISTRICT
1 1 29 1
2 1 29 1
3 1 29 1
4 2 29 2
5 2 29 2
6 2 29 2
7 2 29 3
8 3 29 3
9 3 29 3
10 3 29 4
11 3 29 4
12 3 29 4
13 4 29 5
14 4 29 5
15 4 29 5
16 4 29 6
17 4 29 6
How would I go about combining these taz geometries into county, state, and district geometries? I would like to have a county_geoms, state_geoms, and district_geoms table. I have see that you can use ST_UNION with a geom array, but how would I generate such an array for counties or districts?
I was thinking something like this for counties:
SELECT ST_UNION(SELECT geom from taz_geoms GROUP BY county);
and for districts:
SELECT ST_UNION(SELECT geom from taz_geoms t LEFT JOIN geom_levels gl ON gl.taz = t.taz GROUP BY district);
But those options do not see possible.
Ideas?
try with:
SELECT ST_UNION( ARRAY( 'YOUR SELECT geoms QUERY' ) );
in your case:
SELECT ST_UNION(ARRAY( (SELECT geom from taz_geoms t LEFT JOIN geom_levels gl ON gl.taz = t.taz GROUP BY district) ));
I had the same problem and got it to work with postgre usin the ARRAY() function ;)
I've got a bit of a complicated query that I'm struggling with. You will notice that the schema isn't the easiest thing to work with but it's what I've been given and there isn't time to re-design (common story!).
I have rows like the ones below. Note: The 3 digit value numbers are just random numbers I made up.
id field_id value
1 5 999
1 6 888
1 7 777
1 8 foo <--- foo so we want the 3 values above
1 9 don't care
2 5 123
2 6 456
2 7 789
2 8 bar <--- bar so we DON'T want the 3 values above
2 9 don't care
3 5 623
3 6 971
3 7 481
3 8 foo <--- foo so we want the 3 values above
3 9 don't care
...
...
n 5 987
n 6 654
n 7 321
n 8 foo <--- foo so we want the 3 values above
n 9 don't care
I want this result:
id result
1 999*888*777
3 623*971*481
...
n 987*654*321
Is this clear? So we have a table with n*5 rows. For each of the sets of 5 rows: 3 of them have values we might want to multiply together, 1 of them tells us if we want to multiply and 1 of them we don't care about so we don't want the row in the query result.
Can we do this in Oracle? Preferably one query.. I guess you need to use a multiplication operator (somehow), and a grouping.
Any help would be great. Thank you.
something like this:
select m.id, exp(sum(ln(m.value)))
from mytab m
where m.field_id in (5, 6, 7)
and m.id in (select m2.id
from mytab m2
where m2.field_id = 8
and m2.value = 'foo')
group by m.id;
eg:
SQL> select * from mytab;
ID FIELD_ID VAL
---------- ---------- ---
1 5 999
1 6 888
1 7 777
1 8 foo
1 9 x
2 5 123
2 6 456
2 7 789
2 8 bar
2 9 x
3 5 623
3 6 971
3 7 481
3 8 foo
3 9 x
15 rows selected.
SQL> select m.id, exp(sum(ln(m.value))) result
2 from mytab m
3 where m.field_id in (5, 6, 7)
4 and m.id in (select m2.id
5 from mytab m2
6 where m2.field_id = 8
7 and m2.value = 'foo')
8 group by m.id;
ID RESULT
---------- ----------
1 689286024
3 290972773
Same logic; just removed the hard-coded values. posting this answer thinking might be helpful to some others.
SELECT a.id,
exp(sum(ln(a.val)))
FROM mytab a,
(SELECT DISTINCT id,
field_id
FROM mytab
WHERE val = 'foo') b
WHERE a.id = b.id
AND a.field_id < b.field_id
GROUP BY a.id;