PostgreSQL concatenate two tables on each row - postgresql

I have two tables, as such (where value in all cases is a character varying):
table_one:
|id|value|
|--+-----|
|1 |A |
|2 |B |
|3 |C |
|4 |D |
table_two:
|id|value|
|--+-----|
|11|0 |
|12|1 |
|13|2 |
|14|3 |
|15|4 |
|16|5 |
I want to do a query that results in the following:
result:
|value|
|-----|
|A_0 |
|A_1 |
|A_2 |
|A_3 |
|A_4 |
|A_5 |
|B_0 |
|B_1 |
|B_2 |
|B_3 |
|B_4 |
|B_5 |
|C_0 |
|C_1 |
|C_2 |
|C_3 |
|C_4 |
|C_5 |
|D_0 |
|D_1 |
|D_2 |
|D_3 |
|D_4 |
|D_5 |
This is for a seldom run report on PostgreSQL 8.4.9, so performance isn't critical. If the answer is 9.x specific, upgrading certainly isn't out of the question, so feel free to give 9.x specific answers. Any ideas?

select
table_one.val || '_' || table_two.val
from
table_one
cross join table_two
Documentation for cross join.
SQLFiddle

Related

Flatten all map columns recursively in PySpark dataframe

I have a pyspark dataframe with multiple map columns. I want to flatten all map columns recursively. personal and financial are map type columns. Similarly, we might have more map columns.
Input dataframe:
-------------------------------------------------------------------------------------------------------
| id | name | Gender | personal | financial |
-------------------------------------------------------------------------------------------------------
| 1 | A | M | {age:20,city:Dallas,State:Texas} | {salary:10000,bonus:2000,tax:1500}|
| 2 | B | F | {city:Houston,State:Texas,Zipcode:77001} | {salary:12000,tax:1800} |
| 3 | C | M | {age:22,city:San Jose,Zipcode:940088} | {salary:2000,bonus:500} |
-------------------------------------------------------------------------------------------------------
Output dataframe:
--------------------------------------------------------------------------------------------------------------
| id | name | Gender | age | city | state | Zipcode | salary | bonus | tax |
--------------------------------------------------------------------------------------------------------------
| 1 | A | M | 20 | Dallas | Texas | null | 10000 | 2000 | 1500 |
| 2 | B | F | null | Houston | Texas | 77001 | 12000 | null | 1800 |
| 3 | C | M | 22 | San Jose | null | 940088 | 2000 | 500 | null |
--------------------------------------------------------------------------------------------------------------
use map_concat to merge the map fields and then explode them. exploding a map column creates 2 new columns - key and value. pivot the key column with value as values to get your desired output.
data_sdf. \
withColumn('personal_financial', func.map_concat('personal', 'financial')). \
selectExpr(*[c for c in data_sdf.columns if c not in ['personal', 'financial']],
'explode(personal_financial)'
). \
groupBy([c for c in data_sdf.columns if c not in ['personal', 'financial']]). \
pivot('key'). \
agg(func.first('value')). \
show(truncate=False)
# +---+----+------+-----+-------+----+-----+--------+------+----+
# |id |name|gender|State|Zipcode|age |bonus|city |salary|tax |
# +---+----+------+-----+-------+----+-----+--------+------+----+
# |1 |A |M |Texas|null |20 |2000 |Dallas |10000 |1500|
# |2 |B |F |Texas|77001 |null|null |Houston |12000 |1800|
# |3 |C |M |null |940088 |22 |500 |San Jose|2000 |null|
# +---+----+------+-----+-------+----+-----+--------+------+----+

Query over 3 tables and different columns

I would like to do a fairly simple query, but I cant figure out, how to join the tables together. I am new to this world of SQL and after reading documentation of JOIN and SELECT clauses, I still can't figure this one out.
Here are my 3 tables:
Seller
|SELLER_ID|NUMBER|FIRST_NAME|LAST_NAME|TEAM_NR|
| 1|105 |John |Smith |1 |
| 2|106 |James |Brown |3 |
| 3|107 |Jane |Doe |3 |
| 4|108 |Nicole |Sanchez |2 |
Service
|SERVICE_ID|CODE|NAME |PRICE |SELLER_ID|CLIENT_ID|
| 1| 502|BLAHBLAH |200 |2 |2 |
| 2| 503|BLAHBLAH2|175 |1 |3 |
| 3| 504|BLAHBLAH3|250 |3 |2 |
| 4| 505|BLAHBLAH4|130 |2 |4 |
Client
|CLIENT_ID|NUMBER |FIRST_NAME | LAST_NAME |
| 1|51 |JOHN | ADAMS |
| 2|52 |MARY | BRYANT |
| 3|53 |FRANCIS | JOHNSON |
| 4|55 |BEN | CASTLE |
The goal of this query would be to figure out which team(TEAM_NR from Seller) sold the most services in a month, on the basis of total amount sold(sum of PRICE from Service)
The result should display FIRST_NAME, LAST_NAME and TEAM_NR of everyone in the "winning" team.
I already looked for help from StackOverFlow and Google and tried editing these according to my tables, but they didn't pan out.
Thank You!
SELECT S.FIRST_NAME, S.LAST_NAME, S.TEAM_NR, sum(R.PRICE) Winning
FROM Seller S
LEFT JOIN Service R ON (S.SELLER_ID=R.SELLER_ID)
GROUP BY S.TEAM_NR, S.FIRST_NAME, S.LAST_NAME
EDIT Don't even need any join on Client table.
EDIT 2 All fields from the SELECT have to be in the GROUP BY.

Postgres select from table and spread evenly

I have a 2 tables. First table contains information of the object, second table contains related objects. Second table objects have 4 types( lets call em A,B,C,D).
I need a query that does something like this
|table1 object id | A |value for A|B | value for B| C | value for C|D | vlaue for D|
| 1 | 12| cat | 13| dog | 2 | house | 43| car |
| 1 | 5 | lion | | | | | | |
The column "table1 object id" in real table is multiple columns of data from table 1(for single object its all the same, just repeated on multiple rows because of table 2).
Where 2nd table is in form
|type|value|table 1 object id| id |
|A |cat | 1 | 12|
|B |dog | 1 | 13|
|C |house| 1 | 2 |
|D |car | 1 | 43 |
|A |lion | 1 | 5 |
I hope this is clear enough of the thing i want.
I have tryed using AND and OR and JOIN. This does not seem like something that can be done with crosstab.
EDIT
Table 2
|type|value|table 1 object id| id |
|A |cat | 1 | 12|
|B |dog | 1 | 13|
|C |house| 1 | 2 |
|D |car | 1 | 43 |
|A |lion | 1 | 5 |
|C |wolf | 2 | 6 |
Table 1
| id | value1 | value 2|value 3|
| 1 | hello | test | hmmm |
| 2 | bye | test2 | hmm2 |
Result
|value1| value2| value3| A| value| B |value| C|value | D | value|
|hello | test | hmmm |12| cat | 13| dog |2 | house | 23| car |
|hello | test | hmmm |5 | lion | | | | | | |
|bye | test2 | hmm2 | | | | |6 | wolf | | |
I hope this explains bit bettter of what I want to achieve.

Postgresql + Select field name as upper and lowercase mixed

I am using this postgresql code,
SELECT id as DT_RowId , title
FROM table_name
ORDER BY title asc
LIMIT 25 OFFSET 0
The results returned like this.
+--------+-----+
|dt_rowid|title|
+--------------+
| 1 |A |
| 2 |B |
| 3 |C |
| 4 |D |
| 5 |E |
| 6 |F |
+--------+-----+
But i want results should return like this.
+--------+-----+
|DT_RowId|title|
+--------------+
| 1 |A |
| 2 |B |
| 3 |C |
| 4 |D |
| 5 |E |
| 6 |F |
+--------+-----+
Note - DT_RowId field i want same like this(upper and lower case mixed).
As explained in the manual unquoted identifier are folded to lowercase (which violates the SQL standard where unquoted identifiers should be folded to uppercase).
You need to use a quoted identifier in order to preserve the case:
SELECT id as "DT_RowId",
title
FROM table_name
ORDER BY title asc
LIMIT 25 OFFSET 0

NDepend query methods/types in framework assembly being used by other assemblies/types

I am trying to determine which types or methods in a base framework assembly are being used by other assemblies in the application system. I cannot seem to find a straight-cut query to do that.
What i have to do is first determine which assemblies are directly using the framework assembly, then manually list them in a second query
SELECT TYPES FROM ASSEMBLIES "IBM.Data.DB2"
WHERE IsDirectlyUsedBy "ASSEMBLY:FirstDirectUsedByAssebmly"
OR IsDirectlyUsedBy "ASSEMBLY:SecondDirectUsedByAssebmly"
OR IsDirectlyUsedBy "ASSEMBLY:ThirdDirectUsedByAssebmly"
OR IsDirectlyUsedBy "ASSEMBLY:FourthDirectUsedByAssebmly"
Is there a better/faster way to query for this?
Additionally, the query results are focused on the matched types only. The Dependency graph or matrix exported only shows details of those. I do not know how to render a graph that shows those types or methods plus show the dependent types/methods from other assemblies that are consuming them?
UPDATE
I cannot use a query like
SELECT METHODS/TYPES WHERE IsPublic AND !CouldBeInternal
because the results return very queer results of using obfuscated types within the IBM.Data.DB2 assembly.
SELECT TYPES
FROM ASSEMBLIES "IBM.Data.DB2"
WHERE IsPublic AND !CouldBeInternal
48 items
--------------------------------------------------+--------------+
types |# IL instructi|
|ons |
--------------------------------------------------+--------------+
IBM.Data.DB2.ae+m |0 |
| |
IBM.Data.DB2.ae+x |0 |
| |
IBM.Data.DB2.ae+f |0 |
| |
IBM.Data.DB2.ae+ac |0 |
| |
IBM.Data.DB2.ae+aa |0 |
| |
IBM.Data.DB2.ae+u |0 |
| |
IBM.Data.DB2.ae+z |0 |
| |
IBM.Data.DB2.ae+e |0 |
| |
IBM.Data.DB2.ae+b |0 |
| |
IBM.Data.DB2.ae+g |0 |
| |
IBM.Data.DB2.ae+ab |0 |
| |
IBM.Data.DB2.ae+h |0 |
| |
IBM.Data.DB2.ae+r |0 |
| |
IBM.Data.DB2.ae+p |0 |
| |
IBM.Data.DB2.ae+ad |0 |
| |
IBM.Data.DB2.ae+i |0 |
| |
IBM.Data.DB2.ae+j |0 |
| |
IBM.Data.DB2.ae+t |0 |
| |
IBM.Data.DB2.ae+af |0 |
| |
IBM.Data.DB2.ae+k |0 |
| |
IBM.Data.DB2.ae+l |0 |
| |
IBM.Data.DB2.ae+y |0 |
| |
IBM.Data.DB2.ae+a |0 |
| |
IBM.Data.DB2.ae+q |0 |
| |
IBM.Data.DB2.ae+n |0 |
| |
IBM.Data.DB2.ae+d |0 |
| |
IBM.Data.DB2.ae+c |0 |
| |
IBM.Data.DB2.ae+ae |0 |
| |
IBM.Data.DB2.ae+o |0 |
| |
IBM.Data.DB2.ae+w |0 |
| |
IBM.Data.DB2.ae+s |0 |
| |
IBM.Data.DB2.ae+v |0 |
| |
IBM.Data.DB2.DB2Command |2 527 |
| |
IBM.Data.DB2.DB2Connection |3 246 |
| |
IBM.Data.DB2.DB2DataAdapter |520 |
| |
IBM.Data.DB2.DB2DataReader |4 220 |
| |
IBM.Data.DB2.DB2_UDF_PLATFORM |0 |
| |
IBM.Data.DB2.DB2Enumerator+DB2EnumInstance |19 |
| |
IBM.Data.DB2.DB2Enumerator+DB2EnumDatabase |15 |
| |
IBM.Data.DB2.DB2Error |98 |
| |
IBM.Data.DB2.DB2ErrorCollection |55 |
| |
IBM.Data.DB2.DB2Exception |185 |
| |
IBM.Data.DB2.DB2Parameter |1 853 |
| |
IBM.Data.DB2.DB2ParameterCollection |1 383 |
| |
IBM.Data.DB2.DB2RowUpdatedEventHandler |0 |
| |
IBM.Data.DB2.DB2RowUpdatedEventArgs |14 |
| |
IBM.Data.DB2.DB2Type |0 |
| |
IBM.Data.DB2.DB2XmlReader |500 |
| |
--------------------------------------------------+--------------+
Sum: |14 635 |
| |
Average: |304.9 |
| |
Minimum: |0 |
| |
Maximum: |4 220 |
| |
Standard deviation: |868.22 |
| |
Variance: |753 808 |
| |
--------------------------------------------------+--------------+
Our code does not use those types and enums directly.
This query returns the methods (respectively the types), that are public and could not be internal. Hence, it returns the methods/types that are indeed used outside of their declaring assembly.
SELECT METHODS/TYPES WHERE IsPublic AND !CouldBeInternal