How to Exclude rows in one table by using conditions - sql

I want to exclude rows where linenumber is equal to linkedlineno and type should be ES and SE and two rows number should be equal . And where the linkedlineno NULL should appear in results.
| Name |Linenumber|Linkedlineno|Type | Number|
________ ________ ___________ ____ ________
|Apple| 2 3 ES 9999
|Apple| 3 2 SE 9999
|Apple| 5 NULL ES 9999
|Apple| 6 NULL ES 9999
|Apple| 7 NULL ES 9999
|Apple| 8 NULL ES 9999
Don't use only linked is null need other
When I tried excluding those two rows but where Type NULL rows are not appearing.
Select *
from H1
where Type in('ES','SE') and
linenumber in (select * from H1 linkedlineno) and
Number in (select * from H1 Number )

Related

SQL query to find result set with english and other translated words

2 tables are as Eng & Tr which contain following data-
Eng table:- Tr table:-
sn val1 sn val2 trns
-- --- -- --- ----
1 one 2 zwei de
2 two 2 dos es
3 three 3 drei de
4 four 4 cuatro es
5 five 4 quatro it
I want to get result of values having translated as German language (DE) along with which not translated (like 1, 5 as well). So I want output like below -
sn val1 val2
-- --- ----
1 one
2 two zwei
3 three drei
5 five
Use a left join:
SELECT e.sn, e.val1, t.val2
FROM Eng e
LEFT JOIN Tr t
ON t.sn = e.sn AND t.trns = 'de';
Note that from SQL's point of view, both one and four are really the same in that neither has a translation available in German. My answer therefore reports both of them.

Can repeated columns be combined in an Oracle result set that uses Group By clause?

I need to write a query in Oracle 11g that joins the following two tables, computes an average of the VAL1 column (based on the GROUP BY clause) and also returns a value for the VAL2 column, if present.
The query I came up with, however, contains duplicate rows for the same JOB_ID if there is a value present in VAL2.
Here are the tables:
Table: JOB_BUDGET
JOB_ID VAL1 VAL2 JB_DATE
------------------------------------------------
1 11 12 8/8/2016 <--val2 is always the same
1 12 12 8/8/2016 <--val2 is always the same
1 10 12 8/8/2016 <--val2 is always the same
3 1 NULL 8/8/2016
4 .5 NULL 8/8/2016
5 3 NULL 8/8/2016
Table: JOB_GROUP
GROUP_ID GROUP_NAME JOB_ID
-----------------------------------
10006 A1 1
10006 A1 2
10007 B 3
10005 C 4
10004 D 5
Here is the query:
SELECT
JG.JG_ID,
JG.JG_NAME,
round(avg(JB.VAL1), 2),
JB.VAL2
FROM
JOB_GROUP JG,
JOB_BUDGET JB
WHERE
JB.job_date = to_date('20160808', 'yyyyMMdd') AND
JB.job_id = JG.job_id
GROUP BY
jg.jg_id,
jg.jg_name,
jb.val2
Here are the results I'm getting:
JG_ID JG_NAME VAL1_AVG VAL2
10004 D 3 NULL
10006 A1 11 NULL
10006 A1 11 12
etc...
In the second and third row of the results, VAL1_AVG is duplicated.
Can the query be changed to combine those two rows into one row?
This is what I would like to see (multiple rows for a single jg_id/jg_name combo have been combined in one row):
JG_ID JG_NAME VAL1_AVG VAL2
10004 D 3 NULL
10006 A1 11 12
etc...

Selecting rows and filler (null data)

I have a table that looks like this:
ReportID | TeamID | Inning | Runs
1 A 1 3
1 A 2 3
1 A 5 7
1 B 1 3
1 B 3 2
1 B 6 1
I need to select all of that data, plus null data for the missing innings. It also need to stop at the max Inning for both teams (i.e. teamB's highest inning is 6, so I would collect 6 rows for both teamA and teamB yielding 12 total rows.)
For a visual, I need the output of the query to look like this:
ReportID | TeamID | Inning | Runs
1 A 1 3
1 A 2 3
1 A 3 NULL
1 A 4 NULL
1 A 5 7
1 A 6 NULL
1 B 1 3
1 B 2 NULL
1 B 3 2
1 B 4 NULL
1 B 5 NULL
1 B 6 1
Is there anyway to do this with just a query? Modifying the original table to add the null values is not an option.
Self join to generate the permutations of reports and teams
Left self join to generate hits which might be nullable.
This is probably a lot more efficient if it's done outside of SQL
SELECT ins.ReportID, teams.TeamID, ins.inning, score.Runs
FROM games as ins
JOIN games AS teams
ON ins.ReportID = teams.ReportID
LEFT JOIN games AS score
ON ins.ReportID = score.ReportID
AND teams.TeamID = score.TeamID
AND ins.inning = score.inning
GROUP BY ins.ReportID, teams.TeamID, ins.inning;

Grouping a row based on field in a different table in oracle

I am working with these two tables for the past two days
parts_list table:
PART_ID VENDOR_ID LABEL
1 5 A
1 2 B
1 3 C
2 2 D
2 3 E
3 3 F
vendor_prsdnc table:
VENDOR_ID PRSCDNC
5 3
2 2
3 1
Can anybody please tell me how to retrieve the label of each part from the vendor with highest precedence? For example the part with id one is supplied by 3 vendors but we need the one from vendor with highest precedence ie 5. The expected result is:
PART_ID VENDOR_ID LABEL
1 5 A
2 2 D
3 3 F
[Vendor Id is not proportional with the precedence ]
I have this query
SELECT
SDS.PART_ID,
SDSIS.VENDOR_ID,
MAX(SDSIS.PRSCDNC)
FROM PARTS_LIST SDS,VENDOR_PRSDNC SDSIS
WHERE SDS.VENDOR_ID=SDSIS.VENDOR_ID
GROUP BY SDS.PART_ID,SDSIS.VENDOR_ID;
but it does not return the expected result.
Not tested ,but it should work i think
select part_id,vendor_id,label
from
(
select pl.part_id
,pl.vendor_id
,pl.label
,vp.prscdnc
,max(vp.prscdnc) over (partition by pl.part_id) mx
from part_list pl,vendor_prsdnc vp
where pl.vendor_id=vp.vendor_id
)
where prscdnc =mx;

SQL Server : Lookup / Join on variable length string

I'm trying to write a query that will return the closest match from a table. The table represents a hierarchy, and looks like this:
Hier_Code Group1 Group2 Group3
_________ ______ ______ ______
A 1 1 5
AA 1 2 5
AAA 1 2 5
AAB 1 3 5
AB 2 3 5
ABA 2 3 5
ABB 2 3 5
The values I'm searching for may not match perfectly, in which case I'd want them to 'roll-up' to the next level to find the match. For example:
Value Returns:
A 1 1 5 (Perfect match)
AAC 1 2 5 (AA is closest)
AABB 1 3 5 (AAB is closest)
The idea is that you would strip off one character at a time from the search term until you find a match. I've tried doing this with a recursive CTE, and also with a WHILE loop to no avail.
Any suggestions?
Thanks in advance
Try this:
SELECT top 1 Group1, Group2, Group3
FROM temp
WHERE 'AABB' like Hier_Code + '%'
GROUP BY Group1, Group2, Group3
ORDER BY MAX(len(Hier_Code)) desc
Here's a starter that you could use to create some dynamic SQL:
select id from t
where g1 = 1 and g2 = 1 and g3 = 5
union
select id from t
where g1 = 1 and g2 = 1
union
select id from t
where g1 = 1
The default behavior of UNION is to eliminate duplicates, so that after these three selects are executed, no id would occur more than once in the result set.
The problem is that there are several permutations:
{g1, g2, g3}, {g1, g3}, {g1, g2}, {g2, g3}
so you cannot simply pluck the final condition off as I did in my example.