Updation in tables involving multiple conditions (one to many relationship) - sql

I have created three tables like Product, Brands and ProductBrands in ms sql server
These tables contains data as the following
Products (PId - PName - PDescription)
1 - Mobiles - blah blah
2 - T.V - blah blah
3 - A.C - blah blah
Brands (BId - BName)
1 - Samsung
2 - Sony
3 - L.G
4 - Apple
ProductsBrands (PBId - PId - BId)
1 - 1 - 1
2 - 1 - 2
3 - 1 - 3
4 - 2 - 1
5 - 2 - 3
...
...
Now I have to perform update operation on Product tables on the following conditions
If user will select product "let's say Mobile", and he will select Samsung and L.G only
so for this requirement, the updation in ProductsBrands should be like this
ProductsBrands (PBId - PId - BId)
1 - 1 - 1
{2 - 1 - 2} should be deleted
3 - 1 - 3
...
...
If user will select product "let's say Mobile", and he will select Apple
so for this requirement, the updation in ProductsBrands should be like this
ProductsBrands (PBId - PId - BId)
1 - 1 - 1
2 - 1 - 2
3 - 1 - 3
4 - 2 - 1
5 - 2 - 3
6 - 1 - 4 {new entry is made}
If user will select product "let's say Mobile", and he will select Samsung, L.G and Apple
so for this requirement, the updation in ProductsBrands should be like this
ProductsBrands (PBId - PId - BId)
1 - 1 - 1
{2 - 1 - 2} should be deleted
3 - 1 - 3
4 - 2 - 1
5 - 2 - 3
6 - 1 - 4 {new entry is made}
Now I am very very confused how to write stored procedure for all above conditions
Please help me!!!

Your scenario was a little ambiguous for me. So lets simplify it as follow, and check if I got it correct or not:
You want to Insert or Delete or even Update a record or a set of records in a table(let say tbl1), whenever a condition(let say con1) is correct(let say if con1 is correct then Insert should me made, otherwise Delete or Update should be made).
If this is what you are looking for, then you could make a use of MERGE statement(Read More).
If this is what you are looking for, and you have problem with MERGE statement, please tell us to clarify it.
Other links:Here and Here

Related

Efficient way in SQL to compute a set of rows against all other rows?

Let's say I have a table with data that looks like this:
d user val
1 1 .94
1 2 -.88
1 3 .24
1 4 .74
2 1 .35
2 2 .68
2 3 -.98
2 4 .62
3 1 -.81
3 2 .97
3 3 .29
3 4 ___ (this row doesn't exist in the database)
4 1 .76
4 2 .38
4 3 -.98
4 4 .15
5 1 .69
5 2 .27
5 3 -.49
5 4 -.59
For a given user (let's say 2), I would like the following output:
user calc
1 -.102
3 .668
4 -.1175
Generalized:
user calc
1 ((-.88 - .94) + (.68 - .35) + (.97 - -.81) + (.38 - .76) + (.27 - .69)) / 5
3 ((-.88 - .24) + (.68 - -.98) + (.97 - .29) + (.38 - -.98) + (.27 - -.49)) / 5
4 ((-.88 - .74) + (.68 - .62) + (.38 - .15) + (.27 - -.59)) / 4
Generalized Further:
user calc
1 sum of (user2's d value - user1's d value) / count
3 sum of (user2's d value - user3's d value) / count
4 sum of (user2's d value - user4's d value) / count
To explain further, I'd like to obtain an output that shows everyone's relation to a given user (in this case user 2). In my actual dataset there are hundreds of unsorted distinct users and d values, but I've tried to simplify the dataset for this question.
Also, please note that not all user's have a d value, so it should only factor in matching sets. See how in the example above user 4 doesn't have a value for d=3, so that one is skipped in the calculation.
A join and aggregation should work:
select
t2.user, avg(t1.val - t2.val) as calc
from my_table t1
join my_table t2 on t1.d = t2.d and t1.user <> t2.user
where t1.user = 2
group by t2.user

Group By then concat items with commas

Using event hubs, I'm pushing my data to azure stream analytics. SearchWords, ItemId, UserId are some my data property.
What I want to do is, grouping my searchwords and concat the items. For example:
Data:
SearchWords ItemId UserID
wordA - 100 - 1
wordA - 102 - 1
wordC - 103 - 2
wordD - 102 - 3
wordA - 105 - 5
Output:
wordA - 100,102,105
wordC - 103
wordD - 102
How can i do this in stream analytics query?
You can use user defined aggregates to do this.
https://learn.microsoft.com/en-us/azure/stream-analytics/stream-analytics-javascript-user-defined-aggregates

Query - ComboBox - criteria select - MS Access

I have the following data (examples):
tblModel
Mod_ID Mod_Num Mod_Desc
1 0001 Model01
2 0002 Model02
tblArticle
Art_ID Art_Num Art_Desc
1 001 Article X
2 002 Article Y
3 003 Article Z
4 004 Article K
tblOperation
Op_ID Op_Num Op_Desc
1 01 Gluing
2 02 Sewing
3 03 Stitching
tblMaterial
Mat_ID Mat_Num Mat_Desc
1 B001 Blue
2 R001 Red
3 K001 Black
And the data structure is as follows:
tblModelArticle
MA_ID Mod_ID Art_ID MA_Description
1 1 1 Model01 - Article X
2 1 2 Model01 - Article Y
3 2 3 Model02 - Article Z
4 2 4 Model02 - Article K
tblModelOperation
MO_ID Mod_ID Op_ID MO_Description
1 1 1 Model01 - Op Gluing
2 1 2 Model01 - Op Sewing
3 1 3 Model01 - Op Stitching
4 2 1 Model02 - Op Gluing
5 2 3 Model02 - Op Stitching
tblArticleOperationMaterial
AOM_ID MA_ID MO_ID Mat_ID AOM_Description
1 1 3 1 Model01 - Article X - Operation Stitching - Material Blue
2 2 3 2 Model01 - Article Y - Operation Stitching - Material Red
3 3 5 2 Model02 - Article Z - Operation Stitching - Material Red
4 4 5 3 Model02 - Article K - Operation Stitching - Material Black
My question is: How do I make a ComboBox dropdown in tblArticleOperationMaterial limited? That is, once I for example select "MA_ID" to be 1, I want to be able to select only the 1, 2, 3 as MO_ID. For instance if "MA_ID" 2 value was input, the MO_ID dropbox combox box would offer only options of 4 and 5 to be selected as MO_ID.
its called cascading action. search for cascading combobox or
check this :MS Access Forms : How to dynamically change the select options in a combo box?
or similar question:
Get ID Filed from a Combobox in a Text Box where 1st column set to 0

SQL - Count with distinct on ID

Here is an example of my table structure:
Key - Country - Store
1 - Germany- YYY
1 - Germany- YYY
2 - France- XXX
2 - France- XXX
2 - France- XXX
3 - United Kingdom- YYY
3 - United Kingdom- YYY
4 - Germany- YYY
5 - France- YYY
5 - France- YYY
I would like to start a query on this table to get the following result:
Country- XXX - YYY
Germany - 0 - 2
France - 1 - 1
United Kingdom - 0 - 1
The problem for me ist the ID/Key which does not get used once but several times, thats why I cant just use a count query.
What should my query look like?
You should be able to use the TRANSFORM function to get this result:
TRANSFORM Count(store)
SELECT Country
FROM
(
select distinct key, country, store
from yourtable
) d
GROUP BY Country
PIVOT Store

Select Best value from Text Field

I need to modify a query that is designed to populate our Online ordering site with the products it can sell. I get my data from a view. That veiw has all of the products for sale. Sometimes products can be sold in kits of multiple products. This kits have their own product ID but the veiw renders them with a record for each product so the data looks something like:
1 - item1 - item1desc - 1 - true
2 - item2 - item2desc - 1 - true
3 - item3 - item3desc - 1 - true
4 - item4 - item1desc - 3 - true
4 - item4 - item2desc - 3 - true
4 - item4 - item3desc - 3 - true
What i would like to see is
1 - item1 - item1desc - 1 - true
2 - item2 - item2desc - 1 - true
3 - item3 - item3desc - 1 - true
4 - item4 - kit includes item1desc, item2desc, item3desc - 3 - true
or
4 - item4 - kit - 1 - true
This is the query I have but it still returns 3 rows for a 3 item kit
SELECT [CustomerProductID]
,[CustomerProductName] AS CustomerItemName
,MAX([ProductDescription]) AS CustomerItemDescription
,COUNT([ProductNameID]) AS ProductCount
,[IsActive]
,[ModifiedDate]
FROM [dbo].[vw_ProductList]
where CustomerID in (#tbl)
GROUP BY [CustomerProductID],
[CustomerProductName],
[IsActive],
[ModifiedDate]
Any ideas how to fix this?
It is probably [ModifiedDate] that is different for each row in a "3 item kit".
Remove [ModifedDate] from GROUP BY use MAX([ModifedDate]) in the field list.