I have two tables:
TOOLS ( id | name | qt), which is a list of all equipment
TOOLSOUT ( idtools | qt ), which is a list of all the equipment that are beyond
I want to show all equipment with the quantities available.
If the TOOLS.id is not present in TOOLSOUT.idtools print qt normally. If the TOOLS.id is present in TOOLSOUT.idtools print (TOOLS.qt - TOOLS.qt)
How can I write this query?
Try this query. Seems that's what you're looking for.
SELECT
CASE
WHEN t2.idtools IS NULL
THEN t.qt
ELSE t.qt - t2.qt
END AS qt
FROM
TOOLS t
LEFT JOIN TOOLSOUT t2 ON t.id = t2.idtools
Related
I have a table named Ticket Numbers, which (for this example) contain the columns:
Ticket_Number
Assigned_Group
Assigned_Group_Sequence_No
Reported_Date
Each ticket number could contain 4 rows, depending on how many times the ticket changed assigned groups. Some of these rows could contain an assigned group of "Desktop Support," but some may not. Here is an example:
Example of raw data
What I am trying to accomplish is to get the an output that contains any ticket numbers that contain 'Desktop Support', but also the assigned group of the max sequence number. Here is what I am trying to accomplish with SQL:
Queried Data
I'm trying to use SQL with the following query but have no clue what I'm doing wrong:
select ih.incident_number,ih.assigned_group, incident_history2.maxseq, incident_history2.assigned_group
from incident_history_public as ih
left join
(
select max(assigned_group_seq_no) maxseq, incident_number, assigned_group
from incident_history_public
group by incident_number, assigned_group
) incident_history2
on ih.incident_number = incident_history2.incident_number
and ih.assigned_group_seq_no = incident_history2.maxseq
where ih.ASSIGNED_GROUP LIKE '%DS%'
Does anyone know what I am doing wrong?
You might want to create a proper alias for incident_history. e.g.
from incident_history as incident_history1
and
on incident_history1.ticket_number = incident_history2.ticket_number
and incident_history1.assigned_group_seq_no = incident_history2.maxseq
In my humble opinion a first error could be that I don't see any column named "incident_history2.assigned_group".
I would try to use common table expression, to get only ticket number that contains "Desktop_support":
WITH desktop as (
SELECT distinct Ticket_Number
FROM incident_history
WHERE Assigned_Group = "Desktop Support"
),
Than an Inner Join of the result with your inner table to get ticket number and maxSeq, so in a second moment you can get also the "MAXGroup":
WITH tmp AS (
SELECT i2.Ticket_Number, i2.maxseq
FROM desktop D inner join
(SELECT Ticket_number, max(assigned_group_seq_no) as maxseq
FROM incident_history
GROUP BY ticket_number) as i2
ON D.Ticket_Number = i2.Ticket_Number
)
SELECT i.Ticket_Number, i.Assigned_Group as MAX_Group, T.maxseq, i.Reported_Date
FROM tmp T inner join incident_history i
ON T.Ticket_Number = i.Ticket_Number and i.assigned_group_seq_no = T.maxseq
I think there are several different method to resolve this question, but I really hope it's helpful for you!
For more information about Common Table Expression: https://www.essentialsql.com/introduction-common-table-expressions-ctes/
I have to find matching records and non-matching records as well from two different table.
suppose I have two tables,
table 1: manufacturer_detail
column: Manufacturer_name
Manufacturer_name
ABB, INC
ABB PHARMA
FIBER INC
table 2: Contractor_detail
column:Contractor_name
Contractor_name
ABB, INC
OBS COMPANY
FIBER
Expected Result
Matching_Records
ABB, INC
so here I want to find if any Manufacturer_name is matching with Contractor_name or not? so Basically I want all Manufacturer_name which are matching with Contractor_name as a result and vise-versa like if I wanted see how many of my manufacturer are contractor as well then give me matching list otherwise give me non_matching list.
I am doing this exercise in Snowflake.
SELECT j.Manufacture_name,
CASE
WHEN j.Contractor_name IS NULL THEN 0
ELSE 1
END as "matching"
FROM (
SELECT m.Manufacture_name, c.Contractor_name
FROM manufacture_detail m
LEFT JOIN contractor_detail c
ON c.Contractor_name = m.Manufacturer_name) j;
-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-EDIT
-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-
This script's output will match the expected output you have added to your original question:
SELECT m.Manufacture_name
FROM manufacture_detail m
JOIN contractor_detail c
ON c.Contractor_name = m.Manufacturer_name;
A simple inner join will do the magic in this case.
"My Koryto" gave a good answer. Adding this one to match the desired output - plus testing data:
WITH manufacturer_detail AS (
SELECT value Manufacturer_name
FROM TABLE(FLATTEN(SPLIT('ABB, INC|ABB PHARMA|FIBER INC', '|')))
), Contractor_detail AS (
SELECT value Contractor_name
FROM TABLE(FLATTEN(SPLIT('ABB, INC|OBS COMPANY|FIBER', '|')))
)
SELECT Manufacturer_name AS Matching_Records
FROM manufacturer_detail a
JOIN contractor_detail b
ON a.Manufacturer_name=b.Contractor_name;
(the solution involves just a basic SQL JOIN)
I'm developing a page that allow to search a product by reference or by bar code or by alternative bar codes in certain order list.
I need to get the reference and qtt columns from select.
To do that we have 3 tables:
bi (list all products inside a specific order)
sc (give us standard qtt by product reference)
bc (gives us alternative bar codes, this table has also qtt column)
The problem is that it's possible that a product doesn't have alternative bar codes and in this case the bc table returns null and in this case I have to get the qtt in table sc but I don't know how to do that in same query.
My query is that:
select top 1 bi.ref, bc.qtt
from bi left join
bc
on bc.ref = bi.ref
where (bi.ref='00012' or bi.code='00012' or bc.code='00012') and
bi.bostamp = ('orderID-0001')
The column bi.bostamp is the reference with order id.
So, I need to try integrate sc table in query to get qtt just when bc is null.
Thank you
I think you can use Case When to get the results you want.
Select Top 1 bi.ref, case when isnull(bc.qtt) then sc.qtt else bc.qtt end as qtt
FROM bi left join bc on bc.ref = bi.ref
left join sc on sc.ref = bi.ref
Does that work for you? This also assumes sc has a ref column like bc and bi.
Hi I have a redshift table of articles that has a field on it that can contain many accounts. So there is a one to many relationship between articles to accounts.
However I want to create a new view where it lists the partner id's in one column and in another column a count of how many times the partner id appears in the articles table.
I've attempted to do this using regex and created a new redshift view, but am getting weird results where it doesn't always build properly. So one day it will say a partner appears 15 times, then the next 17, then the next 15, when the partner id count hasn't actually changed.
Any help would be greatly appreciated.
SELECT partner_id,
COUNT(DISTINCT id)
FROM (SELECT id,
partner_ids,
SPLIT_PART(partner_ids,',',i) partner_id
FROM positron_articles a
LEFT JOIN util.seq_0_to_500 s
ON s.i < regexp_count (partner_ids,',') + 2
OR s.i = 1
WHERE i > 0
AND regexp_count (partner_ids,',') = 0
ORDER BY id)
GROUP BY 1;
Let's start with some of the more obvious things and see if we can start to glean other information.
Next GROUP BY 1 on your outer query needs to be GROUP BY partner_id.
Next you don't need an order by in your INNER query and the database engine will probably do a better job optimizing performance without it so remove ORDER BY id.
If you want your final results to be ordered then add an ORDER BY partner_id or similar clause after your group by of your OUTER query.
It looks like there are also problems with how you are splitting a partnerid from partnerids but I am not positive about that because I need to understand your view and the data it provides to know how that affects your record count for partnerid.
Next your LEFT JOIN statement on the util.seq_0_to_500 I am pretty sure you can drop off the s.i = 1 as the first condition will satisfy that as well because 2 is greater than 1. However your left join really acts more like an inner join because you then exclude any non matches from positron_articles that don't have a s.i > 0.
Oddly then your entire join and inner query gets kind of discarded because you only want articles that have no commas in their partnerids: regexp_count (partner_ids,',') = 0
I would suggest posting the code for your util.seq_0_to_500 and if you have a partner table let use know about that as well because you can probably get your answer a lot easier with that additional table depending on how regexp_count works. I suspect regex_count(partnerids,partnerid) exampleregex_count('12345,678',1234) will return greater than 0 at which point you have no choice but to split the delimited strings into another table before counting or building a new matching function.
If regex_count only matches exact between commas and you have a partner table your query could be as easy as this:
SELECT
p.partner_id
,COUNT(a.id) AS ArticlesAppearedIn
FROM
positron_articles a
LEFT JOIN PARTNERTABLE p
ON regexp_count(a.partnerids,p.partnerid) > 0
GROUP BY
p.partner_id
I will actually correct myself as I just thought of a way to join a partner table without regexp_count. So if you have a partner table this might work for you. If not you will need to split strings. It basically tests to see if the partnerid is the entire partnerids, at the beginning, in the middle, or at the end of partnerids. If one of those is met then the records is returned.
SELECT
p.partner_id
,COUNT(a.id) AS ArticlesAppearedIn
FROM
PARTNERTABLE p
INNER JOIN positron_articles a
ON
(
CASE
WHEN a.partnerids = CAST(p.partnerid AS VARCHAR(100)) THEN 1
WHEN a.partnerids LIKE p.partnerid + ',%' THEN 1
WHEN a.partnerids LIKE '%,' + p.partnerid + ',%' THEN 1
WHEN a.partnerids LIKE '%,' + p.partnerid THEN 1
ELSE 0
END
) = 1
GROUP BY
p.partner_id
I am trying to write a query for an inventory system. In order to do this I have to count the number of duplicates in one table and then compare it to the default quantity values taken from another table.
Here are two queries which I am working with currently:
SELECT Template_ID,
COUNT(Template_ID) AS Howmuch, t.name
FROM consumables e, templates t
Where t.consumable_type_id = '2410980'
GROUP BY template_id, t.name
HAVING ( COUNT(Template_ID) > 1 )
The query above takes account of each unique Template Id and gives me a count of how many duplicates are present which tells me the amount of a single substance.
Select
property_descriptors.default_value,
templates.name
From
templates,
Property_descriptors
Where
templates.consumable_type_id = '858190' And
templates.audit_id = property_descriptors.audit_id And
property_descriptors.name = 'Reorder Point'
This query finds the amount of each individual substance we would like to have in our system.
My Issue is that I dont know of a way to compare the results from the 2 queries.
Ideally, I want the query to only give the substance which have a duplicate count lower than their default value (found using query 2).
any ideas would be appreciated!
here is the table schema for reference:
Consumables
ID|Template_ID|
Templates
ID|Property_Descriptor_ID|Name|audit_id
Property_Descriptors
ID| Name|Default_Value|audit_id
Thanks!
SELECT q1.name, q2.default_value - q1.Howmuch FROM
(SELECT Template_ID, COUNT(Template_ID) AS Howmuch, t.name
FROM consumables e, templates t
Where t.consumable_type_id = '2410980'
GROUP BY template_id, t.name
HAVING ( COUNT(Template_ID) > 1 )) q1,
(SELECT property_descriptors.default_value default_value,
templates.name name
FROM
templates,
Property_descriptors
WHERE
templates.consumable_type_id = '858190' And
templates.audit_id = property_descriptors.audit_id And
property_descriptors.name = 'Reorder Point') q2
where q1.name = q2.name
should do the trick you'll need to clean up the result a bit to work away negative results
or add q2.default_value - q1.Howmuch > 0 in the outer WHERE clause