I have a mssql database name Organization
I have 3 tables:
Table 1 named Organization
Table 2 named Product
Table 3 named IPAddress
Table 1 returns about 1000 results which is fine.
SELECT distinct OrganizationName, AssetNumber
FROM OrganizationList
Where OrganizationName = 'orgname'
All I would like to see is the asset numbers with each org. Im not sure how to do this. I know a need a join and have tried a bunch of examples from the net however am not able to get it working.
Also I would like to be able to get the IPaddress for each asset
The problem i an running into is i need to match the OrganizationID col from Organization with the OrganizationID col from Product and i need to match the ProductId from Product and the ProductId from IPAddress
Any help would be greatly appreciated. Please let me know if you will need any more info.
Thank You
Should also list out the IP address for you as well.
SELECT distinct c1.OrganizationName, c2.AssetNumber, c3.IPAddress
FROM Organization c1
INNER JOIN Product c2
ON c1.OrganizationID = c2.OrganizationID
INNER JOIN IPAddress c3
ON c2.ProductId = c3.ProductId
Where c1.OrganizationName = 'orgname'
I am doing some guessing here since you didn't give us all of the information needed to fully help you. How about the following query:
SELECT DISTINCT o.OrganizationName, p.AssetNumber, ip.IPaddress
FROM Organization AS o
INNER JOIN Product AS p
ON o.OrganizationID = p.OrganizationID
INNER JOIN IPAddress AS ip
ON p.ProductId = ip.ProductId
Where o.OrganizationName = 'orgname'
I didn't see any need to join the OrganizationList table. If you do indeed need it then add the following join:
INNER JOIN OrganizationList AS ol
ON o.OrganizationID = ol.OrganizationID
for user3022598
Take result of this query in a temp table:
SELECT distinct o.OrganizationName, p.AssetNumber, i.IPAddress
FROM Organization o
INNER JOIN Product p
ON o.OrganizationID = p.OrganizationID
INNER JOIN IPAddress i
ON p.ProductId = i.ProductId
Where o.OrganizationName = 'orgname'
then you can apply disctint on ipaddress field.
Try this one:
select o.OrgName, o.AssetNo
from OrgList o
join Prdct p
on o.orgid = p.orgid
Union All
select p.productid/name, i.ipadress
from product p
join ipadres i
on p.prdid=i.prdid
Plz check it & let me know if works or not.
Related
Can anyone help I'm new to SQL and trying to figure out the below question see image for the table structure;
Question = Select account name, contact last name, case number, quote number, quote date and quote value for the f third-largest quote ever created for each of the accounts in the EC1 area
So far I got;
Select
a.accountname, cc.lastname, c.casenumber,
q.quotenumber, q.quotedate, q.quotevalue
from
TBL_Quote q
Left join
TBL_case c On q.caseid = c.caseid
Left join
tbl_contact cc On c.contactID = cc. contactID
Left join
tbl_account a On a.accountid = cc.accountid
Where
left(a.postcode, 3) like 'EC1'
and for the third:
SELECT TOP 1 value
FROM
(SELECT DISTINCT TOP 3 value
FROM tbl_quote
ORDER BY value DESC) a
ORDER BY value
I can't seem to combine the top 3 and the query is it best to overpartion by ?
I would suggest joins and a row-limiting clause:
select ac.accountName, co.lastName, ca.caseNumber, qu.quoteNumber
from tbl_account ac
inner join tbl_contact co on co.accountId = ac.accountId
inner join tbl_case ca on ca.contactId = co.contactId
inner join tbl_quote qu on qu.caseId = ca.quoteId
where ac.postcode like 'EC1%'
order by len(qu.value) desc
offset 2 rows fetch next 1 row only
I am new to SQL.Is this a correct join for multiple tables.
select * from [dbo].[Quotes] Q ,[dbo].[Invoices] I ,[dbo].[Receipts] R ,
[dbo].[QuoteLines] QL where QL.QuoteID = Q.ID AND I.QuoteID = Q.ID
AND R.QuoteID = Q.ID AND QL.Amount = 336.47 and QL.TravelType = 'International' and QL.Type = 'Accommodation'
Your question
Is this a correct join for multiple tables
Should be answered with No... It might work as expected, but this way of joining is outdated for centuries.
Your approach names all tables comma separated. This would lead to a huge cartesian product, a combination of each row with each row. The following WHERE clause makes sure, that you will get the needed/related rows and columns only...
Not knowing your structure this is a blind flight, but I think you are looking for something like this:
select *
from [dbo].[Quotes] Q
inner join [dbo].[QuoteLines] QL on Q.ID=QL.QuoteID
inner join [dbo].[Invoices] I on I.QuoteID=Q.ID
inner join [dbo].[Receipts] R on R.QuoteID=Q.ID
where QL.Amount = 336.47
and QL.TravelType = 'International'
and QL.[Type] = 'Accommodation';
You might want to change inner to left in cases, where not each row has a corresponding row on the other side.
Try this:-
Select table1.ID ,table1.Name
from Table1
inner join Table2 on Table1 .ID =Table2 .ID
inner join Table3 on table2.ID=Table3 .ID
where table1.Name = Table3.Name
I have this query that currently returns all the pertinent information I need, but I need it to only return unique DISPLAYCLAIMNUMBER rows. I have tried throwing a DISTINCT before it, but that gives me an error for invalid syntax. I'm pretty confused on how to put DISTINCT on something that is in the middle of my SELECT. The order of my query is also important, because I'm sending the data to an Excel table. I'm sure this is an extremely inefficient, but the database is very fragmented, and I have to refer to several tables, just for one piece of data. Feel free to improve, if there is a way to.
SELECT cd.NAMECREFID, clm.CONTACTNAME, cd.ASSIGNEDPOLICY, clm.DISPLAYCLAIMNUMBER, clm.CAUSEOFLOSS, scp.PERILCODE, scp.DESCRIBE, clm.ASSIGNEDDATETIME, clm.CLOSEDATETIME, clp.PAYMENT
FROM CLMSTAT clm
inner join SCPERTBL scp on clm.ASSIGNEDPERILNUMBER = scp.PERILCODE
inner join CLMPYMT clp on clm.ASSIGNEDCLAIMNUMBER = clp.ASSIGNEDCLAIMNUMBER
inner join SNAMES sn on clm.CONTACTNAME = sn.NAME
inner join TCUSDTL cd on sn.NAMEID = cd.NAMEID
WHERE clm.CONTACTNAME Like '%Smith%'
ORDER BY clm.CONTACTNAME, clm.ASSIGNEDDATETIME DESC
You should simply try this
SELECT
DISTINCT -- at the start
cd.NAMECREFID, clm.CONTACTNAME, cd.ASSIGNEDPOLICY, clm.DISPLAYCLAIMNUMBER, clm.CAUSEOFLOSS, scp.PERILCODE, scp.DESCRIBE, clm.ASSIGNEDDATETIME, clm.CLOSEDATETIME, clp.PAYMENT
FROM CLMSTAT clm
inner join SCPERTBL scp on clm.ASSIGNEDPERILNUMBER = scp.PERILCODE
inner join CLMPYMT clp on clm.ASSIGNEDCLAIMNUMBER = clp.ASSIGNEDCLAIMNUMBER
inner join SNAMES sn on clm.CONTACTNAME = sn.NAME
inner join TCUSDTL cd on sn.NAMEID = cd.NAMEID
WHERE clm.CONTACTNAME Like '%Smith%'
ORDER BY clm.CONTACTNAME, clm.ASSIGNEDDATETIME DESC
This will give you a unique combination of all columns in the SELECT list.
However if this is not what you desire, and you simply want unique values for clm.DISPLAYCLAIMNUMBER only, I'd ask why do you need rest? If you don't then your query's SELECT list should be like below
SELECT DISTINCT clm.DISPLAYCLAIMNUMBER
FROM CLMSTAT clm
...
SELECT cd.NAMECREFID, clm.CONTACTNAME, cd.ASSIGNEDPOLICY, clm.DISPLAYCLAIMNUMBER, clm.CAUSEOFLOSS, scp.PERILCODE, scp.DESCRIBE, clm.ASSIGNEDDATETIME, clm.CLOSEDATETIME, clp.PAYMENT
FROM CLMSTAT clm
inner join SCPERTBL scp on clm.ASSIGNEDPERILNUMBER = scp.PERILCODE
inner join CLMPYMT clp on clm.ASSIGNEDCLAIMNUMBER = clp.ASSIGNEDCLAIMNUMBER
inner join SNAMES sn on clm.CONTACTNAME = sn.NAME
inner join TCUSDTL cd on sn.NAMEID = cd.NAMEID
WHERE clm.CONTACTNAME Like '%Smith%'
GROUP BY clm.DISPLAYCLAIMNUMBER
ORDER BY clm.CONTACTNAME, clm.ASSIGNEDDATETIME DESC
I am new to MS SQL and am having trouble joining 4 tables within a query.
I am trying to join Orders, Order Lines, Client, and Picked tables to create a query to show quantity ordered and picked for a client. If I comment out the last inner join for Picked, I get the correct results. When I include the inner join for Picked the query returns results but data that should be in the Picked fields is NULL. One order line can have 1 or more Picked lines.
SELECT W_Warehouse, OH.OrderID, OH.RequiredDate, C.Client, OL.LineNbr, OL.QtyOrd, P.QtyPick
FROM Order
INNER JOIN Warehouse on Order.OH_WHS = Warehouse.W_PK
INNER JOIN Client on Order.O_Client = Client.C_PK
INNER JOIN OrderLine on Order.O_PK = OrderLine.OL_PK
INNER JOIN Picked on OrderLine.O_PK = Picked.P_PK
WHERE C.CLIENT = 'WENDYS'
Without knowing the data in the tables it is difficult to answer precisely.
But as you say you have 1+ rows in the Picked table, you probably want to do aggregation with GROUP BY and SUM()
Maybe this is what you're looking for:
SELECT
W.W_Warehouse,
OH.OrderID,
OH.RequiredDate,
C.Client,
OL.LineNbr,
OL.QtyOrd,
P.QtyPick
FROM
Order OH
INNER JOIN Warehouse W on OH.OH_WHS = W.W_PK
INNER JOIN Client C on OH.O_Client = C.C_PK
INNER JOIN OrderLine OL on OH.O_PK = OL.OL_PK
CROSS APPLY (
select sum(QtyPick) as QtyPick
from Picked P
where OL.O_PK = P.P_PK
) P
WHERE
C.CLIENT = 'WENDYS'
It calculates the sum of QtyPick separately so it doesn't increase the number of lines in the result.
I got 4 tables as follows:
tbProjekt
--------------
Id
every Machine has ProjektId which belongs to:
tblMaszyna
--------------
Id
ProjektId
tblElement
--------------
Id
Name
in this table i am associating elements with machines:
tblMaszElem
--------------
Id
IdElem
IdMach
I would like to take those elements - Name from tblElement which belongs to machines which belongs to specified ProjectId. So lets say for ProjectId 10 How can i achieve that?
select e.Name
from tbElement e
inner join tbMaszElem me on me.IdElem = e.Id
inner join tbMaszyna m on m.Id = me.IdMach
inner join tbProject p on p.Id = m.ProjektId
where
p.Id = 10
This should do. This selects the Name column of all entries in the tbElement table which are associated to a machine that's associated to a project where the project ID is 10.
Please check this sample and its comment
select
te.name
from
tblMaszElem tmem
inner join tblElement te on te.id = tmem.IdElem
inner join tblMaszyna tmzy on tmzy.id = tmem.IdMach
--inner join tbProjekt tp on tp.id = tmzy.ProjektId --i think this should be avoidable
where
tp.id = 10