how can i union 2 columns to one row - sql

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

Related

Match data values for two tables in Teradata Sql

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)

Simply stack column to existing table

Simple question. Say I have a table with two columns, PersonID and CardID and 10 rows. I have another Table that contains 1 column, Code and 10 rows. I simply want to stack the codes to the other table. Note that is does not matter which person gets which code, they should all just get one.
At the moment I find myself adding rownumbers in both tables and updating were TableA.ID = TableB.ID. But this seems overly complex for such a simple taks.
Any suggestions?
TableA
PersonID CardID
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
Table B
Code
Random1
Random2
Random3
Random4
Random5
Random6
Random7
Random8
Random9
Random10
Desired Result:
PersonID CardID Code
1 1 Random1
2 2 Random2
3 3 Random3
4 4 Random4
5 5 Random5
6 6 Random6
7 7 Random7
8 8 Random8
9 9 Random9
10 10 Random10
Again, code order does not matter. Each person, one code. That's it.
You need not to append 1 extra column to each row, You can use CTE with RowNum like :
;WITH CTE_Results
AS (
SELECT ROW_NUMBER() OVER (ORDER BY Code) as ID, Code
from TableB B
)
select A.PersonId, A.CardID, CTE_Results.Code from CTE_Results
INNER JOIN TableA A ON A.PersonId= CTE_Results.ID
Working SQL Fiddle is : http://sqlfiddle.com/#!18/f8f86/9
Result will be like :
PersonId CardID Code
1 1 Random1
2 2 Random10
3 3 Random2
4 4 Random3
5 5 Random4
6 6 Random5
7 7 Random6
8 8 Random7
9 9 Random8
10 10 Random9

PostgreSQL: Select particular column and its total row count

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 ;

Postgresql: Merge multiple geometries into single geometry using Join

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 ;)

Query to multiply certain sets of rows on a single table

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;