I have two things to do: optimize table, write a query.
customer
id customer year_in_business is_discountable customers_friend_id
1 a 2 true 2
2 b 3 false 1
3 c 2 true 1
4 d 3 true null
Here customer_friend_id is pointing back to pk(id) of the same table.
product
id product
1 xxx
2 yyy
3 aaaa
customer_product
customer_id product_id
1 2
2 3
3 1
Questions
How to make these tables flexible or extensible in the future?
Get Name of each customer that has a friend and the name of that customer’s friend.
Here is one suggestion: remove customer.customers_friend_id and create a new CustomerFriend table like so:
CustomerFriend
customerFriendID customerID friendID
1 1 2
2 2 1
3 3 1
This will allow customers with more than one friend, and avoids nulls for customers without friends.
Related
I'm trying to create an SQL statement to match either an id number or a postcode and then assign a new id number
What I want to end up with is ‘newid’ that correctly recognizes that the first four records are the same person (even though the postcode for record 2 is different).
record id postcode newid
--------------------------
1 1 1 1
2 1 2 1
3 1 1 1
4 2 1 1
5 3 3 2
Any suggestions would be appreciated greatly.
Going based on your example:
SELECT RECORD,
(SELECT MIN (ID)
FROM users u2
WHERE users.id IN (u2.id, u2.postcode)
OR users.postcode in (u2.id, u2.postcode)
) AS newid
FROM users
This results with the following data:
RECORD NEWID
------------------
1 1
2 1
3 1
4 1
5 3
Here is the SQLFiddle
I have found several solutions for my type of problem, but I having trouble applying it in my situation.
Essentially I have a Vehicle Table:
License VIN Region
1 1 1
1 2 2
2 3 1
2 3 2
3 4 1
3 4 2
3 5 3
I want to take the license and vin from region 1 and see if the vin matches in all other regions based on the license. If it doesn't I want all the rows that don't match, but if it does match I don't want the row. So a complexity does come in when say I have 3 licenses and region 1 matches one row, but not the other, I want both the unmatched and region 1; however, when I have 3 licenses that all match I don't want any rows including region 1.
So my results in this case would be:
License VIN Region
1 1 1
1 2 2
3 4 1
3 5 3
I am using SQL Server 2005.
I think this is what you're looking for
SELECT DISTINCT a.*
FROM Vehicle AS a
INNER JOIN Vehicle AS b
ON a.License = b.License
WHERE a.VIN != b.VIN
AND a.Region != b.Region
AND (a.Region = 1 OR b.Region = 1)
Goal:
Load the destination table c with data from table a and b.
Problem:
I have two tables, a and b with that need to be migrated into same table c.
Need to load one table at a time. When loaded to table c, the criteria för table a need to define that criteria most be access 1 and for the table b the access most be 2.
The question is:
How do you make criteria for the for the table c?
Requested result for table c:
table c
----------
number access gender
--------------------
1 1 2
1 2 2
2 2 2
3 1 1
4 2 1
5 1 2
5 2 2
table a
----------
number access gender
--------------------
1 1 2
2 1 2
3 1 1
4 1 1
5 1 2
table b
----------
number access gender
--------------------
1 2 2
2 2 2
3 2 1
4 2 1
5 2 2
table c
----------
number access gender
--------------------
1 1 null
1 2 null
2 2 null
3 1 null
4 2 null
5 1 null
5 2 null
I think I understand what you are trying to do, try using a couple of MERGE statements like so:
MERGE c AS target
USING a AS source
ON (target.number = source.number, target.access = source.access)
WHEN MATCHED THEN
UPDATE SET target.gender = source.gender
WHEN NOT MATCHED BY target THEN
INSERT (number, access, gender)
VALUES (source.number, source.access, source.gender)
MERGE c AS target
USING b AS source
ON (target.number = source.number, target.access = source.access)
WHEN MATCHED THEN
UPDATE SET target.gender = source.gender
WHEN NOT MATCHED BY target THEN
INSERT (number, access, gender)
VALUES (source.number, source.access, source.gender)
Let's say I have the following tables
Collection DirectorId StudioId
1 NULL 1
2 2 NULL
3 NULL 2
Director MovieId
1 1
2 4
Studio MovieId
1 1
2 3
2 4
1 2
The collection table will always have either director or studio id but never both.
How could I write a query to get all the movies in one a collection so that I end up with
CollectionID MovieID
1 1
1 2
2 4
3 3
3 4
I hope this makes sense
This should work (not sure about your column names, but you get the idea):
select c.CollectionID, isnull(d.MovieId, s.MovieId) MovieID
from Collection c
left join Director d on d.Director=c.DirectorId
left join Studio s on s.Studio=c.StudioId
I have a scenario as follows:
ManufacturerID Name
1 XXX
2 YYY
DeptID Name ManufacturerID
1 abc 1
2 bcd 1
3 efg 2
ProductID name deptid
1 dfdfg 1
2 dfdg 2
3 sdfsd 2
PartsID name productid
1 sdfs 1
2 sfdfs 2
3 sdd 1
I want the above table structure to be made as hierarchical using levels. How do I design the table?
I cannot understand what hierarchy you need to make, but if you're using sql server 2008 please take a look at hierachyId datatype.
And please provide more desdcription about you process. Now it looks straightforward:
Manufacturer has many departments, department has many products, product has many parts.