How to get query result in multiple columns in Postgres? - database

I am new to Postgres. I have a table which has the data in the below format:
Table Data
And I want it in the following format. Any help would be appreciated.
Output Format

You can accomplish this with a self-join:
SELECT table_2184."Value" as "2184",
table_2095."Value" as "2095",
table_2184."Date_Time"
FROM table AS table_2184
JOIN table AS table_2095 ON table_2184."Date_Time" = table_2095."Date_Time"
WHERE table_2184."Tag_ID" = 2184
AND table_2095."Tag_ID" = 2095;
Basically, you alias the table so you can join it to itself. If it's not always guaranteed that the tags exist at the same timestamp, you may want to use a FULL OUTER JOIN instead of a JOIN. In that case, also move the WHERE condition to the join.

Related

Looking up values in multiple tables with multiple joins

How would you do a Select statement to lookup a value in a table and then use that value to lookup another value in another table which is then used to lookup a third value in a third table? I cannot do a join from my initial table as there is no common matching field to do the join on.
In my initial table I have an Instr_Id value which is the only possible way to eventually get the Legal_Id value but the table where the Legal_Id is stored does not have an Instr_Id value. In order to do this I need to look up multiple values from 2 other tables to eventually get to the INSTR_ID value.
Example:
I use INSTR_ID in tbl.ABC and join to INSTR_ID in tbl.DEF in order to get the Fin_Enty_Name value in tbl.DEF I then need to use this Fin_Enty_Name value to join to tbl.GHI to get the Fin_Enty_Id . I finally then need to use this Fin_Enty_Id to look up the LEGAL_ID in tbl.JKL so that I can show the LEGAL_ID for each INSTR_ID in my Select query results.
select
a.INSTR_ID,
d.LEGAL_IT
from tbl.ABC as A
Inner join tbl.DEF as B on A.INSTR_ID = B.INSTR_ID
It looks pretty much straight forward:
select a.instr_id, d.legal_id
from tbl.abc a
join tbl.def b
on a.instr_id = b.instr_id
join tbl.ghi c
on b.fin_enty_name = c.fin_enty_name
join tbl.jkl d
on c.fin_enty_id = d.fin_enty_id

How to create a VIEW by join multiple tables having multiple columns using SQL Server

I am creating a view for a table and joining multiple tables for additional columns, getting exact output for some join tables which has only two columns(ID, Name) in it. But i am getting 'Ambiguous column error' for a column which i am using one join table which has more than 10 columns in it.
I know why I am getting this error but i need help to call the EventName from EventInfo join table. Also is it possible to give different column names in the view.
Below are the select statement and the join table that I am getting error.
Thanks in advance for your suggestions.
SELECT
Event_ID, EventName
FROM
[dbo].[tbl_ValueTool_Event_ResultantDataEntryData] Res_event
JOIN
[GRID_ProjectServer_Applications].[dbo].[tbl_ValueTool_EventInformation] EVT ON Res_event.[Event_ID] = EVT.[Event_ID]
Below is the join table from which I want to call the column names corresponding to the Id's
You can use alias for the column names if needed.
SELECT EVT.Event_ID, EVT.EventName
FROM [dbo].[tbl_ValueTool_Event_ResultantDataEntryData] Res_event
JOIN [GRID_ProjectServer_Applications].[dbo].[tbl_ValueTool_Event‌​Information] EVT
ON Res_event.[Event_ID] = EVT.[Event_ID]

Need help on a sql query to get data from multiple tables

I have a couple of tables that have data in them that I am looking to get information from. Here is the rundown....In table 1 I have bunch of columns that I am pulling data from, one of the columns is a user ID (which is a number)that was the last userID to modify a record. In table 2 I want to pull in the name of that user based on the ID that is pulled from the other table (this table has both the userID and the username).
so my final query would have the columns in table 1 as well as the username from table 2 to show that was the user to last edit the record. I assume this has to be done in a nested select statement but for the life of me I cannot come up with the correct syntax.
Can anyone help me out?
Thanks
Jeff
Yes, you need a very basic join that link both tables together.
Select t1.UserID,
t2.UserName
FROM table1 t1 INNER JOIN
table2 t2 ON t1.userid=t2.userid
select t1.*, t2.{username} from table1 as t1
join table2 as t2 on t1.{userId}=t2.{userid};
change {username} with the actual column name of user
similarly {userId} with appropriate column name in tables.
Hope it helps you.
this is standard inner join query, to learn more consider reading: http://www.w3schools.com/sql/

MSSQL - join two views on different data types

I have two views and need to join them on two different data types and two columns.
the first join is on:
the one view has a productlength field of 0.138, data type varchar.
the other view has a length field of 0.138, data type float.
the second join is on:
the one view has a productwidth field of 0.025, data type varchar.
the other view has a width field of 0.025, data type float.
If I joint the two views on just one join it works(can use either fields).
If I use both joins the join fails and the data is not linked.
My query with join is:
SELECT t1.OrderNum,t2.OrderNumber,t1.Description,
t1.ProductThickness,t2.thickness,t1.ProductWidth,t2.width,t1.ProductLength,t2.length,
t1.Productgrade,t2.grade, t1.OrderQty,t1.ProducedQty,t2.DispatchedQty,t2.DispatchDate
FROM table1 as t1 LEFT OUTER JOIN table2 as t2
on t1.ProductWidth=t2.Width
and t1.ProductLength=t2.Length
This join then fails? How can I join these two views on both join conditions and across different data types?
Can I convert the varchar to a float so as to do a like for like conversion?
Thanks as always.
try SQL server convert function
Please confirm if you are using the correct column names and their values are identical while doing the join. Please look at the following link and confirm if you are trying to do the same thing.
http://sqlfiddle.com/#!3/82da2/1

UPDATE query from OUTER JOINed tables or derived tables

Is there any way in MS-Access to update a table where the data is coming from an outer joined dataset or a derived table? I know how to do it in MSSQL, but in Access I always receive an "Operation must use updateable query" error. The table being updated is updateable, the source data is not. After reading up on the error, Microsoft tells me that the error is caused when the query would violate referential integrity. I can assure this dataset will not. This limitation is crippling when trying to update large datasets. I also read that this can supposedly be remedied by enabling cascading updates. If this relationship between my tables is defined in the query only, is this a possibility? So far writing the dataset to a temp table and then inner joining that to the update table is my only solution; that is incredibly clunky. I would like to do something along the lines of this:
UPDATE Table1
LEFT JOIN Table2 ON Table1.Field1=Table2.Field1
WHERE Table2.Field1 IS Null
SET Table1.Field1= Table2.Field2
or
UPDATE Table1 INNER JOIN
(
SELECT Field1, Field2
FROM Table2, Table3
WHERE Field3=’Whatever’
) AS T2 ON Table1.Field1=T2.Field1
SET Table1.Field1= T2.Field2
Update Queries are very problematic in Access as you've been finding out.
The temp table idea is sometimes your only option.
Sometimes using the DISTINCTROW declaration solves the problem (Query Properties -> Unique Records to 'Yes'), and is worth trying.
Another thing to try would be to use Aliases on your tables, this seems to help out the JET engine as well.
UPDATE Table3
INNER JOIN
(Table1 INNER JOIN Table2 ON Table1.uid = Table2.uid)
ON
(Table3.uid = Table2.uid)
AND
(Table3.uid = Table1.uid)
SET
Table2.field=NULL;
What I did is:
1. Created 3 tables
2. Establish relationships between them
3. And used the query builder to update a field in Table2.
There seems to be a problem in the query logic. In your first example, you LEFT JOIN to Table2 on Field1, but then have
Table2.Field1 IS NULL
in the WHERE clause. So, this limits you to records where no JOIN could be made. But then you try and update Table 1 with data from Table2, despite there being no JOIN.
Perhaps you could explain what it is you are trying to do with this query?

Resources