SQL Query syntax, I want to use INNER JOIN - sql

I'm working on a windows application project using front end "vb.net" & back end "Ms Access" I have problem in wrinting sql query
Actually there are 5 tables Transaction,items,itemtitle,itemtype & userinfo.
check the following query & with this referance if u get idea then plz change in correct query
Thanking You
SELECT
TRANSACTIONS.ACCESSIONNO AS
ACCESSIONNO,TRANSACTIONS.TYPEID,
TRANSACTIONS.CHECKOUTDATE AS CHECKOUTDATE,ITEMTITLE.ITEMTITLE,
TRANSACTIONS.CHECKEDOUTBY,
USERINFO.FULLNAME_ENG,
USERINFO.FULLNAME_MAR,
TRANSACTIONS.ACCOUNTNO,
ITEMTYPE.TYPES_MAR,
ITEMTYPE.TYPES_ENG
FROM
TRANSACTIONS,ITEMTYPE,
ITEMTITLE,
USERINFO
WHERE
TRANSACTIONS.ACCOUNTNO=USERINFO.ACCOUNTNO
AND TRANSACTIONS.ACCESSIONNO=ITEMS.ACCESSIONNO
AND ITEMS.ITEMTITLEID=ITEMTITLE.ITEMTITLEID
AND TRANSACTIONS.TYPEID=ITEMTYPE.TYPEID
AND TRANSACTIONS.STATUS='Enabled'

It looks like you left out the ITEMS table. The below joins to that table. At any rate, it demonstrates the INNER JOIN syntax. (Usually I use aliases for readability. I purposefully them out.)
SELECT TRANSACTIONS.ACCESSIONNO AS ACCESSIONNO, TRANSACTIONS.TYPEID, TRANSACTIONS.CHECKOUTDATE AS CHECKOUTDATE, ITEMTITLE.ITEMTITLE, TRANSACTIONS.CHECKEDOUTBY, USERINFO.FULLNAME_ENG, USERINFO.FULLNAME_MAR, TRANSACTIONS.ACCOUNTNO, ITEMTYPE.TYPES_MAR, ITEMTYPE.TYPES_ENG
FROM TRANSACTIONS
INNER JOIN ITEMTYPE ON (TRANSACTIONS.TYPEID = ITEMTYPE.TYPEID)
INNER JOIN ITEMTITLE ON (ITEMS.ITEMTITLEID = ITEMTITLE.ITEMTITLEID)
INNER JOIN USERINFO ON (TRANSACTIONS.ACCOUNTNO = USERINFO.ACCOUNTNO)
INNER JOIN ITEMS ON (TRANSACTIONS.ACCESSIONNO = ITEMS.ACCESSIONNO)
WHERE TRANSACTIONS.STATUS = 'Enabled'

Related

Syntax error in complicated JOIN clause of SQL query through Excel VBA

I have 4 tables I need to join w/ the following fields that can be linked:
tblModels (TRADE_DATE, MODEL)
tblModelDef (MODEL, ASSET)
tblFutPx (TRADE_DATE, ASSET)
tblTA (TRADE_DATE, ASSET)
I want to "LEFT JOIN" the tblModels- i.e. I want to display all of the data for tblModels and show data for tblFutPx and tblTA when available for the corresponding [TRADE_DATE]. So the link is [TRADE_DATE] and [ASSET] for the relevant data, but I need to use tblModelDef as a bridge between tblModels and tblFutPx & tblTA.
I keep getting "Syntax error in JOIN operation" errors.
I've been trying to get this to work for way too long, but this is where I'm at. I've tried simplifying the FROM clause to ensure the error is coming from it, and it is.
FROM ((tblModels INNER JOIN tblModelDef ON tblModels.[MODEL] = tblModelDef.[MODEL]) LEFT JOIN tblFutPx ON (tblModelDef.[ASSET] = tblFutPx.[ASSET]) AND (tblModels.[TRADE_DATE] = tblFutPx.[TRADE_DATE])) LEFT JOIN (tblTA ON (tblModels.[TRADE_DATE] = tblTA.[TRADE_DATE]) AND (tblModelDef.[ASSET] = tblTA.[ASSET]))
Probably need multiple queries to accomplish. Built tables and used Query Builder to design query. Did not assign any primary keys, just set links to 'show all records from tblModels...'. Access used RIGHT JOIN.
Query1:
SELECT tblModelDef.Model, tblModelDef.Asset, tblModels.Trade_Date
FROM tblModelDef RIGHT JOIN tblModels ON tblModelDef.Model = tblModels.Model;
Query2:
SELECT Query1.Model, tblFutPx.Trade_Date, tblFutPx.Asset, tblTA.Trade_Date, tblTA.Asset
FROM tblFutPx RIGHT JOIN (tblTA RIGHT JOIN Query1 ON (tblTA.Asset = Query1.Asset) AND (tblTA.Trade_Date = Query1.Trade_Date)) ON (tblFutPx.Asset = Query1.Asset) AND (tblFutPx.Trade_Date = Query1.Trade_Date);
All in one nested:
SELECT Query1.Model, tblFutPx.Trade_Date, tblFutPx.Asset, tblTA.Trade_Date, tblTA.Asset
FROM tblFutPx RIGHT JOIN (tblTA RIGHT JOIN
(SELECT tblModelDef.Model, tblModelDef.Asset, tblModels.Trade_Date
FROM tblModelDef RIGHT JOIN tblModels ON tblModelDef.Model = tblModels.Model) AS Query1
ON (tblTA.Asset = Query1.Asset) AND (tblTA.Trade_Date = Query1.Trade_Date)) ON (tblFutPx.Asset = Query1.Asset) AND (tblFutPx.Trade_Date = Query1.Trade_Date);

MS Access INNER JOIN/LEFT JOIN problems

I have the following SQL string which tries to combine an INNER JOIN with a LEFT JOIN in the FROM section.
As you can see I use table VIP_APP_VIP_SCENARIO_DETAIL_LE to perform the query. When I use it against this table, Access give me an "Invalid Operation" error.
Interestingly, when I use the EXACT same query using the VIP_APP_VIP_SCENARIO_DETAIL_BUDGET or VIP_APP_VIP_SCENARIO_DETAIL_ACTUALS table, it performs flawlessly.
So why would it work on two tables but not the other? All fields are in all tables and the data types are correct.
As a side note: on the query with the error, if I change the LEFT JOIN to an INNER JOIN, it runs with no problem! I really need a LEFT JOIN though.
SELECT
D.MATERIAL_NUMBER,
D.MATERIAL_DESCRIPTION,
D.PRODUCTION_LOT_SIZE,
D.STANDARDS_NAME,
D.WORK_CENTER,
S.OP_SHORT_TEXT,
S.OPERATION_CODE,
D.LINE_SPEED_UPM,
D.PERCENT_STD,
D.EQUIPMENT_SU,
D.EQUIPMENT_CU,
D.OPERATOR_NUM,
V.COSTING_LOT_SIZE,
V.VOL_TOTAL_ADJ
FROM
([STDS_SCENARIO: TEST] AS D INNER JOIN MASTER_SUMMARY AS S ON
D.MATERIAL_NUMBER = S.MATERIAL_NUMBER AND D.WORK_CENTER = S.WORK_CENTER)
LEFT JOIN
(SELECT ITEM_CODE, COSTING_LOT_SIZE, VOL_TOTAL_ADJ
FROM
VIP_APP_VIP_SCENARIO_DETAIL_LE
WHERE SCENARIO_ID = 16968) AS V ON D.MATERIAL_NUMBER = V.ITEM_CODE
ORDER BY D.MATERIAL_NUMBER, D.STANDARDS_NAME, S.OPERATION_CODE;
tried to mock this up in SQL server with some tables of my own, but the structure seemed to work, this follows the pattern referenced above. (hopefully no syntax errors left here)
SELECT * FROM (
select
D.MATERIAL_NUMBER,
D.MATERIAL_DESCRIPTION,
D.PRODUCTION_LOT_SIZE,
D.STANDARDS_NAME,
D.WORK_CENTER,
S.OP_SHORT_TEXT,
S.OPERATION_CODE,
D.LINE_SPEED_UPM,
D.PERCENT_STD,
D.EQUIPMENT_SU,
D.EQUIPMENT_CU,
D.OPERATOR_NUM
FROM [STDS_SCENARIO: TEST] D
INNER JOIN MASTER_SUMMARY S
ON D.MATERIAL_NUMBER = S.MATERIAL_NUMBER AND D.WORK_CENTER = S.WORK_CENTER) AS J
LEFT JOIN
(SELECT ITEM_CODE, COSTING_LOT_SIZE, VOL_TOTAL_ADJ
FROM
VIP_APP_VIP_SCENARIO_DETAIL_LE
WHERE SCENARIO_ID = 16968) AS V ON J.MATERIAL_NUMBER = V.ITEM_CODE
ORDER BY J.MATERIAL_NUMBER, J.STANDARDS_NAME, J.OPERATION_CODE;
Had help from a friend and we discovered that it was a casting problem between a linked Oracle table and the Access table. To fix the problem we casted both sides of the linked fields to a string:
CSTR(D.[MATERIAL_NUMBER]) = CSTR(V.[ITEM_CODE])

Access SQL Matching Join then Like Join

Good Afternoon,
Is is possible in MS Access SQL to firstly try a matching left join and if there is no match then to a like/contains join?
Currently my like join looks like this
SELECT
[tbl_Area_lkup].[Area],
[tbl_Area_lkup].[Sub Area],
[tbl_Area_lkup].[PageTrimmed] AS [Page lkup],
qry_AdviserPages_Step1.PageTrimmed AS [Trimmed Page],
qry_AdviserPages_Step1.Page AS Page,
qry_AdviserPages_Step1.Date, IIf([tbl_Medium_lkup].[mediumgrp] Is Null,"CHECK",[tbl_Medium_lkup].[mediumgrp]) AS Medium,
qry_AdviserPages_Step1.Hostname,
qry_AdviserPages_Step1.Users,
qry_AdviserPages_Step1.Sessions,
qry_AdviserPages_Step1.Pageviews
FROM (qry_AdviserPages_Step1
LEFT JOIN tbl_Area_lkup ON [qry_AdviserPages_Step1].[PageTrimmed] Like '*' & [tbl_Area_lkup].[PageTrimmed] & '*')
LEFT JOIN tbl_Medium_lkup ON qry_AdviserPages_Step1.Medium = tbl_Medium_lkup.Medium
ORDER BY 2;
What I would like to do is an = join on the same Columns but if there is no match then to carry out the above join.
Any pointers would be great.
Thanks.

SQL Joins are not working properly

I am new to JOINS and testing my query, but it's just not working for me...
The situation:
The database has got the following columns:
links (contains unique data)
cl_link (contains the relation between links & cats)
cats (cat. descriptions
images (contains multiple images of one link)
cfvalues (contains the values of the multiple custom fiels
customfields (contains the multiple customfields)
I am using the following query, but the Joins are not working for me. Because I only get one image while sometimes there are multiple. And I only get one customfield instead of multiple and I get none cfvalues.
I guess something is wrong with the JOINS, but I am not sure. Can somebody help me out here?
The SQL
SELECT DISTINCT
rqypj_mt_links.link_name,
rqypj_mt_links.link_desc,
rqypj_mt_links.address,
rqypj_mt_links.city,
rqypj_mt_links.state,
rqypj_mt_links.country,
rqypj_mt_links.postcode,
rqypj_mt_links.telephone,
rqypj_mt_links.fax,
rqypj_mt_links.email,
rqypj_mt_links.website,
rqypj_mt_links.price,
rqypj_mt_links.lat,
rqypj_mt_links.lng,
rqypj_mt_links.zoom,
rqypj_mt_cats.cat_name,
rqypj_mt_images.filename,
rqypj_mt_cfvalues.value,
rqypj_mt_customfields.caption
FROM rqypj_mt_links
LEFT JOIN rqypj_mt_cl
ON rqypj_mt_links.link_id = rqypj_mt_cl.link_id
LEFT JOIN rqypj_mt_cats
ON rqypj_mt_cl.cat_id = rqypj_mt_cats.cat_id
LEFT JOIN rqypj_mt_images
ON rqypj_mt_links.link_id = rqypj_mt_images.link_id
LEFT JOIN rqypj_mt_cfvalues
ON rqypj_mt_links.link_id = rqypj_mt_cfvalues.link_id
LEFT JOIN rqypj_mt_customfields
ON rqypj_mt_customfields.cf_id = rqypj_mt_customfields.cf_id LIMIT 100
Thanks in advance!
Jelte
your last condition doesn't look right:
on rqypj_mt_customfields.cf_id = rqypj_mt_customfields.cf_id
translates to 1=1
Shouldn't it be:
on rqypj_mt_customfields.cf_id = rqypj_mt_cfvalues.cf_id
Probably because you don't have an order by and are using limit.
Change it to
order by rqypj_mt_links.link_id, rqypj_mt_cl.cat_id
limit 100
and then your multiple pictures for the same link should be together.
Also please consider use of alias to make your code easier to read:
SELECT DISTINCT
links.link_name,
links.link_desc,
links.address,
links.city,
links.state,
links.country,
links.postcode,
links.telephone,
links.fax,
links.email,
links.website,
links.price,
links.lat,
links.lng,
links.zoom,
cats.cat_name,
images.filename,
cfvalues.value,
--custom.caption
FROM rqypj_mt_links links
LEFT JOIN rqypj_mt_cl cl ON links.link_id = cl.link_id
LEFT JOIN rqypj_mt_cats cats ON cl.cat_id = cats.cat_id
LEFT JOIN rqypj_mt_images images ON links.link_id = images.link_id
LEFT JOIN rqypj_mt_cfvalues cfvalues ON links.link_id = cfvalues.link_id
--LEFT JOIN rqypj_mt_customfields custom ON custom.cf_id = custom.cf_id
ORDER BY links.link_id, cats.cat_id
LIMIT 100

SQL Query: Comparing two dates in returned record

I'm trying to come up with an automated solution for something I do manually now and I only have minimal, bare-bones SQL skill. I usually modify simple queries others have built or will build basic select queries. I have done some reading but don't know how to make it do what I need in this case. I need to come up with something others can use while I am out for a month (and which will save me time when I return).
What I need is to return the fields below where tblThree.EndDate is later than tblFive.ServiceEnd. I have to do a couple of other compares on the dates, but if I get a working query of the first one I can make it work with the others. We use MS SQL Server 2008.
I tried creating sub-queries with aliases and failed miserably at making it work.
These are the table and fields I am working with:
tblOne.ServiceID
tblOne.ServiceYear
tblOne.Status
tblTwo.AccountNbr
tblTwo.AcctName
tblThree.BeginDate (smalldatetime, null)
tblThree.EndDate (smalldatetime, null)
tblFour.ClientID
tblFour.ServiceName
tblFive.ContractID
tblFive.ServiceBegin (smalldatetime, null)
tblFive.ServiceEnd (smalldatetime, null)
This is how the tables are related:
tblOne.ServiceID = tblThree.ServiceID
tblOne.ContractID = tblFive.ContractID
tblOne.ClientID = tblFour.ClientID
tblTwo.AccountNbr = tblFour.Account
I used MS Access 2003 to generate the Join SQL:
SELECT tblOne.ServiceID, tblTwo.AccountNbr,
tblTwo.AcctName, tblFour.ServiceName, tblOne.Status,
tblThree.BeginDate, tblThree.EndDate,
tblOne.ServiceYear, tblFive.ServiceBegin,
tblFive.ServiceEnd
FROM ((tblTwo INNER JOIN tblFour
ON tblTwo.AccountNbr=tblFour.AccountNbr) INNER JOIN (tblThree INNER JOIN tblOne
ON tblThree.ServiceID=tblOne.ServiceID)
ON tblFour.ClientID=tblOne.ClientID) INNER JOIN tblFive
ON tblOne.ContractID=tblFive.ContractID;
Thanks for any help.
Just add a WHERE clause to get started:
SELECT tblOne.ServiceID, tblTwo.AccountNbr,
tblTwo.AcctName, tblFour.ServiceName, tblOne.Status,
tblThree.BeginDate, tblThree.EndDate,
tblOne.ServiceYear, tblFive.ServiceBegin,
tblFive.ServiceEnd
FROM ((tblTwo INNER JOIN tblFour
ON tblTwo.AccountNbr=tblFour.AccountNbr) INNER JOIN (tblThree INNER JOIN tblOne
ON tblThree.ServiceID=tblOne.ServiceID)
ON tblFour.ClientID=tblOne.ClientID) INNER JOIN tblFive
ON tblOne.ContractID=tblFive.ContractID
WHERE tblThree.EndDate > tblFive.ServiceEnd;
SELECT
tblOne.ServiceID,
tblOne.ServiceYear,
tblOne.Status,
tblTwo.AccountNbr,
tblTwo.AcctName,
tblThree.BeginDate,
tblThree.EndDate,
tblFour.ClientID,
tblFour.ServiceName,
tblFive.ContractID,
tblFive.ServiceBegin,
tblFive.ServiceEnd
FROM tblOne
INNER JOIN tblThree
ON tblOne.ServiceID = tblThree.ServiceID
INNER JOIN tblFive
ON tblOne.ContractID = tblFive.ContractID
INNER JOIN tblFour
ON tblOne.ClientID = tblFour.ClientID
INNER JOIN tblTwo
ON tblTwo.AccountNbr = tblFour.Account
WHERE tblThree.EndDate > tblFive.ServiceEnd