SQL query / CTE for Association table having many to many relationships - sql-server

I have two tables A and B. B can have many records of A but B will not have any records of A
One-to-many -> one way
Now
Table A:
Id Name
----------
1 Rule1
2 Rule2
Table B:
Id TargetedTypeId TargetedId TargetingType TargetingId Status
TargetedType table:
Id Name
--------------
1 Users
2 Employee
TargetingType table:
Id Name
-----------------
1 Vegetable
2 Fruits
Users table:
Id Name
--------------
1 Abc
2 xyz
Vegetables table:
Id Name
-----------------
1 Onion
2 Potato
Fruits table:
Id Name
---------------
1 Apple
2 Orange
Table B will have rows like this:
Id | TargetedTypeId | TargetedId (Table A Ids) | TargetingType | TargetingId | Status
-----------------------------------------------------------------------
1 1 2 1 2 1
2 1 2 1 1 1
3 1 2 2 1 1
4 1 2 2 2 1
Please hep me to write a query which will fetch A tables entries present in B with status 1 and fetch all targeting types and get fruit details if its fruit and if vegetable if its vegetable type which is TargetingType and fetch its details with respective table fruit/vegetable with Pagination

Related

SQL Server, finding the SUM of a foreign key column

I currently have 3 tables.
Table 1: customers
id(PK) name surname
----------------------------------
1 name1 surname1
2 name2 surname2
3 name3 surname3
4 name4 surname4
Table 2: sales
id(FK) game(FK) price(FK)
-----------------------------
1 1 1
2 4 4
3 4 4
4 3 3
1 3 3
2 3 3
3 2 2
Table 3: stock
id(FK) game price
-----------------------------
1 game1 20
2 game2 30
3 game3 40
4 game4 50
What I'm looking to do is find the sum of all the sales listed in the sales table (table 2).
So far, I can display a table showing how much money each game has made in total but cannot get the overall total of sales to display.
I have tried
select sum(sales.price)
from sold
However, this is just calculating the sum of the foreign key (in this case it would be 20). However, I want it to display 270.
You need to join the stock and sales tables to get the correct price of each of the items sold.
Select sum(stock.price) from sales
inner join stock on sales.game = stock.id

oracle database, Omitting records from a table which are subset of records in same table based on two columns

I have some customers which are clustered into groups by cluster_id and the amount of data is huge (So performance is a matter here). The simplest form of what I have is the following table:
cust_id | cluster_Id
---------- | -----------
1 | 1
2 | 1
3 | 1
2 | 2
1 | 2
2 | 3
4 | 3
1 | 4
I want those clusters with greatest number of customers such that no costumer be removed. In other words, I want to delete records of a cluster that is subset of another cluster.
In above example, the output table should look like this:
cust_id cluster_Id
-------- | ----------
1 | 1
2 | 1
3 | 1
2 | 3
4 | 3

SQL Query to get data based on multiple filters

I have following Product table and ProductTag tables -
ID | Product
--------------
1 | Product_A
2 | Product_B
3 | Product_C
TagID | ProductID
----------------------
1 | 2
1 | 3
2 | 1
2 | 2
2 | 3
3 | 1
3 | 2
Now I need a SQL query that return all products list which are having both Tag 1 and 2. Result should be as given below -
ProductID | Product
------------------------
2 | Product_B
3 | Product_C
Please suggest how can i write a MS SQL query for this.
SELECT p.ID, p.Product
FROM Product p
INNER JOIN ProductTag pt
ON p.ID = pt.ProductID
WHERE pt.TagID IN (1, 2) -- <== Tags you want to find
GROUP BY p.ID, o.Product
HAVING COUNT(*) = 2 -- <== tag count on WHERE clause
however, if TagID is not unique on every Product, you need to count only the distinct product.
HAVING COUNT(DISTINCT pt.TagID) = 2
More on: SQL of Relational Division

Microsoft Access finding second, third, Nth element in a group

I have a table: worker_id, company_id
worker_id is primary key
worker_id | company_id
----------------------
1 | 1
2 | 1
3 | 2
4 | 3
5 | 3
6 | 1
I know how to find the first worker_id in each company_id using Min()
SELECT Min([workers per company].worker_id) AS MinOfworker_id, [workers per company].company_id
FROM [workers per company]
GROUP BY [workers per company].company_id;
Resulting in:
worker_id | company_id
----------------------
1 | 1
3 | 2
4 | 3
I know how to find the last worker_id in each company_id using Max()
SELECT Max([workers per company].worker_id) AS MaxOfworker_id, [workers per company].company_id
FROM [workers per company]
GROUP BY [workers per company].company_id;
Resulting in:
worker_id | company_id
----------------------
6 | 1
3 | 2
5 | 3
Now I want to get the second (or third, Nth) worker_id per company_id resulting in:
worker_id | company_id
----------------------
2 | 1
5 | 3
There is a way to do this?
EDIT:
In kdb+ I can use:
select w:worker_id[1] by company_id from workers_per_company
to get the 2nd worker_id
resulting in:
company_id| w
-------------
1 | 2
2 |
3 | 5
Is there something similar in MS Access?
You are trying to do something in SQL that SQL was not meant for. Instead, consider writing a VBA procedure to iterate over a recordset sorted by company.

need result of two table sql query

I have a two sql server tables that contains data like this
Table a
catId | catname | Isdeleted
-------------------------------------------------
1 ABC 0
2 DEF 0
3 GHI 0
and another table is
Table B
id | Name | Name1 | Catid
--------------------------------------------------
1 abc aaaa 1
2 def bbbb 1
3 ghi gggg 2
4 jkl jjjj 2
5 xyz xxxxx 3
Now I want result in this format
catname from table a and all the fields from table b according to
catid of table a and catname should be distinct.
Please help me
Write your query like this :
SELECT DISTINCT a.catname, b.* FROM a INNER JOIN b
ON a.catid = b.catid WHERE catid = [catid]
If you have multiple records in table b for each catid or catname, you will see multiple records with same catname in result. there is no other choice unless catname be unique in both tables a and b.

Resources