MSSQL - howto to get full list from interval of IDs - sql

I have two tables
Created_Labels:
IF label_ID_from = label_ID_to (it means it has been only one label created), in column label_number is number of created label.
IF label_ID_from <> label_ID_to (more labels was created), in column label_number is NULL and in next two columns is interval of created labels with ID from table bellow.
Labels (list of existing lables):
How can I get the complete list of created label_numbers (get labels with ID 105, 110, 111, 112..120, 200, 201, 202..210, 394, 554)?

SELECT
L.ID
, L.label_number
FROM
Labels L
JOIN
Created_Labels CL
ON
L.ID BETWEEN CL.label_ID_from
AND CL.label_ID_to

Did you try this?
SELECT distinct label_number
FROM created_labeles c;
OR
SELEC distinct l.label_number
FROM created_labeles c,
labels l
WHERE c.label_number = l.label_numbers(+)
AND c.label_number is null
The above second query is the left outer join equivalent in Oracle SQl.

Related

Select only the last date when two columns are duplicate

I need to select seven columns from three different tables, only when one of the columns has a particular value. I also need to select only the last date when two columns (TAGNAME and TAGNUMMER) are both duplicate. I'm using the following code:
select c.AKEY, c.AKT_DATUM, c.TAGNAME, c.TAGNUMMER,
cd.TEILANLAGEN_ID, x.TP_GSAP_KZ, c.KLASSEN_ID
from T0EM01 c, T0EM03 x, T0AD07 cd
where cd.TEILANLAGEN_ID = '219A'
inner join
(select c.TAGNAME and c.TAGNUMMER max(C.AKT_DATUM)
where T0EM01 c c.TAGNAME and T0EM01 c c.TAGNUMMER = m.max_date
Up to where cd.TEIANLAGEN_ID = '219A' it works fine (but there are over 2 million rows).
How can I filter so that when both TAGNAME and TAGNUMMER are repeated in two or more rows I only select the latest date?
"Over 2 million rows" could be less if you properly joined those 3 tables. The way you put it, you're producing Cartesian join and got way too many rows.
from t0em01 c,
t0em03 x,
t0ad07 cd
I have no idea how are they to be joined to each other so I'm just guessing; you should know.
As of the "max date value", one option might be to use a subquery, also properly joined to other table(s). Once again, I don't know how exactly to join them.
Improve it:
select c.akey,
c.akt_datum,
c.tagname,
c.tagnummer,
cd.teilanlagen_id,
x.tp_gsap_kz,
c.klassen_id
from t0em01 c join t0em03 x on x.id = c.id --> I'm just
join t0ad07 cd on cd.id = c.id -- guessing here
where cd.teilanlagen_id = '219A'
and c.akt_datum = (select max(c1.akt_datum) --> subquery, to return
from t0em01 c1 -- only the MAX date value
where c1.tagname = c.tagname
and c1.tagnummer = c.tagnummer
);

PostgreSQL - left join generate_series() and table

I use generate series to create a series of numbers from 0 to 200.
I have a table that contains dirtareas in mm² in a column called polutionmm2. What I need is to left join this table to the generated series, but the dirt area must be in cm² so /100. I was not able to make this work, as I can't figure out how I can connect a table to a series that has no name.
This Is what I have so far:
select generate_series(0,200,1) as x, cast(p.polutionmm2/100 as char(8)) as metric
from x
left join polutiondistributionstatistic as p on metric = x
error: relation X does not exist
Here is some sample data: https://dbfiddle.uk/?rdbms=postgres_13&fiddle=3d7d851887adb938819d6cf3e5849719
what I would need, is the first column (x) counting all the way from 0 to 200, and where there is a matching value, to show it in the second column.
Like this:
x, metric
0, 0
1, 1
2, 2
3, null
4, 4
5, null
... , ...
... , ...
200, null
You can put generate_series() in the FROM. So, I think you want something like this:
select gs.x, cast(p.polutionmm2/100 as char(8)) as metric
from generate_series(0,200,1) gs(x) left join
p
on gs.x = (p.polutionmm2/100);
I imagine there is also more to your query, because this doesn't do much that is useful.

How to query for all of one item with duplicates of another item

I have a table that reports all labels placed in a set of drawings. Each label has a file name and drawing number. I want to filter this table for all entries of a certain file name and duplicates of the drawing number. This will tell me which drawings have multiples of the same label. For example, the picture in the link below shows that I have three of Label 1 in DWG#1 and two of label #1 in DWG#2. This tells me that I need to clean up the duplicates in these two drawings. I need a statement to run in SQL Server to report these entries. Thanks!
You can try to use the count(*) function to determine how many duplicates are there in the table.
select count(), colomn_filename, colomn_drawingnumber
from table_name
group by colomn_filename, colomn_drawingnumber
having count()>1
SELECT fileName, drawingNo, count(*) FROM TableName
GROUP BY fileName, drawingNo
Result:
Label 1 DWG#1 3
Label 1 DWG#2 2
If you really need the result same as yours then use left join
SELECT a.fileName, a.drawingNo FROM TableName a
LEFT JOIN (
SELECT fileName, drawingNo FROM TableName
GROUP BY fileName, drawingNo
HAVING COUNT(*) > 1
) b ON a.fileName = b.fileName AND a.drawingNo = b.drawingNo

Select latest month record from group of records for several groups

I have a table that generates a record each month for each project status report (PSR) if a PSR is/was generated. From a single table, I need to get the most recent record for each PSR.
This gives me the list of PSR's I am looking for:
SELECT Max(Main.PSRMonth) AS MaxOfPSRMonth, Main.PE, Main.Loc, Main.EWO
FROM Main
GROUP BY Main.PE, Main.Loc, Main.EWO
ORDER BY Main.PE, Main.Loc, Main.EWO;
Table:Main
Primary Key is PSRMonth + PE + EWO + LOC
Now, I need all fields FROM Main based on the selection above.
I would like to do this with one SQL statement.
I need all fields FROM Main based on the selection above.
Then try this:
SELECT m1.*
FROM Main m1
INNER JOIN
(
SELECT
Max(PSRMonth) AS MaxOfPSRMonth,
PE,
Loc,
EWO
FROM Main
GROUP BY PE, Loc, EWO
) m2 ON m1.PE = m2.PE AND m1.Loc = m2.Loc AND m1.EWO = m2.EWO
AND m1.PSRMonth = m2.MaxOfPSRMonth
ORDER BY m1.PE, M1.Loc, M1.EWO;

outer query to list only if its rowcount equates to inner subquery

Need help on a query using sql server 2005
I am having two tables
code
chargecode
chargeid
orgid
entry
chargeid
itemNo
rate
I need to list all the chargeids in entry table if it contains multiple entries having different chargeids
which got listed in code table having the same charge code.
data :
code
100,1,100
100,2,100
100,3,100
101,11,100
101,12,100
entry
1,x1,1
1,x2,2
2,x3,2
11,x4,1
11,x5,1
using the above data , it query should list chargeids 1 and 2 and not 11.
I got the way to know how many rows in entry satisfies the criteria, but m failing to get the chargeids
select count (distinct chargeId)
from entry where chargeid in (select chargeid from code where chargecode = (SELECT A.chargecode
from code as A join code as B
ON A.chargecode = B.chargeCode and A.chargetype = B.chargetype and A.orgId = B.orgId AND A.CHARGEID = b.CHARGEid
group by A.chargecode,A.orgid
having count(A.chargecode) > 1)
)
First off: I apologise for my completely inaccurate original answer.
The solution to your problem is a self-join. Self-joins are used when you want to select more than one row from the same table. In our case we want to select two charge IDs that have the same charge code:
SELECT DISTINCT c1.chargeid, c2.chargeid FROM code c1
JOIN code c2 ON c1.chargeid != c2.chargeid AND c1.chargecode = c2.chargecode
JOIN entry e1 ON e1.chargeid = c1.chargeid
JOIN entry e2 ON e2.chargeid = c2.chargeid
WHERE c1.chargeid < c2.chargeid
Explanation of this:
First we pick any two charge IDs from 'code'. The DISTINCT avoids duplicates. We make sure they're two different IDs and that they map to the same chargecode.
Then we join on 'entry' (twice) to make sure they both appear in the entry table.
This approach gives (for your example) the pairs (1,2) and (2,1). So we also insist on an ordering; this cuts to result set down to just (1,2), as you described.