SQL-null value showing in the output when there is a value present in the database - sql

I am extracting values from two different tables in the database. I should get only one line of output but instead I am getting two lines. One of the output line is perfect but the other line has one null value which clearly should not be null. My code is as below:
SELECT DISTINCT DEATH_RATE.COUNTRY_NAME,
DEATH_RATE.DATA_YEAR,
DEATH_RATE.DEATH_RATE_VALUE,
TIME_TO_EXPORT.EXPORT_VALUE
FROM DEATH_RATE, TIME_TO_EXPORT
WHERE TIME_TO_EXPORT.COUNTRY_NAME IN ('Belgium')
AND TIME_TO_EXPORT.COUNTRY_NAME = DEATH_RATE.COUNTRY_NAME
AND DEATH_RATE.DATA_YEAR = 2012
AND DEATH_RATE.DATA_YEAR = DEATH_RATE.DATA_YEAR ;
The output I am getting is as follows:
COUNTRY_NAME DATA_YEAR DEATH_RATE EXPORT_VALUE
1.Belgium 2012 423.5 9
2.Belgium 2012 423.5 null
First line is fine and only that should have been the output. Where is the second line and the null coming from ??
Thanks

The last condition of your query is a bit useless (the condition is always true when the value is not null):
DEATH_RATE.DATA_YEAR = DEATH_RATE.DATA_YEAR
I think you intended that :
TIME_TO_EXPORT.DATA_YEAR = DEATH_RATE.DATA_YEAR
(I assume that the fields have the same name)
In your select, you were thus getting all the TIME_TO_EXPORT records related to 'Belgium' whatever the value of DATA_YEAR.
So your query becomes :
SELECT DISTINCT DEATH_RATE.COUNTRY_NAME,
DEATH_RATE.DATA_YEAR,
DEATH_RATE.DEATH_RATE_VALUE,
TIME_TO_EXPORT.EXPORT_VALUE
FROM DEATH_RATE, TIME_TO_EXPORT
WHERE TIME_TO_EXPORT.COUNTRY_NAME IN ('Belgium')
AND TIME_TO_EXPORT.COUNTRY_NAME = DEATH_RATE.COUNTRY_NAME
AND DEATH_RATE.DATA_YEAR = 2012
AND TIME_TO_EXPORT.DATA_YEAR = DEATH_RATE.DATA_YEAR ;

Replace your last line with this:
AND TIME_TO_EXPORT.DATA_YEAR = DEATH_RATE.DATA_YEAR;

DEATH_RATE are TIME_TO_EXPORT are not properly join. and of course last line has no meaning.

Related

String concatenation query returning wrong results

Trying to automate this part of Teradata query:
c."currency" = CONCAT('FOO_', '2021') -- c."currency" = 'FOO_2021'
trying to replace the 2021 with the currently selected fiscal-year, so it is not hard-coded:
c."currency" = CONCAT('FOO_', c."fiscal-year")
c."fiscal-year" is of type VARCHAR(20).
Problem - the first query returns about 5 times more results than the second one, so I am doing something wrong, I guess. Tried with TO_CHAR(c."fiscal-year") and so on, did not succeed.

How to concatenate date variables to create between in where condition

While I am trying to set my value to between the user given start date and end date for a query, I am running into a run-time error 3071 (The expression is typed incorrectly or it is too complex)
This is being used to pass a user given variable from a form to a query
Please see below
WHERE (
IIf([Forms]![Find]![Entity]<>"",DB.Entity=[Forms]![Find]![Entity],"*")
AND IIf([Forms]![Find]![AEPS]<>"",DB.AEPSProgram=[Forms]![Find]![AEPS],"*")
AND IIf([Forms]![Find]![DeliveryType]<>"",DB.DeliveryType=[Forms]![Find]![DeliveryType],"*")
AND IIf([Forms]![Find]![ReportingYear] is not Null,DB.ReportingYear=[Forms]![Find]![ReportingYear],"*")
AND IIf([Forms]![Find]![Price] is not Null,DB.Price=[Forms]![Find]![Price],"*")
AND IIf([Forms]![Find]![Volume]is not Null,DB.Volume=[Forms]![Find]![Volume],"*")
AND IIf([Forms]![Find]![sDate] is not Null AND [Forms]![Find]![eDate] is not Null,DB.TransactionDate= ">" & [Forms]![Find]![sdate] & " and <" & [Forms]![Find]![edate],"*")
);
If I set it equal to one of the dates it works as expected. I assume I am missing something with how I am joining the dates
Thank you
This (for the date field only) works:
WHERE (((DB.TransactionDate)>[Forms]![Find]![sdate] And (DB.TransactionDate)<[Forms]![Find]![edate])) OR ((([Forms]![Find]![sDate]+[Forms]![Find]![eDate]) Is Null));
Don't use *, it is for use with Like.
Consider assigning NULL condition to corresponding column via NZ() which sets column equal to itself and so avoids filtering any rows by that respective condition. Asterisks alone does not evaluate to a boolean condition unless using LIKE operator.
WHERE DB.Entity = NZ([Forms]![Find]![Entity], DBEntity)
AND DB.AEPSProgram = NZ([Forms]![Find]![AEPS], DB.AEPSProgram)
AND DB.DeliveryType = NZ([Forms]![Find]![DeliveryType], DB.DeliveryType)
AND DB.ReportingYear = NZ([Forms]![Find]![ReportingYear], DB.ReportingYear)
AND DB.Price = NZ([Forms]![Find]![Price], DB.Price)
AND DB.Volume = NZ([Forms]![Find]![Volume], DB.Volume)
AND DB.TransactionDate >= NZ([Forms]![Find]![sdate], DB.TransactionDate)
AND DB.TransactionDate <= NZ([Forms]![Find]![edate], DB.TransactionDate)
;

Update a Table using a Join

I wish to update a table using, but need to use another table to get the correct field. The new information is not taken from another field from another table.
The following SQL statement returns the correct information:
SELECT PURCHASEHEADER.ORDERNOTES
FROM PURCHASEHEADER, ASSEMBLYLINESOURCE
WHERE ASSEMBLYLINESOURCE.HEADERSYSUNIQUEID = 72637001
AND PURCHASEHEADER.ORDERNUMBER = ASSEMBLYLINESOURCE.PURCHASEORDERNUMBER
I have tried the following:
UPDATE PURCHASEHEADER SET PURCHASEHEADER.ORDERNOTES = 'Updated'
WHERE EXISTS (
SELECT 1 FROM ASSEMBLYLINESOURCE
WHERE PURCHASEHEADER.ORDERNUMBER = ASSEMBLYLINESOURCE.PURCHASEORDERNUMBER
) AND ASSEMBLYLINESOURCE.HEADERSYSUNIQUEID = 72637001
An error is returned saying: " ...Column Unknown ASSEMBLYLINESOURCE.HEADERSYSUNIQUEID..." but it does exist as it works in the first query.
I have seen similar posts from Mark Rotteveel dated July 2017, but still can't get it to work.
There is an issue with your closing bracket. Try this, it worked for me.
UPDATE PURCHASEHEADER set PURCHASEHEADER.ORDERNOTES = 'Updated'
WHERE EXISTS (SELECT 1 FROM ASSEMBLYLINESOURCE WHERE
PURCHASEHEADER.ORDERNUMBER = ASSEMBLYLINESOURCE.PURCHASEORDERNUMBER AND
ASSEMBLYLINESOURCE.HEADERSYSUNIQUEID = 72637001)

Simple, but most likely an incorrect Join is giving a MAX_JOIN_SIZE error

Probably a simple SQL query, but struggling as still learning
The following query runs fine:
SELECT NationalArea. *
FROM NationalArea
WHERE NationalArea.AreaCode = '01922'
This returns about 30 results.
This also runs fine:
SELECT DestinationNames.Name
FROM `DestinationNames`
WHERE DestinationNames.AreaCode = '01922'
This returns just the one
I am trying to run a query that joins the two where the National Area will give a list of area codes and the destination will match those area codes with the names of the towns. The query I have is as follows:
SELECT NationalArea.*, DestinationNames.Name
FROM NationalArea
JOIN DestinationNames
ON NationalArea.AreaCode=DestinationNames.AreaCode
WHERE NationalArea.AreaCode = '01922'
But I get the following error
1104 - The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay
Thanks in advance
You can display the current value with
SHOW VARIABLES LIKE '%MAX_JOIN_SIZE%';
You can change it with:
SET MAX_JOIN_SIZE = 100
Or skip the check entirely with (run this as a separate command before your query):
SET SQL_BIG_SELECTS = 1
But I would first examine why your join returns more than that. It doesn't look like it should. The default value of max_join_size is 4294967295!

sqlite3 UPDATE generating nulls

I'm trying to transition from MySQL to SQLIte3 and running into an update problem. I'm using SQLite 3.6.20 on redhat.
My first line of code behaves normally
update atv_covar set noncomp= 2;
All values for noncomp (in the rightmost column) are appropriately set to 2.
select * from atv_covar;
A5202|S182|2
A5202|S183|2
A5202|S184|2
It is the second line of code that gives me problems:
update atv_covar
set noncomp= (select 1 from f4003 where
atv_covar.study = f4003.study and
atv_covar.rpid = f4003.rpid and
(rsoffrx="81" or rsoffrx="77"));
It runs without generating errors and appropriately sets atv_covar.noncomp to 1 where it matches the SELECT statement. The problem is that it changes atv_covar.noncomp for the non-matching rows to null, where I want it to keep them as 2.
select * from atv_covar;
A5202|S182|
A5202|S183|1
A5202|S184|
Any help would be welcome.
#Dan, the problem with your query is not specific to SQLite; you are updating all rows of atv_covar, but not all of them have correspondence in f4003, so these default to NULL. You should filter the update or provide a default value.
The following statement sets 1 only to the rows that macth the filtering condition:
UPDATE atv_covar
SET noncomp = 1
WHERE EXISTS (
SELECT 'x'
FROM f4003
WHERE atv_covar.study = f4003.study
AND atv_covar.rpid = f4003.rpid
AND (rsoffrx="81" or rsoffrx="77")
);
The following statement sets 1 or 2 for all rows of noncomp, depending on the filtering match (use this instead of two updates):
UPDATE atv_covar
SET noncomp = COALESCE((
SELECT 1
FROM f4003
WHERE atv_covar.study = f4003.study
AND atv_covar.rpid = f4003.rpid
AND (rsoffrx="81" or rsoffrx="77")
), 2);