Sql Query help needed for product relation - sql

I have one product table with following info.
ID ProductId Name OtherData
1 0 A data1
2 0 B data2
3 1 A1 NULL
4 1 A2 NULL
I need all data with detail ProductId is relationship with ID column.
I need result like below
ID ProductId Name OtherData
1 0 A data1
2 0 B data2
3 1 A1 data1
4 1 A2 data1
What kind of join or query I should use?

SELECT s.ID, s.ProductId, s.Name,
OtherData = COALESCE(s.OtherData, r.OtherData)
FROM dbo.Products AS s
LEFT OUTER JOIN dbo.Products AS r
ON s.ProductId = r.ID;

Related

SQL split one column into two columns based on values and use columns

Table: ProductionOrder
Id Ordernumber Lotsize
1 Order1 50
2 Order 2 75
3 WO-order1 1
4 WO-order2 1
Table: history
Id ProductionOrderID Completed
1 3 1
2 3 1
3 4 1
4 4 1
Table: ProductionOrderDetail
ID ProductionOrderID ProductionOrderDetailDefID Content
1 1 16 50
2 1 17 7-1-2018
3 2 16 75
4 2 17 7-6-2018
Start of my code:
Select p.ID, p.OrderNumber,
Case productionOrderDetailDefID
Where(Select pd1.productionOrderDetailDefID where ProductionOrderDetialDefID = 16) then min(pd1.content)
from ProductionOrder p
Left join History h1 on p.id = h1.productionOrderID
Left Join ProductionOrderDetail pd1 on p.ID = ProductionOrderID
The result in trying to get is
Id Ordernumber Lotsize Productionorder Completed
1 Order1 50 WO-order1 2
2 Order 2 75 WO-order2 2
Any help would be appreciated.
Try this
SELECT ordernumber,lotsize,Ordernumber,count(Ordernumberid)
FROM productionorder inner join history on productionorder.id = history.Ordernumberid
GROUP BY Ordernumber;
A bit of weird joins going on here. You should add this to a SQL fiddle so that we can see our work easier.
A link to SQL fiddle: http://sqlfiddle.com/
Here is my first attempt
SELECT
po.id
, po.ordernumber
, po.lotsize
, po2.productionorder
, SUM(h.completed)
FROM productionorder as po
INNER JOIN history as h
ON h.id = po.id
INNER JOIN prodcuctionorder as po2
ON po2.ordernumberid = h.ordernumberid
WHERE po.id NOT EXISTS IN ( SELECT ordernumberid FROM history )
GROUP BY
po.id
, po.ordernumber
, po.lotzise
, po2.productionorder
How far does that get you?

Sql server query to join two tables to get desired result set

I have two table Customer-items and Available-locations i need to get the location where all items in customer-items are available.
Customer-items
ItemID ItemName
1 item1
2 item2
3 item3
Available-locations
LocationID ItemID AvailableForPickup
20 1 1
20 2 1
20 3 0
21 1 1
21 2 1
21 3 1
on simple inner join e.g
SELECT * FROM Customer-items
INNER JOIN Available-locations
ON Customer-items.ItemID = Available-locations.ItemsID AND AvailableForPicup = 1
this will exclude item 3 from Available-locations for LocationID 20 but return other two items for locationID 20 and all items for location 21.
i need to get the result set like; to exclude all items for location if any of item is not available.
Available-locations_CustomerItems
LocationID ItemID AvailableForPickup
21 1 1
21 2 1
21 3 1
select * from Customer-items
join Available-locations
on Customer-items.ItemID = Available-locations.ItemsID
where Available-locations.LocationID in
(select LocationID from Available-locations where AvailableForPickup = 1)
Please use the below code. It's working fine with SQL Server 2012.
DECLARE #Customer_Items TABLE (ItemsID int,ItemName VARCHAR(10))
DECLARE #Available_Locations TABLE (LocationID int,ItemsID int,AvailableForPickup int)
INSERT INTO #Customer_Items (ItemsID,ItemName)
VALUES
(1,'item1'),
(2,'item2'),
(3,'item3')
INSERT #Available_Locations
(LocationID,ItemsID, AvailableForPickup)
VALUES
(20,1,1),
(20,2,1),
(20,3,0),
(21,1,1),
(21,2,1),
(21,3,1)
SELECT LocationID,al.ItemsID,AvailableForPickup FROM #Customer_Items ci
INNER JOIN #Available_Locations al
ON ci.ItemsID = al.ItemsID AND al.AvailableForPickup = 1
WHERE al.LocationID NOT IN (SELECT al2.LocationID FROM #Available_Locations al2 WHERE al2.AvailableForPickup =0)
You could use NOT IN in following:
SELECT l.*
FROM [Customer-items] AS i
INNER JOIN [Available-locations] AS l ON i.ItemID = l.ItemID
WHERE l.LocationID NOT IN (SELECT LocationID FROM [Available-locations] WHERE AvailableForPicup = 0)
Note that - is not available in tables names. You have to use brackets [] on table names like [customer-items]. Also use alias names on tables to make It more readable.
OUTPUT
LocationId ItemId AvailableForPickup
21 1 1
21 2 1
21 3 1
SELECT B.ITEMID
,B.LOCATIONID
,B.AVAILABLEFORPICKUP
FROM #A A
RIGHT JOIN #B B ON A.ITEMID = B.ITEMID
WHERE B.AVAILABLEFORPICKUP= 1
SELECT * FROM Customer-items
INNER JOIN Available-locations
ON Customer-items.ItemID = Available-locations.ItemsID AND locationId
NOT IN (select locationID FROM Available-locations WHERE AvailableForPicup = 0)

How to Add Order to an existing table?

I have table called Products. Let say this is my table,
ID Name ParentID
-- --- --------
1 a NULL
2 b NULL
3 a1 1
4 a2 1
5 b2 2
6 b2 2
Now I need to add [Order] Column with respect to ParentID,
ID Name ParentID Order
-- --- -------- ----
1 a NULL NULL
2 b NULL NULL
3 a1 1 1
4 a2 1 2
5 b2 2 1
6 b2 2 2
Creating [Order] is trivial but inserting record is a bit tricky part
UPDATE [Products]
SET [Products].[Order] = PTT.[Order]
FROM
[Products]
INNER JOIN (SELECT ID, ROW_NUMBER() OVER (PARTITION BY PT.ParentID ORDER BY ID) AS [Order]
FROM [Products] PT
WHERE PT.ParentID IS NOT NULL) AS PTT ON PTT.ID = [Products].ID

Update statement with duplicates values

I have a table like this
Clubcard
AssoicaticeCard MainCard custID
A A 1
B B 1
C A 1
AA AA 2
BC BC 2
CC CC 2
Ar Ar 3
Bs Bs 3
Cv Cv 3
Now I need the result to be set like this where my AssoicaticeCard and MainCard have are be linked up properly.
I need the result like this to be shown. How to write an update statement for this to get achieved?
AssoicaticeCard MainCard custID
A A 1
B A 1
C A 1
AA AA 2
BC AA 2
CC AA 2
Ar Ar 3
Bs Ar 3
Cv Ar 3
I have an table with data like with which is inconsistency, now need to write an update statement to solve the issue. My table have around 1 million records.
update T1
set MainCard = T2.AssoicaticeCard
from YourTable as T1
inner join
(
select min(AssoicaticeCard) as AssoicaticeCard,
custID
from YourTable
group by custID
) T2
on T1.custID = T2.CustID
update clubcard set Maincard = 'A' where custID = 1;
update clubcard set Maincard = 'AA' where custID = 2;
update clubcard set Maincard = 'Ar' where custID = 3;

Get the max value of a column from set of rows

I have a table like this
Table A:
Id Count
1 4
1 16
1 8
2 10
2 15
3 18
etc
Table B:
1 sample1.file
2 sample2.file
3 sample3.file
TABLE C:
Count fileNumber
16 1234
4 2345
15 3456
18 4567
and so on...
What I want is this
1 sample1.file 1234
2 sample2.file 3456
3 sample3.file 4567
To get the max value from table A I used
Select MAX (Count) from A where Id='1'
This works well but my problem is when combining data with another table.
When I join Table B and Table A, I need to get the MAX for all Ids and in my query I dont know what Id is.
This is my query
SELECT B.*,C.*
JOIN A on A.Id = B.ID
JOIN C on A.id = B.ID
WHERE (SELECT MAX(COUNT)
FROM A
WHERE Id = <what goes here????>)
To summarise, what I want is Values from Table B, FileNumber from Table c (where the count is Max for ID from table A).
UPDATE: COrrecting table C above. Looks like I need Table A.
I think this is the query you're looking for:
select b.*, c.filenumber from b
join (
select id, max(count) as count from a
group by id
) as NewA on b.id = NewA.id
join c on NewA.count = c.count
However, you should take into account that I don't get why for id=1 in tableA you choose the 16 to match against table C (which is the max) and for id=2 in tableA you choose the 10 to match against table C (which is the min). I assumed you meant the max in both cases.
Edit:
I see you've updated tableA data. The query results in this, given the previous data:
+----+---------------+------------+
| ID | FILENAME | FILENUMBER |
+----+---------------+------------+
| 1 | sample1.file | 1234 |
| 2 | sample2.file | 3456 |
| 3 | sample3.file | 4567 |
+----+---------------+------------+
Here is a working example
Using Mosty’s working example (renaming the keyword count to cnt for a column name), this is another approach:
with abc as (
select
a.id,
a.cnt,
rank() over (
partition by a.id
order by cnt desc
) as rk,
b.filename
from a join b on a.id = b.id
)
select
abc.id, abc.filename, c.filenumber
from abc join c
on c.cnt = abc.cnt
where rk = 1;
select
PreMax.ID,
B.FileName,
C2.FileNumber
from
( select C.id, max( C.count ) maxPerID
from TableC C
group by C.ID
order by C.ID ) PreMax
JOIN TableC C2
on PreMax.ID = C2.ID
AND PreMax.maxPerID = C2.Count
JOIN TableB B
on PreMax.ID = B.ID