How to remove repeated rows in this Access query? - c#

How can I get rid of duplicated rows in my MS access query?
SELECT pt.code,
resis.resis,
sense.sense
FROM (sense INNER JOIN pt ON sense.code = pt.code)
INNER JOIN resis ON pt.code = resis.code;

Related

update query with multiple inner joins

I want to update the table with multiple inner join query but when I write the query it through an error incorrect syntax near inner keyword
Update inventory_detail INNER JOIN inventory
ON inventory_detail.inventory_id = Inventory.Inventory_id
INNER JOIN Ingredients
ON Inventory.Inventory_id=Ingredients.invenotry_id
SET inventory_detail.Quantity=inventory_detail.Quantity-1
WHERE inventory_detail.loc_id =1 AND Ingredients.item_id=27 ;
ERD diagram
Change your query to:
UPDATE inventory_detail
SET Quantity = Quantity - 1
FROM inventory_detail
INNER JOIN inventory
ON inventory_detail.inventory_id = Inventory.Inventory_id
INNER JOIN Ingredients
ON Inventory.Inventory_id = Ingredients.invenotry_id
WHERE inventory_detail.loc_id = 1
AND Ingredients.item_id = 27;

How to join tables and queries in C# with access database

this is how i join 2 tables and select form it:
OleDbDataAdapter DataA = new OleDbDataAdapter(#"Select tfr.FeedID, tf.FeedName, tfr.FeedQuantity, tf.DM
FROM tFeeds AS tf
INNER JOIN tFeedsRations AS tfr ON (tf.FeedID=tfr.FeedID)", Connection);
but what about adding a access query to this select command?
for example I want to add this statement to my select command:
Select qfq.FeedDMQuantites
From qFeeds_Quantities as qfq
what should I do?
Well add another JOIN condition to this table qFeeds_Quantities (assuming you have a relationship to this table or a common column among other table).
Assuming you have a common column like FeedID in this new table as well you can make another JOIN like
select tfr.FeedID, tf.FeedName, tfr.FeedQuantity,
tf.DM, qfq.FeedDMQuantites
FROM (tFeeds AS tf
INNER JOIN tFeedsRations AS tfr ON tf.FeedID = tfr.FeedID)
INNER JOIN qFeeds_Quantities as qfq ON tf.FeedID = qfq.FeedID;
If you want to include another JOIN then parenthesize like
FROM ((tFeeds AS tf
INNER JOIN tFeedsRations AS tfr ON tf.FeedID = tfr.FeedID)
INNER JOIN qFeeds_Quantities as qfq ON tf.FeedID = qfq.FeedID)
INNER JOIN BLAH AS bll ON bll.test = tf.test;

Two inner joins in one SQL query string

I'm currently trying to get information from three tables. I have a 'worker' table which includes a store ID (store_idStore) and a job ID (job_idJob) as foreign keys. In the Store and Job tables, each of them has a 'Name' field - this is the data I want to return in my query along with all of the information from Worker. My only caveat is that I have to put the query into a string in C#.
So far I'm able to join and get the information from Worker and Store:
SELECT worker.*, store.Name AS 'Store'
from worker inner join store on worker.Store_idStore = store.idStore
How do I expand this to get the relative Job's name as well?
Try this One :
SELECT worker.*,store.Name AS store,job.Name AS job from worker inner join store on worker.Store_idStore = store.idStore and worker inner join job on worker.job_idJob = job.idJob
Just append another inner join on the end
SELECT w.*,s.Name AS 'Store', j.name as 'job' from worker w
inner join store s on worker.Store_idStore = store.idStore
Inner join job J on worker.job_idjob = job.idjob
Try this:
SELECT DISTINCT w.*, s.*, j.*
FROM worker AS w
LEFT JOIN store AS s ON w.store_idStore = s.id
LEFT JOIN job AS j ON w.job_idJob = j.id
Of course you can replace * with any columns' names you want. And you can add Where clause at the end.

MS Access Select Multiple Joins

I'm trying to select by c# and odbc from the following tables.
LinkTab (FromDevID, FromPort, ToDevID, ToPort)
DevList (ID,DevName...)
The result should look like
FromDevName | FromPort | ToDevName | ToDevPort
i tried already the following statement:
SELECT dev1.DevName, lt.FromPort, dev2.DevName, lt.ToPort
FROM (LinkTab lt
INNER JOIN DevList dev1 ON lt.FromDevID = dev1.ID)
INNER JOIN devList dev2 ON lt.ToDevID = dev2.ID
and I couldn't get all records. I guess there is a mistake at my join condition.
Presumably, you need left join:
SELECT dev1.DevName, lt.FromPort, dev2.DevName, lt.ToPort
FROM (LinkTab lt LEFT JOIN
DevList dev1
ON lt.FromDevID = dev1.ID
) LEFT JOIN
devList dev2
ON lt.ToDevID = dev2.ID;
The reason your query would not return all the rows is because the devises may not always match. If this is the case, then INNER JOIN will filter out the rows with unmatched devices. A LEFT JOIN will keep all the rows in the first table, assigning NULL for the columns from the second and third tables.

Reduce Execution Time in MYSQL from ASP.NET

i have a table which is relevant GPS application in mysql. whenever i am executing from asp.net application it showing fetal error. so i have decided to run in mysql, it will take more than 1 min(exactly 70 sec) for executing the SP. is it possible to sort out the issues.
Further information:
table 1 : server_gpsdata(it contains gps data. it locks every 10 secs from gps device).
select * from server_gpsdata SD
INNER JOIN mstcab MC on MC.cabid= SD.cabid
INNER JOIN server_tblstatus TS on TS.statusid= MC.CabStatusid
INNER JOIN carmaster CM on CM.carid= MC.carid
INNER JOIN cabtype CT on CT.cabtypeid= CM.sizeid
where date(SD.cur_datetime) =current_date and
MC.Cabid not in (select D.cabid from trncabdriver D
where date(D.logintime)=current_date) and
SD.gpsdataid in (select max(SGD.gpsdataid) from server_gpsdata SGD
where date(SGD.cur_datetime)=current_date
group by SGD.cabid);
The following should show a significant improvement in performance:
select SD.*, MC.*, TS.*, CM.*, CT.* from
(select max(SGD.gpsdataid) maxgpsdataid from server_gpsdata SGD
where date(SGD.cur_datetime)=current_date
group by SGD.cabid) SDM
INNER JOIN server_gpsdata SD ON SDM.maxgpsdataid = SD.gpsdataid
INNER JOIN mstcab MC on MC.cabid= SD.cabid
INNER JOIN server_tblstatus TS on TS.statusid= MC.CabStatusid
INNER JOIN carmaster CM on CM.carid= MC.carid
INNER JOIN cabtype CT on CT.cabtypeid= CM.sizeid
LEFT JOIN trncabdriver D ON MC.Cabid= D.Cabid AND date(D.logintime)=current_date
where D.Cabid IS NULL
Note that both the existing and proposed queries are returning all columns from the server_gpsdata, mstcab, server_tblstatus, carmaster and cabtype tables - the query is likely to perform better if only the columns that are actually required are selected in the query.

Categories