Using select inner join in sql - sql

So I have 5 tables:
dane_osobowe:
(PK) id
imie
druzyna:
(PK) id
nazwa
gracz:
(PK) id
(FK) dane_osobowe
(FK) pozycja
kontrakt:
(PK) id
(FK) gracz
(FK) druzyna
pozycja:
(PK) id
nazwa
How can I chose all "gracz" from druzyna "1" which has "pozycja" 2?
I tried something like this:
SELECT *
FROM gracz AS gr
INNER JOIN kontrakt AS kg
ON gr.ID = kg.Gracz
INNER JOIN Druzyna AS d
ON kg.Druzyna = d.ID
WHERE d.ID = 1
INNER JOIN pozycja as poz
ON poz.id = gracz.pozycja
WHERE gracz.pozycja = 2
But it doesn't work :/ Somebody have idea what I'm doing wrong?

Try this:
SELECT *
FROM gracz AS gr
INNER JOIN kontrakt AS kg
ON gr.ID = kg.Gracz
INNER JOIN Druzyna AS d
ON kg.Druzyna = d.ID
--Remove the where condition from here
INNER JOIN pozycja as poz
ON poz.id = gracz.pozycja
WHERE gracz.pozycja = 2
and d.ID = 1 --Add it over here using "and"
ie, move all the where conditions together at the last.
EDIT:
To get the selected columns you can specify it like this:
SELECT d.id, gr.dane_osobowe, poz.nazwa
FROM gracz AS gr
INNER JOIN kontrakt AS kg
ON gr.ID = kg.Gracz
INNER JOIN Druzyna AS d
ON kg.Druzyna = d.ID
--Remove the where condition from here
INNER JOIN pozycja as poz
ON poz.id = gracz.pozycja
WHERE gracz.pozycja = 2
and d.ID = 1 --Add it over here using "and"

Related

Postgres - Query that Join two tables

I have two table that need to join them in one table.
1: First Query
SELECT cu.user_name,
li.successdate
FROM logininfo li
JOIN user_mapping um ON um.user_key = li.username
JOIN cwd_user cu ON um.username = cu.user_name
ORDER BY successdate;
Result:
user_name | successdate
---------------------+-------------------------
K_Daniel | 2018-09-02 13:38:22.331
2: Second Query
WITH last_login_date AS
(SELECT user_id
, to_timestamp(CAST(cua.attribute_value AS double precision)/1000) AS last_login
FROM cwd_user_attribute cua
WHERE cua.attribute_name = 'lastAuthenticated'
AND to_timestamp(CAST(cua.attribute_value AS double precision)/1000) < (CURRENT_DATE))
SELECT c.user_name
, g.group_name
FROM cwd_user c
INNER JOIN last_login_date l ON (c.id = l.user_id)
INNER JOIN cwd_membership m ON (c.id = m.child_user_id)
INNER JOIN cwd_group g ON (m.parent_id = g.id)
WHERE g.group_name LIKE '%CEO-%' ;
Result:
user_name | group_name
------------------+----------------------------------------------------
K_Daniel | CEO-Building1
3-Here is the expected result:
user_name | successdate | group_name
---------------------+-------------------------+-----------------------
K_Daniel | 2018-09-02 13:38:22.331 | CEO-Building1
what is the appropriate query to join these table?
Any idea
Thanks,
after serval workaround I found solution.
WITH last_login_date AS
(SELECT user_id
, to_timestamp(CAST(cua.attribute_value AS double precision)/1000) AS last_login
FROM cwd_user_attribute cua
WHERE cua.attribute_name = 'lastAuthenticated'
AND to_timestamp(CAST(cua.attribute_value AS double precision)/1000) < (CURRENT_DATE))
SELECT c.user_name
, li.successdate
, g.group_name
FROM cwd_user c
INNER JOIN last_login_date l ON (c.id = l.user_id)
INNER JOIN cwd_membership m ON (c.id = m.child_user_id)
INNER JOIN cwd_group g ON (m.parent_id = g.id)
INNER JOIN user_mapping um ON (c.user_name = um.username)
INNER JOIN logininfo li ON (um.user_key = li.username)
WHERE g.group_name LIKE 'CEO-%' ;

SQL Filtering rows with no duplicate value

Hi so I'm new to SQL and I'm trying to find a way in which I can obtain only the rows that have values that are not duplicate to each other in a specific column of table.
For example the Table below is called T1 and contains:
ID|Branch ID
1 444
2 333
3 444
4 111
5 555
6 333
The result I want will be
ID|Branch ID
4 111
5 555
So only showing non duplicate rows
Edit: I want to apply this to a large relational code. Here is a snippet of where I want it to be added
FROM dbo.LogicalLine
INNER JOIN dbo.Page ON dbo.LogicalLine.page_id = dbo.Page.id
INNER JOIN dbo.Branch ON dbo.LogicalLine.branch_id = dbo.Branch.id
The table LogicalLine will have a column called branch_id containing duplicate id values. I wish to filter those out showing only the non-duplicate branch_id like above example then INNER JOIN the Branch table into the LogicalLine which I have done.
Added -Full Code here:
SELECT
(SELECT name
FROM ParentDevice
WHERE (Dev1.type NOT LIKE '%cable%') AND (id = Dev1.parent_device_id))T1_DeviceID,
(SELECT name
FROM Symbol
WHERE (id = CP1.symbol_id) AND (type NOT LIKE '%cable%'))T1_DeviceName,
(SELECT name
FROM Location
WHERE (id = Page.location_id))T1_Location,
(SELECT name
FROM Installation
WHERE (id = Page.installation_id))T1_Installation,
(SELECT name
FROM ParentDevice
WHERE (Dev2.type NOT LIKE '%cable%') AND (id = Dev2.parent_device_id))T2_DeviceID,
(SELECT name
FROM Symbol
WHERE ( id = CP2.symbol_id) AND (type NOT LIKE '%cable%'))T2_DeviceName,
(SELECT name
FROM Location
WHERE (id = PD2.location_id))T2_Location,
(SELECT name
FROM Installation
WHERE (id = Page.installation_id))T2_Installation,
(SELECT devicefamily
FROM Device
WHERE (type LIKE '%cable%') AND (id = SymCable.device_id))CablePartNumber,
(SELECT name
FROM ParentDevice
WHERE (id = DevCable.parent_device_id) AND (DevCable.type LIKE '%cable%'))CableTag
FROM dbo.LogicalLine
INNER JOIN dbo.Page ON dbo.LogicalLine.page_id = dbo.Page.id
INNER JOIN dbo.Branch ON dbo.LogicalLine.branch_id = dbo.Branch.id
LEFT OUTER JOIN dbo.Symbol AS SymCable ON dbo.LogicalLine.cable_id = SymCable.id
LEFT OUTER JOIN dbo.Device AS DevCable ON SymCable.device_id = DevCable.id
LEFT OUTER JOIN dbo.ParentDevice AS ParentCable ON DevCable.parent_device_id = ParentCable.id
INNER JOIN dbo.SymbolCP AS CP1 ON dbo.Branch.cp1_id = CP1.id
INNER JOIN dbo.SymbolCP AS CP2 ON dbo.Branch.cp2_id = CP2.id
INNER JOIN dbo.Symbol AS S1 ON CP1.symbol_id = S1.id
INNER JOIN dbo.Symbol AS S2 ON CP2.symbol_id = S2.id
INNER JOIN dbo.Device AS Dev1 ON S1.device_id = Dev1.id
INNER JOIN dbo.Device AS Dev2 ON S2.device_id = Dev2.id
INNER JOIN dbo.ParentDevice AS PD1 ON Dev1.parent_device_id = PD1.id
INNER JOIN dbo.ParentDevice AS PD2 ON Dev2.parent_device_id = PD2.id
INNER JOIN dbo.Location AS L1 ON PD1.location_id = L1.id
INNER JOIN dbo.Location AS L2 ON PD2.location_id = L2.id
INNER JOIN dbo.Installation AS I1 ON L1.installation_id = I1.id
INNER JOIN dbo.Installation AS I2 ON L2.installation_id = I2.id
WHERE
(PD1.project_id = #Projectid) AND (dbo.LogicalLine.drawingmode LIKE '%Single Line%');
Select Id, BranchId from table t
Where not exists
(Select * from table
where id != t.Id
and BranchId = t.BranchId)
or
Select Id, BranchId
From table
Group By BranchId
Having count(*) == 1
EDIT: to modify as requested, simply add to your complete SQL query a Where clause:
Select l.Id BranchId, [plus whatever else you have in your select clause]
FROM LogicalLine l
join Page p ON p.id = l.page_Id
join Branch b ON b.Id = l.branch_id
Group By l.branch_id, [Plus whatever else you have in Select clause]
Having count(*) == 1
or
Select l.Id BranchId, [plus whatever else you have in your select clause]
FROM LogicalLine l
join Page p on p.id = l.page_Id
join Branch b on b.Id = l.branch_id
Where not exists
(Select * from LogicalLine
where id != l.Id
and branch_id = l.branch_id)

replace NOT IN with LEFT JOIN

I've got a table structure like this
[condition]
condition_id, (pk)
question_id (fk)
[option]
condition_id, (fk)
ext_id (fk)
[external]
ext_id, (pk)
inst_id (fk)
[instance]
inst_id, (pk)
keeper_id (fk)
[keeper]
keeper_id, (pk)
org_id
[question]
question_id (pk)
org_id
[localization]
question_id (fk, pk),
org_id (pk),
language (pk)
label
I need to get all questions from question table with localizations (from localization table), that do not exist in condition table for certain ext_id.
My query is
SELECT
q.question_id as q_id,
l.label as q_value
FROM question q
INNER JOIN localization l
ON l.question_id = q.question_id
INNER JOIN external ex
ON ex.ext_id = 'EXTERNAL_ID'
INNER JOIN instance i
ON i.inst_id = ex.inst_id
INNER JOIN keeper k
ON k.keeper_id = i.keeper_id
WHERE q.org_id IN ('*', k.org_id)
AND l.org_id = '*'
AND l.language = 'EN'
AND q.question_id NOT IN (
SELECT
question_id
FROM condition c
INNER JOIN option o
ON o.condition_id = c.condition_id
WHERE o.ext_id = 'EXTERNAL_ID'
)
But how to replace that subquery with LEFT JOIN?
Here is one way
SELECT p.question_id AS q_id,
l.label AS q_value
FROM question q
INNER JOIN localization l
ON l.question_id = q.question_id
INNER JOIN EXTERNAL ex
ON ex.ext_id = 'EXTERNAL_ID'
INNER JOIN instance i
ON i.inst_id = ex.inst_id
INNER JOIN keeper k
ON k.keeper_id = i.keeper_id
LEFT JOIN (select distinct c.question_id
from condition c
JOIN option o
ON o.condition_id = c.condition_id
AND o.ext_id = 'EXTERNAL_ID' ) c
ON c.question_id = q.question_id
WHERE q.org_id IN ( '*', k.org_id )
AND l.org_id = '*'
AND l.language = 'EN'
AND c.question_id IS NULL
I am not a expert in Postgres but I prefer NOT EXISTS to do this
SELECT p.question_id AS q_id,
l.label AS q_value
FROM question q
INNER JOIN localization l
ON l.question_id = q.question_id
INNER JOIN EXTERNAL ex
ON ex.ext_id = 'EXTERNAL_ID'
INNER JOIN instance i
ON i.inst_id = ex.inst_id
INNER JOIN keeper k
ON k.keeper_id = i.keeper_id
WHERE q.org_id IN ( '*', k.org_id )
AND l.org_id = '*'
AND l.language = 'EN'
AND NOT EXISTS (SELECT 1
FROM condition c
INNER JOIN option o
ON o.condition_id = c.condition_id
WHERE o.ext_id = 'EXTERNAL_ID'
AND q.question_id = c.question_id)

Need help fixing the syntax for this LEFT OUTER JOIN

I have a database structure like so:
SELECT * FROM Culture;
------------------------
Id ShortName FullName Supported
22 en-US English (United States) 1
23 fr-FR French (France) 1
24 hi-IN Hindi (India) 0
SELECT * FROM ResourceKey;
----------------------------
Id Name
20572 HowAreYou
20571 Hello
20573 ThisKeyHasUSEnglishValueOnly
SELECT * FROM Strings;
-----------------------
Id CultureId ResourceKeyId ResourceValue
41133 22 20571 Hello
41134 22 20572 How are you?
41135 23 20571 Bonjour
41136 23 20572 Comment allez-vous?
41137 22 20573 This key has US English value only.
SELECT * FROM Category;
------------------------
Id Name
1 JavaScript
SELECT * FROM StringCategory;
------------------------------
Id ResourceKeyId CategoryId
1 20571 1
2 20572 1
3 20573 1
I want to display all resource key names and resource values, i.e. string values against each key, for, say, the French (France) culture, i.e. the culture with the ShortName fr-FR but even if a key does not have a value in the culture, it must display the key name but NULL for the value. Like so:
Name ResourceValue
-------------------------------------------------------
Hello Bonjour
HowAreYou Comment allez-vous?
ThisKeyHasUSEnglishValueOnly NULL
It seems like a simple LEFT OUTER JOIN application to me, but my code isn't working. Could someone please help correct my code?
My query is:
SELECT ResourceKey.Name AS Name, ResourceValue
FROM
ResourceKey LEFT OUTER JOIN Strings
ON
Strings.ResourceKeyId = ResourceKey.Id
INNER JOIN StringCategory
ON
StringCategory.ResourceKeyId = Strings.ResourceKeyId
INNER JOIN Category
ON
StringCategory.CategoryId = Category.Id
LEFT OUTER JOIN Culture
ON
Strings.CultureId = Culture.Id AND Culture.Id = (SELECT Id FROM Culture WHERE ShortName = 'fr-FR')
AND
Category.Name = 'JavaScript';
Somehow, the last join in the above-query turns out to become an inner join, eliminating those rows where there is no value in the said culture.
SELECT a.name, b.ResourceValue
FROM ResourceKey a
LEFT JOIN
(
SELECT b.ResourceKeyID, b.ResourceValue
FROM Strings b
INNER JOIN Culture c
ON b.CultureID = c.ID
WHERE c.shortname = 'fr-FR'
) b ON a.ID = b.ResourceKeyId
UPDATED
USE SSTOBMAY;
SELECT a.name, b.ResourceValue
FROM ResourceKey a
LEFT JOIN
(
SELECT b.ResourceKeyID, b.ResourceValue
FROM Strings b
INNER JOIN Culture c
ON b.CultureID = c.ID
WHERE c.shortname = 'fr-FR'
) b ON a.ID = b.ResourceKeyId
INNER JOIN
StringCategory sc ON
sc.ResourceKeyId = a.Id
INNER JOIN Category c ON c.Id = sc.CategoryId
WHERE c.Name = 'JavaScript';
SQLFiddle Demo
LEFT JOINS can become inner joins when you specify criteria on the outer joined table .
Try using a Left join to a subquery of culture containing your where clause instead of left joining to Culture.
LEFT JOIN (SELECT Id ,resourcevalue
FROM Culture
WHERE shortname = 'fr-FR') as CULTURE
ON
Strings.CultureId = Culture.Id AND Culture.Id
Full query
SELECT ResourceKey.Name AS Name, ResourceValue
FROM
ResourceKey LEFT OUTER JOIN Strings
ON
Strings.ResourceKeyId = ResourceKey.Id
INNER JOIN StringCategory
ON
StringCategory.ResourceKeyId = Strings.ResourceKeyId
INNER JOIN Category
ON
StringCategory.CategoryId = Category.Id
LEFT JOIN (SELECT Id ,resourcevalue
FROM Culture
WHERE shortname = 'fr-FR') as CULTURE
ON
Strings.CultureId = Culture.Id
WHERE Category.Name = 'JavaScript';

SQL Counting # of times a member shows up with certain code values

I am running a query that returns the location of a member and the product the member is enrolled in. Each time a member makes a claim with their product, they get a revenue code associated to them. Below is my query that I have now:
SELECT DISTINCT
e.State,
f.Product,
d.MemberID,
b.RevenueCode
FROM
Claims a
INNER JOIN
dw.Revenue b
ON
a.RevenueKey = b.RevenueKey
INNER JOIN
dw.Member d
ON
a.MemberKey = d.MemberKey
INNER JOIN
dw.Product f
ON
a.ProductKey = f.ProductKey
INNER JOIN
dw.State
ON
a.StateKey = f.StateKey
WHERE
b.RevenueCode IN ('0134', '0135')
It returns a set like the following:
State Product MemberID RevenueCode
MN xxx 945-234-245 0134
MN xxx 945-234-245 0135
SD xxx 231-345-235 0134
When a MemberID has both 0134 and 0135 RevenueCodes associated with it, they are considered to be in a special category. How would I modify my above query to count the number of times a MemberID has both RevenueCodes by State and by Product?
SELECT DISTINCT
e.State
,f.Product
,d.MemberID
,b.RevenueCode
,(SELECT 1
FROM Claims AS a1
INNER JOIN dw.Revenue AS b1 ON a1.RevenueKey = b1.RevenueKey
WHERE b1.RevenueCode IN ('0134', '0135')
AND b.revenuekey = b1.revenuekey
AND a.MemberKey = a1.Memberkey
HAVING COUNT(DISTINCT b1.RevenueCode) = 2) AS SpecialCategory
FROM Claims a
INNER JOIN dw.Revenue b ON a.RevenueKey = b.RevenueKey
INNER JOIN dw.Member d ON a.MemberKey = d.MemberKey
INNER JOIN dw.Product f ON a.ProductKey = f.ProductKey
INNER JOIN dw.State ON a.StateKey = f.StateKey
WHERE b.RevenueCode IN ('0134', '0135')
SELECT DISTINCT
e.State,
f.Product,
d.MemberID,
b.RevenueCode.
CASE WHEN EXISTS(
SELECT NULL
FROM Claims a1
JOIN dw.Revenue b1 ON a1.RevenueKey = b1.RevenueKey
JOIN dw.Member d1 ON a1.MemberKey = d1.MemberKey
JOIN dw.Product f1 ON a1.ProductKey = f1.ProductKey
WHERE b1.RevenueCode IN('0134', '0135') AND
d1.MemberID = d.MemberID AND
f1.ProductKey = f.ProductKey AND
f1.StateKey = f.StateKey
) THEN 1 ELSE 0 END As IsSpecialCategory
FROM
...
Figured it out...Simply needed to Count the Distinct RevenueCodes and Group By State, Product, and MemberID