I have two tables with two formats date
table 1 :
id time ref
5 1397635972 A
10 1397635975 B
50 1397635976 C
table 2 :
id time ref
10 2013/10/05 D
51 2014/01/02 E
how join two table on table1.id=table2.id and table1.time=table2.time
This is my attempt :
$sql=' select table1.id, table1.time, table1.ref, table2.id, table2.time, table2.ref
from table1 INNER JOIN table2
ON (table1.id = table2.id AND DATE_FORMAT(table1.time,'%y/%m/%d') = table2.time)';
Try this:
select table1.id, table1.time, table1.ref, table2.id, table2.time, table2.ref
from table1 INNER JOIN table2
ON (table1.id = table2.id AND FROM_UNIXTIME(table1.time) = table2.time)
Are you sure that the ID needs to be a part of the join as well? This kind of join with this kind of structure would make almost no sense from a database design standpoint...
No data are not shown
For example FROM_UNIXTIME table2.time :
SELECT FROM_UNIXTIME(1196440219) : '2007-11-30 10:30:19'
I need to format then become : 2013/10/05
Related
I have 3 tables in that 2 tables are master table and 3rd is transaction table. i need to get count from transaction table for each value in other two table without loosing rows in mater table
i need result like below
Table layout for understanding
This is the code i have tried,
select s.status_name, e.machine_group_name, qty = COALESCE(COUNT(e.id),0)
from tbl_status s
left outer JOIN tbl_transaction as e ON e.status_name = s.status_name
group by e.machine_group_name, s.status_name
This is solution i have figured:
select m.machine_group_name, s.status_name, qty = COUNT(e.id) from
tbl_machine_group as m
cross join tbl_status as s
left outer join tbl_transaction as e on e.status_name = s.status_name
and e.machine_group_name = m.machine_group_name
group by m.machine_group_name, s.status_name
order by machine_group_name
select
MC_Group_Name
,Status_Name
,count(1) as [Count of Transaction]
from
tbl_Transaction tbl_3
left join tbl_Machine_Group tbl_1
on tbl_3.MC_Group_Name = tbl_1.MC_Group_Name
left join tbl_Status tbl_2
on tbl_3.Status_Name = tbl_2.Status_Name
group by
MC_Group_Name
,Status_Name
I have joined select columns from 3 tables into a new table using the following:
SELECT A.ExternalID, A.UserDefinedXml.value('(Skin_Sheet/#Label)[1]', 'varchar(3)') AS SS, A.ServiceSiteUid, A.LastModifiedDate, A.PersonUid,
B.FirstName, B.LastName, B.PersonUid,
C.Name
FROM Patient A
INNER JOIN Person B ON B.PersonUid = A.PersonUid
INNER JOIN ListServiceSite C ON C.ServiceSiteUid = A.ServiceSiteUid
WHERE SS IS NOT NULL
ORDER By LastModifiedDate;
This all works but I'm not sure how to reference the column SS created from data extracted from the XML so I can only select the observations in which the value is "Yes" or "No". In R I would have created a new object but I'm not sure how SQL stores this new table if I don't specify what the table name is.
Side note, I did try to insert this into a new table but SQL wasn't letting me because, for some reason, the join resulted in PersonUid being duplicated.
Thank you in advance, I'm very, very new to SQL and trying to learn on the fly.
Conceptually WHERE comes before SELECT, so you need to push the query into a derived table subquery or Common Table Expression (CTE) to reference SS in a WHERE clause. EG
with q as
(
SELECT A.ExternalID, A.UserDefinedXml.value('(Skin_Sheet/#Label)[1]', 'varchar(3)') AS SS, A.ServiceSiteUid, A.LastModifiedDate, A.PersonUid,
B.FirstName, B.LastName, B.PersonUid,
C.Name
FROM Patient A
INNER JOIN Person B ON B.PersonUid = A.PersonUid
INNER JOIN ListServiceSite C ON C.ServiceSiteUid = A.ServiceSiteUid
)
SELECT *
FROM q
WHERE SS IS NOT NULL
ORDER By LastModifiedDate;
This will put your results into a temp table, and avoids the problem of having two columns with the same name:
SELECT
A.ExternalID
,SS = A.UserDefinedXml.value('(Skin_Sheet/#Label)[1]', 'varchar(3)')
,A.ServiceSiteUid
,A.LastModifiedDate
,PersonUid_A = A.PersonUid
,B.FirstName
,B.LastName
,PersonUid_B = B.PersonUid
,C.Name
INTO #TempResults
FROM Patient A
INNER JOIN Person B ON B.PersonUid = A.PersonUid
INNER JOIN ListServiceSite C ON C.ServiceSiteUid = A.ServiceSiteUid
WHERE SS IS NOT NULL
ORDER BY LastModifiedDate;
Using SQL Server Management Studio 2016 (v 13.0).
I have two tables with two distinct keys I can use to join, with the catch being that there are mixed NULLs in both PK columns:
Table 1 Table 2
Acct1 App1 Acct2 App2 Product
----------- ----------------------
1 A NULL A Bed
2 B 2 B Sofa
3 C 3 NULL Bed
4 D 4 D Bed
Desired result in the joined table, only including those where Product = Bed:
Acct App Product
1 A Bed
3 C Bed
4 D Bed
Thank you!
While I agree #d219's answer should be the correct solution, a different approach could use an or in the join such like:
select Acct1,App1,Product
from table1 inner join table2
on App1=App2 or Acct1=Acct2
where Product='Bed'
See this post for discussion on using the or join.
One way would be to do two separate SELECT statements using each key and then UNION them, something like this:
SELECT t1.Acct1, t1.App1, t2.Product
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.Acct1 = t2.Acct2
WHERE t2.Product = 'Bed'
UNION
SELECT t1.Acct1, t1.App1, t2.Product
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.App1 = t2.App2
WHERE t2.Product = 'Bed'
I have a table in 2 different database. Table1 in DataBase1 and Table2 in database2.
Table1 and Table2 have the same columns with different rows content.
each row correspond to a parcel number (TShipping_Tracking or TShipping_Reference or TShipping_OrderRef or TShipping_Barcode) and an ID (TShipping_ID). Remarque: for each parcel (row) only 1 of the 4 column , related to parcel number, listed above is not null
These are the schema of the table in each database:
create table Database1..Table1 (TShipping_ID varchar(50),TShipping_Tracking varchar(50),TShipping_Reference varchar(50),TShipping_OrderRef varchar(50),TShipping_Barcode varchar(50))
create table Database2..Table2 (TShipping_ID varchar(50),TShipping_Tracking varchar(50),TShipping_Reference varchar(50),TShipping_OrderRef varchar(50),TShipping_Barcode varchar(50))
Moreover, I have the table Database1..Reject having the same columns as Table1 (and Table2) exept TShipping_ID:
create table Database1..Table3(TShipping_Tracking varchar(50),TShipping_Reference varchar(50),TShipping_OrderRef varchar(50),TShipping_Barcode varchar(50))
I want to retreive TShipping_ID of the parcel that does not exist in Database1
I did the following query but it has a very bad response time:
select isnull(isnull(isnull(D2t1 .TShipping_ID,D2t2.TShipping_ID),D2t3.tshipping_id),D2t4.tshipping_id) as TShipping_ID
from Database1..Table3 D1t3
left join Database1..Table1 D1t1 on D1t3.TShipping_tracking=D1t1.TShipping_tracking
left join Database1..Table1 D1t2 on D1t3.TShipping_Reference=D1t2.TShipping_Reference
left join Database1..Table1 D1t3 on D1t3.TShipping_OrderRef=D1t3.TShipping_OrderRef
left join Database1..Table1 D1t4 on D1t3.TShipping_barcode=D1t4.TShipping_barcode
left join Database2..Table2 D2t1 on D1t3.TShipping_tracking=D2t1.TShipping_tracking
left join Database2..Table2 D2t2 on D1t3.TShipping_Reference=D2t2.TShipping_Reference
left join Database2..Table2 D2t3 on D1t3.TShipping_OrderRef=D2t3.TShipping_OrderRef
left join Database2..Table2 D2t4 on D1t3.TShipping_barcode=D2t4.TShipping_barcode
where D1t1.TShipping_Tracking is null and D1t2.TShipping_Reference is null and D1t3.TShipping_OrderRef is null and D1t4.TShipping_BarCode is null
Does anyone has a better way to do it?
Thanks
Assuming that Table1.TShipping_ID is a NOT NULL field:
SELECT t2.TShipping_ID
FROM Table3 t3
LEFT JOIN Table2 t2
ON t2.TShipping_Tracking = t3.TShipping_Tracking
OR t2.TShipping_Reference = t3.TShipping_Reference
OR t2.TShipping_OrderRef = t3.TShipping_OrderRef
OR t2.TShipping_Barcode = t3.TShipping_Barcode
LEFT JOIN Table2 t1
ON t1.TShipping_Tracking = t3.TShipping_Tracking
OR t1.TShipping_Reference = t3.TShipping_Reference
OR t1.TShipping_OrderRef = t3.TShipping_OrderRef
OR t1.TShipping_Barcode = t3.TShipping_Barcode
WHERE t1.TShipping_ID IS NULL
This will still be slow as hell, not the least of which because it's pretty close to a CROSS JOIN, but you don't supply enough information about what you're doing or what the key fields of the tables are to make me think you can replace the OR with AND in the join conditions. I suspect that may be the case, however.
We had the following query in sql server:
SELECT b.columnB,
b.displayed_name AS displayName,
c.type_cd,
c.type_desc,
b.month_desc AS month
FROM table1 a, table2 b, table3 c
WHERE b.region_code *= a.columnA
AND c.program_type_cd *= a.program_type_cd
which, in oracle, got converted to:
SELECT b.columnB,
b.displayed_name displayName,
c.type_cd,
c.type_desc,
b.month_desc month
FROM table1 a,
table2 b,
table3 c
WHERE b.columnB = a.columnA(+)
AND c.type_cd = a.type_cd(+)
But while running this in oracle we get an error
"a table must be outer joined to at most one other table"
whats the best way to fix this and keep the same logic as sql server?
Try this once:
SELECT b.columnB,
b.displayed_name displayName,
c.type_cd,
c.type_desc,
b.month_desc month
FROM table1 a
LEFT JOIN table2 b ON b.columnB = a.columnA
LEFT JOIN tablec c ON c.type_cd = a.type_cd
Why is table1 listed as an OUTER JOIN if you're not returning any data from it? It seems like you'd want table1 to be an inner join to table2, and then do an OUTER between 2 and 3. Like this:
SELECT b.columnB,
b.displayed_name displayName,
c.type_cd,
c.type_desc,
b.month_desc month
FROM table1 a,
table2 b,
table3 c
WHERE b.columnB = a.columnA
AND c.type_cd = a.type_cd(+)
On another note, I'd recommond switching to ANSI joins (as in Eric's example) - they're much easier to read, though functionally, they're the same thing and are executed the exact same way.