Joining tables with multiple conditions - sql

I have two tables I would like to combine and set multiple conditions to pull out the desired data:
Table 1: [Folder1].[Name].[Info]
|---------------------|------------------|------------------|
| NameID | Gender | DoB |
|---------------------|------------------|------------------|
| 1 | M | 19800909 |
|---------------------|------------------|------------------|
| 2 | M | 19620102 |
|---------------------|------------------|------------------|
| 3 | F | 19501012 |
|---------------------|------------------|------------------|
| 4 | F | 19900212 |
|---------------------|------------------|------------------|
| 5 | M | 19501010 |
|---------------------|------------------|------------------|
And Table 2: [Folder1].[Body].[Sign]
|----------------|------------|------------|------------|
| NameID | BODYID | Category | Result |
|----------------|------------|------------|------------|
| 1 | 80000001 | Height | 178 |
|----------------|------------|------------|------------|
| 1 | 80000002 | Waist | 32 |
|----------------|------------|------------|------------|
| 1 | 80000003 | weight | 78 |
|----------------|------------|------------|------------|
| 1 | 80000003 | weight | 85 |
|----------------|------------|------------|------------|
| 2 | 80000001 | height | 170 |
|----------------|------------|------------|------------|
| 2 | 80000002 | waist | 30 |
|----------------|------------|------------|------------|
| 2 | 80000003 | weight | 78 |
|----------------|------------|------------|------------|
| 2 | 80000003 | weight | 80 |
|----------------|------------|------------|------------|
| 2 | 80000003 | weight | 76 |
|----------------|------------|------------|------------|
| 3 | 80000001 | height | 168 |
|----------------|------------|------------|------------|
| 4 | 80000001 | height | 170 |
|----------------|------------|------------|------------|
| 5 | 80000001 | height | 171 |
|----------------|------------|------------|------------|
I want to combine the 2 tables together with set conditions so that the combined graph would have Top 50 NameID, Gender, DoB, BodyID, Category, Result of people with DoB before 19900101, showing only Height and weight data, and showing only the people with 3 or more weight data.
The current SQL code I have right now is:
SELECT TOP 50 [Info].[NameID]
,[Gender]
,[DoB]
,[BodyID]
,[Category]
,[Result]
FROM [Folder1].[Name].[Info] LEFT JOIN [Folder1].[Body].[Sign]
ON [Info].[NameID] = [Sign].[NameID]
WHERE ([DoB] < '19900101')
AND ([Category] = 'Weight' OR [Category] = 'Height')
AND [Category] IN (SELECT Count(case when [BODYID] = 80000003 then 1 else null end) FROM [Folder1].[Body].[Sign] GROUP BY [Category] HAVING COUNT([BODYID]) >2)
ORDER BY [NameID]
The query can be successfully executed and a table shows up, but no information has appeared. I have a feeling that something is wrong with that 'count' section, but couldn't figure out what's wrong with it.
What I am hoping to get as a result is something like:
|------------|------------|------------|------------|--------|--------|
| NameID | Gender | DoB | BODYID |Category|Result |
|------------|------------|------------|------------|--------|--------|
| 2 | M | 19620102 | 80000001 |Height | 170 |
|------------|------------|------------|------------|--------|--------|
| 2 | M | 19620102 | 80000003 |Weight | 78 |
|------------|------------|------------|------------|--------|--------|
| 2 | M | 19620102 | 80000003 |Weight | 80 |
|------------|------------|------------|------------|--------|--------|
| 2 | M | 19620102 | 80000003 |Weight | 76 |
|------------|------------|------------|------------|--------|--------|
Thanks in advance.

When you left join a table and then you put condition for a column that exists within that table you are actually making an inner join by discarding all the rows from the output that don't satisfy this condition. Since this is a left join a joining condition may evaluate to false and yet you are enforcing another condition in WHERE clause thus that row is discarded because it also evaluates to false.
I'm not going to follow the logic inside your entire WHERE clause, but I've moved one condition to JOIN clause and added brackets with OR [Category] IS NULL to the "complicated" condition so that even if LEFT JOIN is not satisfied, and in that case [Category] would be NULL it is still returning rows in the output.
SELECT TOP 50 [Info].[NameID]
,[Gender]
,[DoB]
,[BodyID]
,[Category]
,[Result]
FROM [Folder1].[Name].[Info] LEFT JOIN [Folder1].[Body].[Sign]
ON [Info].[NameID] = [Sign].[NameID] AND [Sign].[Category] IN ('Weight', 'Height')
WHERE [DoB] < '19900101'
AND ( [Category] IN ( ... ) OR [Category] IS NULL )
ORDER BY [NameID]

I'm not sure that I follow the whole question, but this definitely doesn't look right:
AND [Category] IN (SELECT Count(case when [BODYID] = 80000003 then 1 else null end)
FROM [Folder1].[Body].[Sign]
GROUP BY [Category]
HAVING COUNT([BODYID]) > 3
)
I don't fully follow the logic, but I could imagine that you want:
AND [Category] IN (SELECT [Category]
FROM [Folder1].[Body].[Sign]
GROUP BY [Category]
HAVING COUNT([BODYID]) > 3
)
or perhaps:
AND [Category] IN (SELECT [Category]
FROM [Folder1].[Body].[Sign]
GROUP BY [Category]
HAVING SUM(case when [BODYID] = 80000003 then 1 else 0 end) > 3
)

It looks like the reason you might not be getting results is because you have a SELECT COUNT in your where clause for. The code below shows the area of trouble.
AND [Category] IN (SELECT Count(case when [BODYID] = 80000003 then 1 else null end)
FROM [Folder1].[Body].[Sign] GROUP BY [Category] HAVING COUNT([BODYID]) >3)
The problem is that you are comparing a category which appears to be a varchar with a count which will return an int value. So if the count of the subquery = 10 then it will not match any of your categories.
This is likely why you are seeing no results because you have no categories equal to the count of your sub query.

Related

Query is summing values multiple times

Hi my query below is summing multiple values based on #cropseasons in my table. Since i have 4 crop seasons it seems to be multiplying the values by 4 since i have crop season as 1, 2, 3 or 4. All i want is values for 1 crop season. Can anyone assist? I have crop season in both tables.
With Summary as (
Select B_NAME as Branch, LOC as Location
,SUM(payment) as Gallons
,SUM(case when printed = 1 THEN Fee ELSE NULL END) as FeeCollected
,SUM(case when printed = 0 THEN Fee ELSE NULL END) as FeeNotCollected
,SUM(case when printed = 1 THEN Payment ELSE NULL END) as GallonsIssued
,SUM(case when printed = 0 THEN Payment ELSE NULL END) as GallonsNotIssued
From SicbWeeklyDeliveriesFuelArchive F Inner Join FarmerGroups G ON G.BSI_CODE = F.BSI_CODE
Where F.CROP_SEASON = #cropseason
Group By B_NAME, LOC
)
SELECT Branch
,Location
,Gallons
,GallonsIssued
,GallonsNotIssued
,FeeCollected
,FeeNotCollected
,((GallonsIssued/Gallons) * 100) as pct_GallonsCollected
FROM Summary
Order by Location, Branch
SicbWeeklyDeliveriesFuelArchive
+-------+----------+-------------+-----+---------+------+-------------+---------+
| ID | BSI_CODE | B_NAME | LOC | PAYMENT | FEE | CROP_SEASON | PRINTED |
+-------+----------+-------------+-----+---------+------+-------------+---------+
| 18735 | 2176 | SAN NARCISO | CZ | 85 | 8.5 | 4 | 0 |
| 18738 | 2176 | SAN NARCISO | CZ | 65 | 6.5 | 4 | 0 |
| 18739 | 10494 | SAN NARCISO | CZ | 85 | 8.5 | 3 | 0 |
+-------+----------+-------------+-----+---------+------+-------------+---------+
FarmerGroups
+-------+----------+-------------+-------------+
| ID | BSI_CODE | CROP_SEASON | BRANCH |
+-------+----------+-------------+-------------+
| 10473 | 2176 | 4 | SAN NARCISO |
| 11478 | 2176 | 3 | SAN NARCISO |
| 12787 | 10494 | 4 | SAN ROMAN |
+-------+----------+-------------+-------------+
It seems your join criteria is incomplete. The tables share BSI_CODE and CROP_SEASON, so I guess you want:
FROM sicbweeklydeliveriesfuelarchive f
JOIN farmergroups g ON g.bsi_code = f.bsi_code AND g.crop_season = f.crop_season
WHERE f.crop_season = #cropseason
But that's just guessing. Only you know how the tables are really related, what their rows represent, what columns make a row unique and what result you are actually after. Why do you join farmergroups at all? It looks like you are not really using the table in your query.

Make a query making groups on the same result row

I have two tables. Like this.
select * from extrafieldvalues;
+----------------------------+
| id | value | type | idItem |
+----------------------------+
| 1 | 100 | 1 | 10 |
| 2 | 150 | 2 | 10 |
| 3 | 101 | 1 | 11 |
| 4 | 90 | 2 | 11 |
+----------------------------+
select * from items
+------------+
| id | name |
+------------+
| 10 | foo |
| 11 | bar |
+------------+
I need to make a query and get something like this:
+--------------------------------------+
| idItem | valtype1 | valtype2 | name |
+--------------------------------------+
| 10 | 100 | 150 | foo |
| 11 | 101 | 90 | bar |
+--------------------------------------+
The quantity of types of extra field values is variable, but every item ALWAYS uses every extra field.
If you have only two fields, then left join is an option for this:
select i.*, efv1.value as value_1, efv2.value as value_2
from items i left join
extrafieldvalues efv1
on efv1.iditem = i.id and
efv1.type = 1 left join
extrafieldvalues efv2
on efv1.iditem = i.id and
efv1.type = 2 ;
In terms of performance, two joins are probably faster than an aggregation -- and it makes it easier to bring in more columns from items. One the other hand, conditional aggregation generalizes more easily and the performance changes by little as more columns from extrafieldvalues are added to the select.
Use conditional aggregation
select iditem,
max(case when type=1 then value end) as valtype1,
max(case when type=2 then value end) as valtype2,name
from extrafieldvalues a inner join items b on a.iditem=b.id
group by iditem,name

Duplicate records upon joining table

I am still very new to SQL and Tableau however I am trying to work myself towards achieving a personal project of mine.
Table A; shows a table which contains the defect quantity per product category and when it was raised
+--------+-------------+--------------+-----------------+
| Issue# | Date_Raised | Category_ID# | Defect_Quantity |
+--------+-------------+--------------+-----------------+
| PCR12 | 11-Jan-2019 | Product#1 | 14 |
| PCR13 | 12-Jan-2019 | Product#1 | 54 |
| PCR14 | 5-Feb-2019 | Product#1 | 5 |
| PCR15 | 5-Feb-2019 | Product#2 | 7 |
| PCR16 | 20-Mar-2019 | Product#1 | 76 |
| PCR17 | 22-Mar-2019 | Product#2 | 5 |
| PCR18 | 25-Mar-2019 | Product#1 | 89 |
+--------+-------------+--------------+-----------------+
Table B; shows the consumption quantity of each product by month
+-------------+--------------+-------------------+
| Date_Raised | Category_ID# | Consumed_Quantity |
+-------------+--------------+-------------------+
| 5-Jan-2019 | Product#1 | 100 |
| 17-Jan-2019 | Product#1 | 200 |
| 5-Feb-2019 | Product#1 | 100 |
| 8-Feb-2019 | Product#2 | 50 |
| 10-Mar-2019 | Product#1 | 100 |
| 12-Mar-2019 | Product#2 | 50 |
+-------------+--------------+-------------------+
END RESULT
I would like to create a table/bar chart in tableau that shows that Defect_Quantity/Consumed_Quantity per month, per Category_ID#, so something like this below;
+----------+-----------+-----------+
| Month | Product#1 | Product#2 |
+----------+-----------+-----------+
| Jan-2019 | 23% | |
| Feb-2019 | 5% | 14% |
| Mar-2019 | 89% | 10% |
+----------+-----------+-----------+
WHAT I HAVE TRIED SO FAR
Unfortunately i have not really done anything, i am struggling to understand how do i get rid of the duplicates upon joining the tables based on Category_ID#.
Appreciate all the help I can receive here.
I can think of doing left joins on both product1 and 2.
select to_char(to_date(Date_Raised,'d-mon-yyyy'),'mon-yyyy')
, (p2.product1 - sum(case when category_id='Product#1' then Defect_Quantity else 0 end))/p2.product1 * 100
, (p2.product2 - sum(case when category_id='Product#2' then Defect_Quantity else 0 end))/p2.product2 * 100
from tableA t1
left join
(select to_char(to_date(Date_Raised,'d-mon-yyyy'),'mon-yyyy') Date_Raised
, sum(Comsumed_Quantity) as product1 tableB
where category_id = 'Product#1'
group by to_char(to_date(Date_Raised,'d-mon-yyyy'),'mon-yyyy')) p1
on p1.Date_Raised = t1.Date_Raised
left join
(select to_char(to_date(Date_Raised,'d-mon-yyyy'),'mon-yyyy') Date_Raised
, sum(Comsumed_Quantity) as product2 tableB
where category_id = 'Product#2'
group by to_char(to_date(Date_Raised,'d-mon-yyyy'),'mon-yyyy')) p2
on p2.Date_Raised = t1.Date_Raised
group by to_char(to_date(Date_Raised,'d-mon-yyyy'),'mon-yyyy')
By using ROW_NUMBER() OVER (PARTITION BY ORDER BY ) as RN, you can remove duplicate rows. As of your end result you should extract month from date and use pivot to achieve.
I would do this as:
select to_char(date_raised, 'YYYY-MM'),
(sum(case when product = 'Product#1' then defect_quantity end) /
sum(case when product = 'Product#1' then consumed_quantity end)
) as product1,
(sum(case when product = 'Product#2' then defect_quantity end) /
sum(case when product = 'Product#2' then consumed_quantity end)
) as product2
from ((select date_raised, product, defect_quantity, 0 as consumed_quantity
from a
) union all
(select date_raised, product, 0 as defect_quantity, consumed_quantity
from b
)
) ab
group by to_char(date_raised, 'YYYY-MM')
order by min(date_raised);
(I changed the date format because I much prefer YYYY-MM, but that is irrelevant to the logic.)
Why do I prefer this method? This will include all months where there is a row in either table. I don't have to worry that some months are inadvertently filtered out, because there are missing production or defects in one month.

How to insert records based on another table value

I have the following three tables:
Permission
| PermissionId | PermissionName |
+--------------+----------------+
| 1 | A |
| 2 | B |
| 3 | C |
| 100 | D |
Group
| GroupId | GroupLevel | GroupName |
+---------+------------+----------------------+
| 1 | 0 | System Administrator |
| 7 | 0 | Test Group 100 |
| 8 | 20 | Test Group 200 |
| 9 | 20 | test |
| 10 | 50 | TestGroup01 |
| 11 | 51 | TestUser02 |
| 12 | 52 | TestUser03 |
GroupPermission
| GroupPermissionId | FkGroupId | FkPermissionId |
+-------------------+-----------+----------------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 1 | 4 |
I need to insert records into GroupPermission table, if table Group, GroupLevel column have 0
then I need to take its GroupId and need to insert values to GroupPermission table as that particular id and 100.
In order to above sample table records, I need to insert the following two records to GroupPermission table,
| FkGroupId | FkPermissionId |
+-----------+----------------+
| 1 | 100 |
| 7 | 100 |
How can I do it
This question is not very clear and I can only assume the value 100 is a static value and that you don't actually have foreign keys as the names of the columns imply. Also, you really should avoid reserved words like "Group" for object names. It makes things more difficult and confusing.
The simple version of your insert might look like this.
insert GroupPermission
(
FkGroupId
, FkPermissionId
)
select g.GroupId
, 100
from [Group] g
where g.GroupLevel = 0
--EDIT--
Since you want to only insert those rows that don't already exist you can use NOT EXISTS like this.
select g.GroupId
, 100
from [Group] g
where g.GroupLevel = 0
AND NOT EXISTS
(
select *
from GroupPermission gp
where gp.FkGroupId = g.GroupId
and g.FkPermissionId = 100
)
Or you could use a left join like this.
select g.GroupId
, 100
from [Group] g
left join GroupPermission gp on gp.FkGroupId = g.GroupId
and gp.FkPermissionId = 100
where g.GroupLevel = 0
and gp.FkGroupId is null

SQL select with preference on column values

I am new to SQL and I would like to ask about how to select entries based on preferences and grouping.
+----------+----------+------+
| ENTRY_ID | ROUTE_ID | TYPE |
+----------+----------+------+
| 1 | 15 | 0 |
| 1 | 26 | 1 |
| 1 | 39 | 1 |
| 2 | 22 | 1 |
| 2 | 15 | 1 |
| 3 | 30 | 1 |
| 3 | 35 | 0 |
| 3 | 40 | 1 |
+----------+----------+------+
With the table above, I would like to select 1 entry for each ENTRY_ID with the following preference for the returned ROUTE_ID:
IF TYPE = 0 is available
for any one of the entries with the same ENTRY_ID, return the minimum ROUTE_ID for all entries with TYPE = 0
IF for the same ENTRY_ID only TYPE = 1 is available, return the minimum ROUTE_ID
The expected outcome for the query will be the following:
+----------+----------+------+
| ENTRY_ID | ROUTE_ID | TYPE |
+----------+----------+------+
| 1 | 15 | 0 |
| 2 | 15 | 1 |
| 3 | 35 | 0 |
+----------+----------+------+
Thank you for your help!
You can group by both TYPE and ENTRY_ID, and then use the HAVING clause to filter out those where TYPE is not the minimal value for that record.
SELECT ENTRY_ID, MIN(ROUTE_ID), TYPE
FROM MyTable
GROUP BY ENTRY_ID, TYPE
HAVING TYPE = (SELECT MIN(s.TYPE) FROM MyTable s WHERE s.ENTRY_ID = MyTable.ENTRY_ID)
This relies on type only being able to be 0 or 1. If there are more possible values, it will only return the lowest type.
If you want complete rows, use a correlated subquery:
select t.*
from t
where t.route_id = (select top 1 t2.route_id
from t as t2
where t2.entry_id = t.entry_id
order by iif(t2.type = 0, 1, 2), -- put type 0 first
t2.route_id asc -- then the first route_id
);
This has the advantage that it can return more than just the three columns you show in the question.