I'm very confused with a stored procedure.
The stored procedure - regardless of how I try to use the joins - seems to repeat certain records. It's always the same user every time this procedure is run within the date range 11-1-2017 to 11-30-2017. I'm seriously at a loss here.
I've attached a screenshot showing 1 record repeating at the bottom of the screenshot.
The code here is where I believe the issue lies:
Code:
FROM
tbl_Claims
LEFT JOIN
tbl_Users ON tbl_Claims.ProvidedBy = tbl_Users.UserID
--***12-15-2017***
LEFT JOIN
tbl_ClaimDetails ON tbl_Claims.ClaimDetailID = tbl_ClaimDetails.ClaimDetailID
LEFT JOIN
tbl_Individuals ON tbl_ClaimDetails.IndID = tbl_Individuals.IndID
--***12-15-2017***
LEFT JOIN
tbl_Users ApprovedByJoin ON tbl_ClaimDetails.ApprovedBy = ApprovedByJoin.UserID
LEFT JOIN
tbl_Events ON tbl_Claims.InternalEventID = tbl_Events.EventID
LEFT JOIN
tbl_Events ted ON tbl_Events.EventID = tbl_Events.EventID
LEFT JOIN
tbl_ExpenseType ON tbl_Claims.ExpenseTypeID = tbl_ExpenseType.ExpenseTypeID
LEFT JOIN
tbl_Services ON tbl_Claims.ServiceID = tbl_Services.ServiceID
Related
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])
I'm not familiar with MS Access 2003-2007 SQL, but I have to maintain/extend a project that uses it. (The original author has left the company. Hooray for legacy code.) What I'm trying to do is join a number of related tables so that the query gives me the number of transactions for a particular user within a particular time range. The end result of this is that I want to see how many hours passed between an OUT event and the previous IN event for that user.
The code I have so far is as follows:
SELECT Directions.DirectionText, Transactions.Timestamp
FROM Users
LEFT JOIN AccessNumbers ON Users.AccessNumberID = AccessNumbers.AccessNumberID
LEFT JOIN Transactions ON AccessNumbers.Number = Transactions.Number
LEFT JOIN Events ON Transactions.Event = Events.EventNumber
LEFT JOIN Readers ON Transactions.ReaderID = Readers.ReaderID
LEFT JOIN Directions ON Readers.Direction = Direction.Direction
WHERE
(Events.EventNum IN (1, 22)) AND
(Users.[Name] = "firstName") AND
(Users.Surname = "Surname") AND
(Transactions.Timestamp >=#2017-04-10 01:00:00#) AND
(Transactions.Timestamp <=#2017-05-09 14:57:30#)
ORDER BY Transactions.Timestamp
The error I receive is "Syntax Error (missing operator in query expression 'Users.AccessNumberID = ... Direction.Direction'
I have also tried the following, to receive "Syntax Error on JOIN operation':
SELECT Directions.DirectionText, Transactions.Timestamp
FROM Users
LEFT JOIN (AccessNumbers ON Users.AccessNumberID =
AccessNumbers.AccessNumberID)
ON (AccessNumbers.Number = Transactions.Number)
ON (Transactions.Event = Events.EventNumber)
ON (Transactions.ReaderID = Readers.ReaderID)
ON (Readers.Direction = Direction.Direction)
WHERE ...
I'm aware that the SQL needs parentheses, but I don't know where to place them.
I've had issues with Access trying to put in my parentheses for me in the past... especially with LEFT joins. I'd normally parenthesize your first query like so:
SELECT Directions.DirectionText, Transactions.Timestamp
FROM ((((Users
LEFT JOIN AccessNumbers ON Users.AccessNumberID = AccessNumbers.AccessNumberID)
LEFT JOIN Transactions ON AccessNumbers.Number = Transactions.Number)
LEFT JOIN Events ON Transactions.Event = Events.EventNumber)
LEFT JOIN Readers ON Transactions.ReaderID = Readers.ReaderID)
LEFT JOIN Directions ON Readers.Direction = Directions.Direction
WHERE
(Events.EventNum IN (1, 22)) AND
(Users.[Name] = "firstName") AND
(Users.Surname = "Surname") AND
(Transactions.Timestamp >=#2017-04-10 01:00:00#) AND
(Transactions.Timestamp <=#2017-05-09 14:57:30#)
ORDER BY Transactions.Timestamp
Your second query would need more LEFT JOIN clauses thrown in to function.
A simple trick is the following:
Add a closing parenthesis before each LEFT beyond the first.
Add an opening parenthesis after FROM for each LEFT beyond the first.
Result:
SELECT Directions.DirectionText, Transactions.Timestamp
FROM ((((Users
LEFT JOIN AccessNumbers ON Users.AccessNumberID = AccessNumbers.AccessNumberID
) LEFT JOIN Transactions ON AccessNumbers.Number = Transactions.Number
) LEFT JOIN Events ON Transactions.Event = Events.EventNumber
) LEFT JOIN Readers ON Transactions.ReaderID = Readers.ReaderID
) LEFT JOIN Directions ON Readers.Direction = Direction.Direction
WHERE
(Events.EventNum IN (1, 22)) AND
(Users.[Name] = "firstName") AND
(Users.Surname = "Surname") AND
(Transactions.Timestamp >=#2017-04-10 01:00:00#) AND
(Transactions.Timestamp <=#2017-05-09 14:57:30#)
ORDER BY Transactions.Timestamp
The result is the same as Sturgus says, but adding them this way makes sense to me. For layout purposes, you might move each closing parenthesis to the line above it.
I am working on a society project. I am using sql database and vb.net. I have four tables on the database as follows.
Registration
Nominee
MemberDetail
MachineryDetail
I want to show data from all four table in one crystal report. I tried to do this using inbuilt sql query creator of vs 2010 but it returns 0 row while data existed in database. I also create a dataset and a table to fill all and tried to show on crystal report but result is zero. my query is as follows -
SELECT Registration.RegID, Registration.BoardNo, Registration.Name, Registration.Gender, Registration.DOB, Registration.RationCardNo, Registration.F_H_Name,
Registration.VoterID, Registration.Eligibility, Registration.PanNo, Registration.Categary, Registration.BankAcNo, Registration.Bussiness, Registration.Phone,
Registration.Address, Registration.Photo, Nominee.NomineeName, Nominee.Relation, Nominee.Adult, Nominee.NDOB, Nominee.Guard_Address,
Nominee.EnrollDate, MemberDetail.F_M_Number, MemberDetail.No_of_Dependent, MemberDetail.Land_Owned, MemberDetail.HouseNo, MemberDetail.MAddress,
MemberDetail.BuiltArea, MemberDetail.HCost, MachinaryDetail.Head, MachinaryDetail.Number, MachinaryDetail.MCost
FROM MachinaryDetail INNER JOIN
MemberDetail ON MachinaryDetail.RegID = MemberDetail.RegID INNER JOIN
Nominee ON MachinaryDetail.RegID = Nominee.RegID INNER JOIN
Registration ON MachinaryDetail.RegID = Registration.RegID
WHERE Registration.RegID='BS-ksd2'
Perhaps one or more of the other tables has no matching record. If so, an INNER JOIN to that table will cause no rows to be returned.
You may want to try a LEFT OUTER JOIN, so that you'll get all the contents from the tables on the left side of the LEFT OUTER JOIN clause.
Try this:
SELECT Registration.RegID, Registration.BoardNo, Registration.Name, Registration.Gender, Registration.DOB, Registration.RationCardNo, Registration.F_H_Name,
Registration.VoterID, Registration.Eligibility, Registration.PanNo, Registration.Categary, Registration.BankAcNo, Registration.Bussiness, Registration.Phone,
Registration.Address, Registration.Photo, Nominee.NomineeName, Nominee.Relation, Nominee.Adult, Nominee.NDOB, Nominee.Guard_Address,
Nominee.EnrollDate, MemberDetail.F_M_Number, MemberDetail.No_of_Dependent, MemberDetail.Land_Owned, MemberDetail.HouseNo, MemberDetail.MAddress,
MemberDetail.BuiltArea, MemberDetail.HCost, MachinaryDetail.Head, MachinaryDetail.Number, MachinaryDetail.MCost
FROM MachinaryDetail
LEFT OUTER JOIN MemberDetail ON MachinaryDetail.RegID = MemberDetail.RegID
LEFT OUTER JOIN Nominee ON MachinaryDetail.RegID = Nominee.RegID
LEFT OUTER JOIN Registration ON MachinaryDetail.RegID = Registration.RegID
WHERE RegID='BS-ksd2'
Lets say I run a crooked car company. Let's say I have the following table:
car_engine_mileage_counters which is a join table from car_engines onto mileage_counters also storing a calculated field of mileage
Lets also say that I encode a coefficient at the engine block level in my factory on an engine template.
UPDATE car_engine_mileage_counters
SET mileage = mileage_counters.mileage * coefficients.coefficient
FROM car_engines
INNER JOIN engine_templates
ON car_engines.template_id = engine_templates.id
INNER JOIN mileage_counters
ON mileage_counters.id = car_engine_mileage_counters.mileage_counter_id
INNER JOIN mileage_counter_templates
ON mileage_counter.template_id = mileage_counter_templates.id
INNER JOIN coefficients
ON coefficients.mileage_counter_template_id = mileage_counter_templates.id
WHERE coefficients.engine_template_id = engine_template.id AND car_engines.id = car_engine_mileage_counters.engine_id;
This (clearly fictitious) example fails with:
ERROR: invalid reference to FROM-clause entry for table
"car_engine_mileage_counters"
LINE 7: ON mileage_counters.id = car_engine_mileage_counters...
^
HINT: There is an entry for table "measure_instances_question_instances", but it cannot be referenced
from this part of the query.
Enumerating all tables in a single FROM clause, and using WHERE AND in place of all INNER JOINs however works fine.
My question is, why? What is wrong with the inner join query? How can I fix it? Does it matter?
UPDATE cem
SET mileage = mileage_counters.mileage * coefficients.coefficient
FROM car_engine_mileage_counters cem
INNER JOIN car_engines
ON car_engines.id = cem.engine_id
INNER JOIN engine_templates
ON car_engines.template_id = engine_templates.id
INNER JOIN mileage_counters
ON mileage_counters.id = cem.mileage_counter_id
INNER JOIN mileage_counter_templates
ON mileage_counter.template_id = mileage_counter_templates.id
INNER JOIN coefficients
ON coefficients.mileage_counter_template_id = mileage_counter_templates.id
WHERE coefficients.engine_template_id = engine_template.id AND car_engines.id = cem.engine_id;
I'm trying to retrieve a list of components via my computer_system, BUT if a computer system's graphics card is set to null (I.e. It has an onboard), the row isn't returned by my select statement.
I've been trying to use COALESCE without results. I've also tried with and OR in my WHERE clause, which then just returns my computer system with all different kinds of graphic cards.
Relevant code:
SELECT
computer_system.cs_id,
computer_system.cs_name,
motherboard.name,
motherboard.price,
cpu.name,
cpu.price,
gfx.name,
gfx.price
FROM
public.computer_case ,
public.computer_system,
public.cpu,
public.gfx,
public.motherboard,
public.ram
WHERE
computer_system.cs_ram = ram.ram_id AND
computer_system.cs_cpu = cpu.cpu_id AND
computer_system.cs_mb = motherboard.mb_id AND
computer_system.cs_case = computer_case.case_id AND
computer_system.cs_gfx = gfx.gfx_id; <-- ( OR computer_system.cs_gfx IS NULL)
Returns:
1;"Computer1";"Fractal Design"; 721.00; "MSI Z87"; 982.00; "Core i7 I7-4770K "; 2147.00; "Crucial Gamer"; 1253.00; "ASUS GTX780";3328.00
Should I use Joins? Is there no easy way to say return the requested row, even if there's a bloody NULL value. Been struggling with this for at least 2 hours.
Tables will be posted if needed.
EDIT: It should return a second row:
2;"Computer2";"Fractal Design"; 721.00; "MSI Z87"; 982.00; "Core i7 I7-4770K "; 2147.00; "Crucial Gamer"; 1253.00; "null/nothing";null/nothing
You want a LEFT OUTER JOIN.
First, clean up your code so you use ANSI joins so it's readable:
SELECT
computer_system.cs_id,
computer_system.cs_name,
motherboard.name,
motherboard.price,
cpu.name,
cpu.price,
gfx.name,
gfx.price
FROM
public.computer_system
INNER JOIN public.computer_case ON computer_system.cs_case = computer_case.case_id
INNER JOIN public.cpu ON computer_system.cs_cpu = cpu.cpu_id
INNER JOIN public.gfx ON computer_system.cs_gfx = gfx.gfx_id
INNER JOIN public.motherboard ON computer_system.cs_mb = motherboard.mb_id
INNER JOIN public.ram ON computer_system.cs_ram = ram.ram_id;
Then change the INNER JOIN on public.gfx to a LEFT OUTER JOIN:
LEFT OUTER JOIN public.gfx ON computer_system.cs_gfx = gfx.gfx_id
See PostgreSQL tutorial - joins.
I very strongly recommend reading an introductory tutorial to SQL - at least the PostgreSQL tutorial, preferably some more material as well.
It looks like it's just a bracket placement issue. Pull the null check and the graphics card id comparison into a clause by itself.
...
computer_system.cs_case = computer_case.case_id AND
(computer_system.cs_gfx IS NULL OR computer_system.cs_gfx = gfx.gfx_id)
Additionally, you ask if you should use joins. You are in fact using joins, by virtue of having multiple tables in your FROM clause and specifying the join criteria in the WHERE clause. Changing this to use the JOIN ON syntax might be a little easier to read:
FROM sometable A
JOIN someothertable B
ON A.somefield = B.somefield
JOIN somethirdtable C
ON A.somefield = C.somefield
etc
Edit:
You also likely want to make the join where you expect the null value to be a left outer join:
SELECT * FROM
first_table a
LEFT OUTER JOIN second_table b
ON a.someValue = b.someValue
If there is no match in the join, the row from the left side will still be returned.