i have 3 database
supplier(sid:integer,sname:string,Address:string)
parts:(pid:integer,pname:string,color:string)
catalog(sid:number,pid:number,cost:string)
qn: find names of suppliers who supply only green parts
Don't know how you are storing your colors (i.e. HTML color defintion, name or what) but a query like this might do the trick:
select supplier.name
from supplier
where supplier.sid in (
-- all the suppliers who supply green parts
select distinct(a.sid)
from catalog a
inner join parts b on a.pid = b.pid
where b.color = 'green')
and supplier.sid not in (
-- all the supplier ids who supply parts that are not green
select distinct(a.sid)
from catalog a
inner join parts b on a.pid = b.pid
where b.color <> 'green')
This returns the supplier names that ONLY stock green parts.
select supplier.sid, supplier.sname, supplier.Address from catalog where part.color = 'green' left join supplier on (catalog.sid = supplier.sid)
left join part on (catalog.pid = parts.pid);
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 a Link structure table with ID and parentID.
ID, Parent, name
1,1
2,1
3,2
4,3
5,3
To this table I have a structure_article relation table
in this table I have relation between a Link and a article.
struture_article
structid, articleID
4,1000
4,1001
5,1002
Every article in that table have a supplier.
Now i am trying to create a recursive function that creates the tree
nods if i pick a specific supplier.
Article table
ArticleID, SUPPLIER ID
1000,1
1001,2
1002,2
If I pick articles with supplier 1 then I want the function to show me the tree structure that have articles from that supplier.
I have 20 suppliers and 300 links in the DB now i want only to show articles from the suppliers i pick. I don want any empty nods.
Is this even possible do create with a recursive function in Sql Server version 2008?
I tyied wiht this code the problem is that i get only nods that have articles connected
WITH a
AS (SELECT *
FROM structure
WHERE parent = 125
UNION ALL
SELECT m.*
FROM structure m
JOIN a
ON m.parent = a.internidstructure)
SELECT *
FROM a
WHERE internidstructure IN (SELECT DISTINCT( internidstructure )
FROM dbo.articles
INNER JOIN dbo.structure_article
ON dbo.articles.internidarticle =
dbo.structure_article.internidarticle
WHERE ( dbo.articles.internidsupplier IN (SELECT
internidsupplier
FROM site_sup
WHERE
internidsite = 1) ))
ORDER BY parent,
sortno
Try using a left join instead of an inner join.
I'm not sure to have understood your need but if you want to have a new tree table without nodes not linked to a supplier, this query can work.
WITH A AS
(
SELECT S.ID as ID, S.Parent as Parent, 1 as art_linked
FROM structure S
INNER JOIN dbo.structure_article SA
ON S.ID = SA.structid
INNER JOIN Article AR
ON AR.ArticleID = SA.ArticleID
WHERE AR.SupplierID = 1
UNION ALL
SELECT S.ID, S.Parent, 0
FROM structure S
INNER JOIN A
ON A.parent = S.ID
WHERE S.ID <> S.Parent
)
SELECT A.ID, A.Parent, MAX(A.art_linked)
FROM A
GROUP BY A.ID, A.Parent
Consider these three tables:
Content:
Id(pk), Name, and ContentTypeId(fk from ContentType)
ContentTypeId:
This table will have values like
1 Text
2 Number
...
Id(pk), Value
ContentUpdates:
Id(fk from Content), Status, UpdateDate
To get data from all three for a report there is a View like so:
select C.Id, C.Name, CT.Value, CU.UpdateDate
from ContentUpdates CU
join Content C
On CU.Id = C.Id
join ContentType CT
On C.ContentTypeId = CT.Id;
Currently this particular View gives me 800 records.
I had a similar requirement where in I needed all those columns plus a filter based on the Status column in ContentUpdates table
I thought of using this View joining the 'ContentUpdates' table on the ID.
So I might want to get all the Content that has a Status of 5, for example.
Currently there are only 2 records with that Status, however using a JOIN on the View the result set has way more records than that.
What am I doing wrong?
Can the View be actually used or am I better off with this:
SELECT C.Id, C.Name, CT.Value, CU.UpdateDate
FROM ContentUpdates CU
JOIN Content C
On CS.Id = C.Id
JOIN ContentType CT
On C.ContentTypeId = CT.Id
WHERE CU.Status = 5;
Yes, you can use a view, but you need to expose any of the fields you want to query by or display:
CREATE VIEW content_view
AS
SELECT C.Id, C.Name, CT.Value, CU.UpdateDate, CU.Status
FROM ContentUpdates CU
INNER JOIN Content C On CS.Id = C.Id
INNER JOIN ContentType CT On C.ContentTypeId = CT.Id;
then:
SELECT * FROM content_view WHERE Status = 5;
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.
I need to select a random record from 3 tables and ensure I am ordering by photoOrder
Select TOP 1(a.id), a.mls_number, a.parcel_name, a.property_type, a.ownership_type, b.filename, b.photoOrder, c.county_Name
From property as a
Inner JOIN
listingPhotos as b on a.id = b.ListingID
LEFT JOIN
counties as C on a.county_name = c.id
WHERE a.isCommercial = 'True'
Order By NEWID()
So this query works, but I need to ensure that the b.filename record is ordered by b.photoOrder and thus the b.photoOrder should always be 1.
The b table (listing photos) has multiple photo files per property and I need to only select the photo that is 1st in the photo order.
Thanks
You could subquery your listingPhotos table and limit to WHERE PhotoOrder = 1:
Select TOP 1(a.id), a.mls_number, a.parcel_name, a.property_type, a.ownership_type, b.filename, b.photoOrder, c.county_Name
From property as a
Inner JOIN
(SELECT ListingID , filename, PhotoOrder FROM listingPhotos WHERE PhotoORder = 1
) as b on a.id = b.ListingID
LEFT JOIN
counties as C on a.county_name = c.id
WHERE a.isCommercial = 'True'
Order By NEWID()