I have 2 table and i tried to get table 1 which contains Job_ID which has not finish the progress in table 2.
Table 1. [Job]
Job_ID (key) |GroupID
1410 | A
2309 | B
3456 | C
Table 2. [Progress]
Job_ID |Percent
1410 | 10
1410 | 50
1410 | 60
2309 | 50
2309 | 100
3456 | 100
Expected:
Job_ID (key) |GroupID
1410 | A
My SQL Query is:
SELECT FROM Job,(SELECT DISTINCT * FROM Progress
WHERE Percent<100) AS PGR
WHERE Job.Job_ID = PGR.Job_ID
But i still get this
Job_ID (key) |GroupID
1410 | A
2309 | B
My logic was getting only Job_ID which has Percent < 100 but i failed to find specific way in SQL in stackoverflow. Please help me in this case.
You can take help from below Query.
SELECT *
FROM table1 a
WHERE NOT EXISTS (SELECT 1 FROM table2 b WHERE a.job_id = b.job_id and percentage = 100)
Use LEFT JOIN and NULL check?
SELECT T1.JobID, T2.GroupID
FROM Table1 T1
LEFT JOIN T2 ON T1.JobID = T2.JobId
AND T2.Percent = 100
WHERE T2.JobID IS NULL;
use not exists
select
a.* from job a
where not exists (select 1 from progres b
where a.job_id =b.job_id and b.Percent=100
)
Try this
select
job.*
from
job
where
not exists (select 1 from progres where progres.job_id =job.job_id)
Related
This is my code! I want to give a part id and purchase order id to my report and it brings all the related information with those specification. The important thing is that, if we have same purchase order id and part id we need the code to return the result with the highest transaction id. The following code is not providing what I expected. Could you please help me?
SELECT MAX(INVENTORY_TRANS.TRANSACTION_ID), INVENTORY_TRANS.PART_ID
, INVENTORY_TRANS.PURC_ORDER_ID, TRACE_INV_TRANS.QTY, TRACE_INV_TRANS.CREATE_DATE, TRACE_INV_TRANS.TRACE_ID
FROM INVENTORY_TRANS
JOIN TRACE_INV_TRANS ON INVENTORY_TRANS.TRANSACTION_ID = TRACE_INV_TRANS.TRANSACTION_ID
WHERE INVENTORY_TRANS.PART_ID = #PartID
AND INVENTORY_TRANS.PURC_ORDER_ID = #PurchaseOrderID
GROUP BY TRACE_INV_TRANS.QTY, TRACE_INV_TRANS.CREATE_DATE, TRACE_INV_TRANS.TRACE_ID, INVENTORY_TRANS.PART_ID
, INVENTORY_TRANS.PURC_ORDER_ID
The sample of trace_inventory_trans table is :
part_id trace_id transaction id qty create_date
x 1 10
x 2 11
x 3 12
the sample of inventory_trans table is :
transaction_id part_id purc_order_id
11 x p20
12 x p20
I wanted to have the result of biggest transaction which is transaction 12 but it shows me transaction 11
I would use a sub-query to find the MAX value, then join that result to the other table.
The ORDER BY + TOP (1) returns the MAX value for transaction_id.
SELECT
inv.transaction_id
,inv.part_id
,inv.purc_order_id
,tr.qty
,tr.create_date
,tr.trace_id
FROM
(
SELECT TOP (1)
transaction_id,
part_id,
purc_order_id
FROM
INVENTORY_TRANS
WHERE
part_id = #PartID
AND
purc_order_id = #PurchaseOrderID
ORDER BY
transaction_id DESC
) AS inv
JOIN
TRACE_INV_TRANS AS tr
ON inv.transaction_id = tr.transaction_id;
Results:
+----------------+---------+---------------+------+-------------+----------+
| transaction_id | part_id | purc_order_id | qty | create_date | trace_id |
+----------------+---------+---------------+------+-------------+----------+
| 12 | x | p20 | NULL | NULL | 3 |
+----------------+---------+---------------+------+-------------+----------+
Rextester Demo
Trying to update one column, from another table with the highest Date.
Table 1 Example:
PartNumber | Cost
1000 | .10
1001 | .20
Table 2 Example:
PartNumber | Cost | Date
1000 | .10 | 2017-01-01
1000 | .50 | 2017-02-01
1001 | .20 | 2017-01-01
1002 | .50 | 2017-02-02
I would like to update table 1 with the most recent values from table2, which would be .50 for each... The query I use to update this has worked just fine until I realized I was not grabbing the correct Cost because there were multiples.. I now want to grab the highest dated revision.
My query:
UPDATE dex_mfgx..insp_master
SET dex_mfgx..insp_master.costperpart = t2.sct_cst_tot
FROM dex_mfgx..insp_master AS t1
INNER JOIN qad_repl..sct_det_sql AS t2
ON t1.partnum = t2.sct_part
WHERE t1.partnum = t2.sct_part and t2.sct_cst_date = MAX(t2.sct_cst_date) ;
My Error:
Msg 147, Level 15, State 1, Line 6
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
Not having much luck with HAVING or GROUPING, although I havent used them much..
Any have an idea that would help?
I think I understand what you are trying to solve now. Thanks to Lamak for setting me straight as I was way off base originally.
Something like this I think is what you are looking for.
with TotalCosts as
(
SELECT t2.sct_cst_tot
, t1.partnum
, RowNum = ROW_NUMBER() over(partition by t1.partnun order by t2.sct_cst_date desc)
FROM dex_mfgx..insp_master AS t1
INNER JOIN qad_repl..sct_det_sql AS t2 ON t1.partnum = t2.sct_part
)
update t1
set costperpart = tc.sct_cst_tot
from dex_mfgx..insp_master AS t1
join TotalCosts tc on tc.partnum = t1.partnum
where tc.RowNum = 1
What I need to do is the following:
I have in my database a table like this:
idx | name | age
------ ---------- -------
1 | John | 18
2 | Marry | 19
3 | Eric | 17
Then I get a secondTable:
name | age
------ -----
Moses | 29
John | 18
Eric | 20
I would like to run an except query like:
select *
from firstTable
where (name, age) not in (select * from secondTable)
and an intersect query like this:
select *
from firstTable
where (name, age) in (select * from secondTable)
So the result for the first query will be:
2 | Marry | 19
---- -------- ----
3 | Eric | 17
and the result for the second query will be:
1 | John | 18
I've also found a solution that recommends on the following:
select *
from firstTable
where EXISTS (select 1
from secondTable
where firstTable.name = secondTable.name
and firstTable.age = secondTable.age))
but then if I have on both tables "john - null" it will treat them as unknown (neither equal nor un-equal). I know the reason for that, but I do need them to be equal.
The reason I need to do this is in order to preserve the current index values to the query's result.
You just need to include handling the NULL values into your query logic. It would be like so:
SELECT *
FROM firstTable
WHERE EXISTS (SELECT TOP(1) 1
FROM secondTable
WHERE firstTable.name = secondTable.name
AND (
firstTable.age = secondTable.age
OR
(firstTable.age IS NULL AND secondTable.age IS NULL)
)
);
Should work like a charm. =)
Try this:
select distinct a.idx,a.name,a.age,b.name,b.age from first_table as a
inner join
second_table as b
on a.name = b.name and a.age = b.age
This one display only the record with same value both first_table and second_table
And this query Display not in second_table and union both table if has:
select distinct a.idx,b.name,b.age from first_table as a
inner join
second_table as b
on a.name = b.name and a.age = b.age
union all
select a.idx,a.name,a.age
from first_table as a where a.name not in(select name from second_table)
I have to just insert the value from one table into another but the condition is that out of same id I have to select that one having maximum date and then insert into another. like :
table 1
a | b
1 | 12/1/13
1 | 18/1/13
2 | 2/4/13
2 | 9/8/13
table 2
a | b
1 | 18/1/13
2 | 9/8/13
please suggest the SQL query for it
Could you try :
INSERT INTO Table2 (idcolumn, datecolumn)
SELECT DISTINCT idcolumn, datecolumn
FROM Table1
GROUP BY idcolumn
ORDER BY datecolumn DESC
INSERT INTO table2(a,b)
SELECT a, MAX(b) AS b
FROM table1
GROUP BY a;
I have two tables.
MainTable:
MainID | LastValue | LastReadingDate
1 | 234 | 01.01.2012
2 | 534 | 03.02.2012
Readings:
MainID | ValueRead | ReadingDate
1 | 123 | 03.02.2012
1 | 488 | 04.03.2012
2 | 324 | 03.02.2012
2 | 683 | 05.04.2012
I want to get
SELECT MainTable.MainID, MainTable.LastValue, MainTable.LastReadingDate, (SELECT ValueRead, MAX(ReadingDate)
FROM Readings
WHERE Readings.MainID=MainTable.MainID ORDER BY ValueRead)
In other words, I want to get the current LastValue and LastReadingDate from MainTable along side the ValueRead with the most recent ReadingDate from Readings.
Here is a query you could use. It'll show all MainTable entries, including those that doesn't have a "Reading" entry yet. Change the LEFT JOIN to an INNER JOIN if you don't want it like that.
WITH LastReads AS (
SELECT ROW_NUMBER() OVER (PARTITION BY MainID ORDER BY ReadingDate DESC) AS ReadingNumber,
MainID,
ValueRead,
ReadingDate
FROM Readings
)
SELECT M.MainID, M.LastValue, M.LastReadingDate, R.ValueRead, R.ReadingDate
FROM MainTable M
LEFT OUTER JOIN LastReads R
ON M.MainID = R.MainID
AND R.ReadingNumber = 1 -- Last reading, use 2 or 3 to get the 2nd newest, 3rd newest, etc.
SQLFiddle-link: http://sqlfiddle.com/#!3/16c68/3
Another link with N number of readings per mainid: http://sqlfiddle.com/#!3/16c68/4
Not tried this myself, but here goes. Please try
select max(r.readingdate), max(t.lastvalue), max(t.lastreadingdate)
from readings r inner join
( select MainID, LastValue, LastReadingDate
from MainTable m
where LastReadingDate =
(select max(minner.LastReadingDate)
from MainTable minner
where minner.MainID = m.MainID
)
) t
on (r.mainid = t.mainid)
try this:
select M.LastValue, M.LastReadingDate,
(select top 1 ValueRead from Readings where MainID=M.MainID order by ReadingDate desc)
from MainTable M