Unusual SQL Server Query Result - sql-server-2008

I faced with an unusual query result while running a sql server query.
The important table to note is Code_Structure_Type.
It contains columns Code_Structure_Type_Id smallint, Name char(50) and few other columns that are not used here.
My Query
SELECT DISTINCT A1.A1_Id AS Code_Unit_Attribute_Id,
A1.Name AS Code_Unit_Attribute_Name,
Code_Structure_Type.A1_Name AS Code_Attribute_Type
--, Code_Structure_Type.Code_Structure_Type_Id
FROM Defect
INNER JOIN Defect_Code_Unit
ON Defect.Defect_Id = Defect_Code_Unit.Defect_Id
INNER JOIN Code_Unit
ON Defect_Code_Unit.Code_Unit_Id = Code_Unit.Code_Unit_Id
INNER JOIN A1
ON Code_Unit.A1_Id = A1.A1_Id
INNER JOIN ERA_Module
ON Defect.ERA_Module_Id = ERA_Module.ERA_Module_Id
INNER JOIN Code_Unit_Type
ON Code_Unit.Code_Unit_Type_Id = Code_Unit_Type.Code_Unit_Type_Id
INNER JOIN Code_Structure_Type
ON Code_Unit_Type.Code_Structure_Type_Id = Code_Structure_Type.Code_Structure_Type_Id
WHERE ERA_Module.ERA_Project_Id = 472
AND ERA_Module.Is_Active = 1
AND Defect.Is_Closed = 0
AND Defect_Code_Unit.Is_Active = 1
AND Code_Structure_Type.Name = 'Pega Rules'
--AND Code_Structure_Type.Code_Structure_Type_Id = 2
AND A1.A1_Id > 0
AND Code_Unit.Scope_Id = 1
This query returns me with a single row which is incorrent. The expected result of this query should be none because we do not have Defects for 'Pega Rules' Code_Structure_Type.
If I uncomment this line
--, Code_Structure_Type.Code_Structure_Type_Id
I get no results which is correct.
Also if I filter from the Code_Structure_Type_Id which is for 'Pega Rules' not with Code_Structure_Type.Name I get the correct result as well which is none.
To do this I uncomment this line
--AND Code_Structure_Type.Code_Structure_Type_Id = 2
and comment this line
AND Code_Structure_Type.Name = 'Pega Rules'
So basically I'm doing the same thing and there is no other name like 'Pega Rules' in the table Code_Structure_Type, not even close. Only five rows are there in that table.
I need to understand what is happening here.
Thanks in advance.
Links to Execution Plans
Files, when used 'pega rules' only - incorrect_original.xml when used 'pega rules' + select - correct_with_added_select.xml when used id - correct_with_Id.xml

Related

How to pull in null values when searching for specific document name

I am having issues pulling in null values in my query. I am looking for patients who have a specific document name in their chart but also want to show patients who do not have this specific document name as well. Right now my code is only pulling in the patients with the document name History and Physical (Transcription) but I need to see Null values as well. Below is my code:
snip of code
SELECT CV3ClientVisit.ClientDisplayName, CV3ClientVisit.CurrentLocation, CV3ClientVisit.IDCode, CV3ClientVisit.VisitIDCode, CV3ClientVisit.VisitStatus, CV3ClientVisit.TypeCode, CV3ClientDocumentCUR.DocumentName
FROM CV3ClientVisit INNER JOIN
CV3ClientDocumentCUR ON CV3ClientVisit.GUID = CV3ClientDocumentCUR.ClientVisitGUID
WHERE (CV3ClientVisit.VisitStatus = 'ADM') AND (CV3ClientVisit.TypeCode = 'INPATIENT ADMIT') AND (CV3ClientDocumentCUR.DocumentName = 'History & Physical (transcription)' OR CV3ClientDocumentCUR.DocumentName IS NULL )
Use a LEFT JOIN with the condition in the ON clause:
SELECT cv.ClientDisplayName, cv.CurrentLocation, cv.IDCode,
cv.VisitIDCode, cv.VisitStatus, cv.TypeCode, cd.DocumentName
FROM CV3ClientVisit cv LEFT JOIN
CV3ClientDocumentCUR cd
ON cv.GUID = cd.ClientVisitGUID AND
cd.DocumentName = 'History & Physical (transcription)'
WHERE cv.VisitStatus = 'ADM' AND
cv.TypeCode = 'INPATIENT ADMIT' ;
I also added table aliases to simplify the query.

SQL 1064 Error, Syntax going unseen

SELECT
i.*,
ii.file_location
FROM group_shop_item i, group_shop_itemimage ii, group_shop_brand b
WHERE
i.brand_id = b.id
AND
b.brand_status_id = 1
AND
i.is_deleted = 0
AND
i.is_displayed = 1
AND
i.id = ii.item_id
AND
ii.is_main = 1
AND
i.deal_participate = 1
AND
i.brand_label_id IS NOT NULL
ORDER BY i.datetime_modified DESC;
This SQL query keeps throwing me a 1064. It seems to be on the last line which I've tried with and without the i table variable. I can't for the life of me catch the error, anyone can lend me another pair of eyes?
I'm throwing this as a RAW query into the in built Django function and building this query with string concatenation. This copy paste is directly from a print I've done from the command line. It's outputting neatly but isn't reading when I run the view on my browser.
Over 25 years ago ANSI Standard join syntax was adopted. You need to cease using comas between table names in the from clause.
SELECT
i.*
, ii.file_location
FROM group_shop_item i
INNER JOIN group_shop_itemimage ii ON i.id = ii.item_id
INNER JOIN group_shop_brand b ON i.brand_id = b.id
WHERE i.is_deleted = 0
AND i.is_displayed = 1
AND ii.is_main = 1
AND i.deal_participate = 1
AND i.brand_label_id IS NOT NULL
AND b.brand_status_id = 1
;
Regarding the 1064 error, please read this without the exact error message and the exact/full query we can't offer much insight into that.
The other thing you need to be careful of is that "select *" isn't good practice either.

how to perform count using nested select in a select statement

So here is the issue. I'm trying to write a new fillrate report because the one built in is not good enough... I'm trying to run a single select statement to return both, a count of how many times an item was ordered for a specific month, and then also a count of how many times it was invoiced/shipped in full.
This code is obviously wrong, I also currently have it restricted to only look at AUG of 2015, but that is just to simplify results during testing.
I can't figure out how to do the 2nd count... This is what I was trying (brain stuck on old for each loop logic):
select inv_mast.item_id,
inv_mast.item_desc,
"YEAR" = year(oe_line.required_date),
"MONTH" = month(oe_line.required_date),
"ORDERS" = count(1),
"HITS" = (
select count(1)
from invoice_line
where invoice_line.order_no = oe_line.order_no
and invoice_line.oe_line_number = oe_line.line_no
and invoice_line.qty_shipped = oe_line.qty_ordered
)
from oe_line,
inv_mast,
inv_loc
where inv_mast.inv_mast_uid = oe_line.inv_mast_uid
and inv_mast.delete_flag = 'N'
and inv_mast.inv_mast_uid = inv_loc.inv_mast_uid
and inv_loc.location_id = '101'
and year(oe_line.required_date) = '2015'
and month(oe_line.required_date) = '8'
group by inv_mast.item_id,
inv_mast.item_desc,
year(oe_line.required_date),
month(oe_line.required_date)
order by inv_mast.item_id
To me it would seem like you could rewrite the query to use a left join on the invoice_line table instead. Without any proper test data I can't guarantee it is correct, but I think it should be.
Besides the left join I also changed to explicit joins and moved the aliases as I don't think MySQL supports the alias = column syntax.
select inv_mast.item_id,
inv_mast.item_desc,
year(o.required_date) as "YEAR",
month(o.required_date) as "MONTH",
count(1) as "ORDERS",
count(invoice_line.order_no) as "HITS"
from oe_line o
join inv_mast on inv_mast.inv_mast_uid = o.inv_mast_uid
join inv_loc on inv_mast.inv_mast_uid = inv_loc.inv_mast_uid
left join invoice_line on invoice_line.order_no = o.order_no
and invoice_line.oe_line_number = o.line_no
and invoice_line.qty_shipped = o.qty_ordered
where inv_mast.delete_flag = 'N'
and inv_loc.location_id = '101'
and year(o.required_date) = '2015'
and month(o.required_date) = '8'
group by inv_mast.item_id,
inv_mast.item_desc,
year(o.required_date),
month(o.required_date)
order by inv_mast.item_id;

Conditional Join: don't join on null

I have relationships that might not necessarily exist (they could be optional i.e. null); for example, a image may not have an address so it may be null.
I am unsure how to not return all null values.
Is there some condition I can put in place on the join that says if the address is null don't do a join and don't return all the null columns?
SELECT im.title, im.alias_title, im.description, im.main_image, im.hits,
im.show_comment, im.created_on, im.date_taken, im.account_type_id,
c.make, c.model, ad.address_line_1, ad.address_line_2,
spc.state_province_county, tvc.town_village_city, co.country,
ge.latitude, ge.longitude, ge.zoom, ge.yaw, ge.pitch,
us.first_name, us.surname, us.user_set_online, ut.username,
ut.account_type_id, aty.`type`, ufy.realname, ufy.location,
ufy.location, ufy.account_type_id
FROM image im
INNER JOIN user us
ON im.user_id = us.id
LEFT JOIN user_type ut
ON us.id = ut.user_id
LEFT JOIN user_flickr_youtube ufy
ON ut.id = ufy.user_type_id
LEFT JOIN account_type aty
ON ut.account_type_id =aty.id
LEFT JOIN address ad
ON im.address_id = ad.id
LEFT JOIN state_province_county spc
ON ad.state_province_county_id = spc.id
LEFT JOIN town_village_city tvc
ON ad.town_village_city_id =tvc.id
LEFT JOIN country co
ON ad.country_id =co.id
LEFT JOIN geolocation ge
ON im.geolocation_id = ge.id
LEFT JOIN camera c
ON im.camera_id = c.id
WHERE im.alias_title = 'test'
AND im.approved = 'Yes'
AND im.visible = '1'
LIMIT 1;
Is there some condition i can put in place on the join that says if the address is null dont do a join and dont bring me back all the null columns
Yes; you can run a JOIN instead of a LEFT JOIN. But that won't simply exclude the address if it is NULL, it will ignore the whole row altogether.
Usually this kind of situation is either handled by supplying a default value, possibly empty, for example directly in MySQL
SELECT
...COALESCE(ad.address_line_1,'(no address)') AS address_line_1,
COALESCE(ad.address_line_2,'') AS address_line_2, ...
or it is handled by the application:
if row['address_line_1']:
result = result + ("<td class=\"address\">%s</td>" % ( row['address_line_1'] ))
...
This also because a query could potentially return not one record, but several, and of these, some might have a NULL colum and some might not.
UPDATE
There is a way, but it's likely to make milk go sour in cows fifty miles downrange.
This is a proof of concept, on a MUCH smaller query and table, and takes advantage of the possibility of dynamically building a query.
First of all we have our query WHERE condition, here represented by "id = 1". We want to have the name column if the name column is not NULL.
SELECT #address := COALESCE(MIN(',name'),'') FROM client WHERE name IS NOT NULL AND id = 1;
This will return an empty string if the selected column is NULL. Otherwise it will return a comma and the name of that column.
This is the statement that in your case will be humongous, given your query. It contains the same WHERE as before, without the request that the name be NULL. And the field list is now dynamic.
SELECT #string := CONCAT('SELECT id', #address, ' FROM client WHERE id = 1');
Except that #string is, well, a string. To execute it as a query we do
PREPARE query FROM #string;
EXECUTE query;
DEALLOCATE PREPARE query;
How this might interact with your application, I do not dare fathom. I have tried an implementation in PHP on an expendable VM :-), cycling between the values of 1 and 3 (one row has a NULL name, one hasn't).
<?php
// Connect to this VM's local DB
mysql_connect('localhost','root','') or die("Cannot connect");
mysql_select_db('test');
foreach(array(1, 3) as $id)
{
mysql_query("SELECT #address := COALESCE(MIN(',name'),'') FROM client WHERE name IS NOT NULL AND id = $id;");
mysql_query("SELECT #string := CONCAT('SELECT id', #address, ' FROM client WHERE id = ', $id);");
mysql_query("PREPARE query FROM #string;");
$exec = mysql_query("EXECUTE query;");
while($tuple = mysql_fetch_assoc($exec))
{
print implode(" | ", $tuple) . "\n";
}
mysql_query("DEALLOCATE PREPARE query;");
}
?>
The answer seems to indicate it's working:
1 | Rossi
3
(I wouldn't have been surprised if it returned something like 'Ia! Cthulhu fhtagn!').

mySQL updating but also not updating

I am writing in mySQL/PHP, but have this problem I cannot get my head around. This one PHP script contains two SQL statements. What I am trying to do is update a sports league table (a SQL table called tblrank - which contains many league tables, separated by TableID), and to indicate if the team has gone up or down since the last time it was updated. This first bit of code is bit clunky and probably could've been written better (I could do it in about 6 lines in MSSQL), it works by counting the number of teams that are ranked lower than it, then adding one. It appears to work... sort of, as I will explain later.
update tblrank AS r
set Rank = 1 + (select count(*) from
(select r2.teamID
from tblrank r2
inner join tblrank r3
where r3.TableID = r2.TableID and r3.TableID = $tableid
and (r3.Points > r2.Points
or (r3.Points = r2.Points and r3.TieBreaker > r2.TieBreaker))) as duh
where duh.teamID = r.teamID
and duh.TableID = r.TableID
and r.TableID = $tableid
Then, this bit of code is run to pick the image to display.
update tblrank
set image = case when Rank < LastRank then 'up.png'
when Rank > LastRank then 'down.png'
else 'nomove.png' end
where TableID = $tableid
Now if I run this for $tableid = 1, it works just fine. But, if I run it for $tableid = 2, then all Ranks in the whole of tblRank are set to 1. (Before running it for $tableid = 2, all the ranks are 1, except for records where TableID =1 ). This is obviously not what I want.
Both statements are inside a if(mysql_query($sql)) conditional, so I can verify if they have executed or not.
I use MS SQL a lot more than mySQL so I'm not an expert - can anyone help me with this as I'm baffled! I have checked that both chunks of code do execute. There is no other SQL executed between the two.
Well, I've fixed it, I'm not sure what was wrong, but I've split the update statement up, with a temporary table called rankcount being created and populated first. I did this to make it easier to see what was going on.
create temporary table rankcount (TableID int, UserID int)");
insert into rankcount (TableID, UserID)
select r2.TableID, r2.UserID
from tblRank r2
inner join tblRank r3
where r3.TableID = r2.TableID and r3.TableID = $tableid
and (r3.Points > r2.Points
or (r3.Points = r2.Points and r3.TieBreakerOne > r2.TieBreakerOne))
Then this works...
update userEntryTableRank r
set Rank = 1 + (select count(*) from rankcount rc
where rc.UserID = r.UserID and rc.TableID = r.TableID)
where r.TableID = $tableid
:)
Here's how I'd do it. First initialize all the Rank values to zero.
UPDATE tblrank SET Rank = 0;
UPDATE tblrank r1 JOIN tblrank r2 ON r1.TableID = r2.TableID
SET r1.Rank = r1.Rank + 1
WHERE r1.Points > r2.Points
OR (r1.Points = r2.Points AND r1.TieBreaker > r2.TieBreaker)
The join naturally matches each row r1 to the set of rows r2 with the same table and team, and a lower score. Then it increments Rank by 1 for each of these matching rows.