I decomposed a large table into five tables based on BCNF and I tried to join them all together to check if I lost something or not.
I used this sql statement in Sqlite3
Select
spart.Sno, spart.Sname,
cpart.Cno, CtoT.Cname,
CtoT.Tno,tpart.Tname,
scorepart.Degree
from spart, cpart, CtoT, tpart, scorepart
where
cpart.Cname = CtoT.Cname
spart.Sno = scorepart.Sno
CtoT.Tno = tpart.Tno
cpart.Cno = scorepart.Cno;
And I got Error: near "spart": syntax error
Could anybody help me out?
Try this:
Select
spart.Sno, spart.Sname,
cpart.Cno, CtoT.Cname,
CtoT.Tno,tpart.Tname,
scorepart.Degree
from spart, cpart, CtoT, tpart, scorepart
where
(cpart.Cname = CtoT.Cname)
and (spart.Sno = scorepart.Sno)
and (CtoT.Tno = tpart.Tno)
and (cpart.Cno = scorepart.Cno);
I'm not sure the parentheses are really needed. I've forgotten the syntax rules.
Related
So I have the following table:
And I'm trying to write a Query where I can send the code BR_BN as a variable in my WHERE clause
and if I get BR_BN then I want to retrieve the records with this code AND the records with the Code_FS RB02. On the other side when I get the value AB_CP, I want to include the recordes with the Code_FS RB01.
Here's the Query I've tried so far:
DECLARE #Code_OB VARCHAR(20) = 'BR_BN'
SELECT * FROM Dummy_AV
WHERE FK = 2
OR
(#Code_OB = 'BR_BN' AND Code_FS = 'RB02' AND Code_FS = #Code_OB)
But it doesn't work, it retrieves all the records regardless of the FK, and/or the #Code_FS.
How can I achieve this?
Thanks for the help.
You don't note the FK = 2 being needed, yet you have it in front of an OR in the WHERE clause. I think this is what you're after, if it isn't exactly what you're aiming for hopefully it gets you on the right track. For future questions, always helpful to paste your sample data as data instead of an image.
DECLARE #Code_OB VARCHAR(20) = 'BR_BN'
SELECT * FROM Dummy_AV
WHERE FK = 2 -- you will get all rows where this is true
OR
((#Code_OB = Code_OB AND Code_FS = 'RB02') OR (Code_OB = 'AB_CP' AND Code_FS = 'RB01')) -- you will get all rows where one of these is true
I am getting the titled error from a number of SQL Server views I am trying to create. They are modified from a MS Access database I'm upgrading to use SQL as the back end. See SQL below:
SELECT dbo.Site_Info_All.SiteID,
IIf(dbo.Site_Info_All.Fixed_Charge, N'Yes', N'No') AS [Fixed Charge],
dbo.Site_Info_All.Fixed_Charge_Date, dbo.MG_Definition.MG_Definition
FROM dbo.MG_Definition INNER JOIN
dbo.Site_Info_All ON dbo.MG_Definition.MG_DefinitionID =
dbo.Site_Info_All.MG_DefinitionID
It looks as though I'm being told that the Fixed_Charge field is not boolean, except that it is. I'm encountering this issue with multiple views. What am I doing wrong?
It's not boolean. It's a bit. :)
A bit column doesn't directly evaluate to true or false, the way a bool does in other languages. You actually have to compare it to another bit value to return the boolean value.
declare #bit bit = 1;
if (#bit) print '#bit was true'; -- this does not work
if (#bit = 1) print '#bit = 1 was true'; -- this works
What you want is:
... IIf(dbo.Site_Info_All.Fixed_Charge = 1, N'Yes', N'No') ...
SELECT dbo.Site_Info_All.SiteID,
IIf(dbo.Site_Info_All.Fixed_Charge = 1, N'Yes', N'No') AS [Fixed Charge],
dbo.Site_Info_All.Fixed_Charge_Date,
dbo.MG_Definition.MG_Definition
FROM dbo.MG_Definition
INNER JOIN dbo.Site_Info_All ON dbo.MG_Definition.MG_DefinitionID = dbo.Site_Info_All.MG_DefinitionID
In the end, I decided that IIF simply wasn't working for whatever reason. An answer to that would be very welcome, btw.
I used
SELECT dbo.Site_Info_All.SiteID,
CASE WHEN dbo.Site_Info_All.Fixed_Charge = 1
THEN N'Yes'
ELSE N'No'
END AS [Fixed Charge], dbo.Site_Info_All.Fixed_Charge_Date,
dbo.MG_Definition.MG_Definition
FROM dbo.MG_Definition INNER JOIN
dbo.Site_Info_All ON dbo.MG_Definition.MG_DefinitionID =
dbo.Site_Info_All.MG_DefinitionID
That gave me the results I was after, but only after pulling out what little hair I have left.
I am trying to create a MySQL statement that I can put in a php script to update the weight on a few thousand products in magento 1.9.
This is the statement I currently have:
UPDATE dp_catalog_product_entity_decimal AS ped JOIN dp_eav_attribute AS ea ON ea.entity_type_id = 10 AND ea.attribute_code = 'weight' AND ped.attribute_id = ea.attribute_id SET ped.value = 8 WHERE ped.entity_id = P1000X3;
It was partially taken from another post so I am not sure if it will work at all but I currently have the error "#1054 - Unknown column 'P1000X3' in 'where clause'"
I am not that great with sql joins and I dont really know the magento databse at all so any help to get this statement to work is much apreciated.
Thanks.
Matt
The correct SQL to update the weight of a product with a specific SKU in Magento is this (replace YOUR-MAGENTO-SKU with the sku of the item you want to update, and the 8 with the weight value).
UPDATE catalog_product_entity_decimal AS cped
JOIN eav_attribute AS ea ON ea.entity_type_id = (SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'catalog_product')
LEFT JOIN catalog_product_entity AS cpe ON cpe.entity_id = cped.entity_id
AND ea.attribute_code = 'weight'
AND cped.attribute_id = ea.attribute_id
SET cped.value = 8
WHERE cpe.sku = 'YOUR-MAGENTO-SKU';
In your case, you have the table prefix dp_, and the product sku P1000X3, so the correct SQL for you would be:
UPDATE dp_catalog_product_entity_decimal AS cped
JOIN dp_eav_attribute AS ea ON ea.entity_type_id = (SELECT entity_type_id FROM dp_eav_entity_type WHERE entity_type_code = 'catalog_product')
LEFT JOIN dp_catalog_product_entity AS cpe ON cpe.entity_id = cped.entity_id
AND ea.attribute_code = 'weight'
AND cped.attribute_id = ea.attribute_id
SET cped.value = 8
WHERE cpe.sku = 'P1000X3';
After running this, be sure to reindex your Magento Indexes.
Do your Magento tables have the prefix dp_? Make sure of this.
Also on this part:
WHERE ped.entity_id = P1000X3;
ped.entity_id will be an integer value (number).
I'm not sure where you got P1000X3, but using that won't work (it's a string value). Even so, strings should be wrapped with a single quotes ', like this:
'P1000X3';
About Reporting Service:
Here is my Query Code, and Result Return.
As you can see, at the same "DateTime" I have three values are "BATCH_NAME", "START_TIME", and "END_TIME", but it not order in row.
So please help me delete all elements that null value.
Sorry for my bad English. Thanks all.
Here is my image:
http://upanh.biz/images/2014/08/22/untitledNzblm.png
http://upanh.biz/images/2014/08/22/untitledJ23t5.png
Still hard to help you without knowing the actual tables and data, but I will give it a try.
I think you need to join StringSnapshot and SnapshotTag 3 times, because there are 3 different values you are looking for.
try something like this:
SELECT
Hist.DateTime,
BatchName = StringSnapshotBatchName.Value,
StartTime= ...,
EndTime= ...,
FROM
EventHistory Hist
JOIN StringSnapshot StringSnapshotBatchName
ON StringSnapshotBatchName.EventLogKey= Hist.EventLogKey
JOIN SnapshotTag SnapshotTagBatchName
ON SnapshotTagBatchName.SnapshotTagKey = StringSnapshotBatchName.SnapshotTagKey
AND SnapshotTagBatchName.TagName = 'BATCH_NAME'
JOIN StringSnapshot StringSnapshotStartTime
ON StringSnapshotStartTime .EventLogKey= Hist.EventLogKey
JOIN SnapshotTag SnapshotTagStartTime
ON SnapshotTagStartTime .SnapshotTagKey = StringSnapshotStartTime .SnapshotTagKey
AND SnapshotTagStartTime .TagName = 'START_TIME'
JOIN....
I am trying to translate a QueryExpression that is in some existing code into a T-SQL select statement.
I've run across the following statement and I'm having trouble understanding what they mean by a Natural Join:
linkEntity1.JoinOperator = JoinOperator.Natural;
Would this be equivalent to an Inner Join in T-SQL? Googling has not been much help.
Here's the rest of the QueryExpression Code:
QueryExpression query = new QueryExpression();
query.EntityName = "showinfo";
ColumnSet columns = new ColumnSet();
columns.Attributes = new String[] { "company" };
query.ColumnSet = columns;
query.Criteria = new FilterExpression();
query.Criteria.FilterOperator = LogicalOperator.And;
ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = "company";
condition1.Operator = ConditionOperator.NotNull;
query.Criteria.Conditions = new ConditionExpression[] { condition1 };
LinkEntity linkEntity1 = new LinkEntity();
linkEntity1.JoinOperator = JoinOperator.Natural;
linkEntity1.LinkFromEntityName = "show";
linkEntity1.LinkFromAttributeName = "showid";
linkEntity1.LinkToEntityName = "showintegration";
linkEntity1.LinkToAttributeName = "showcode";
linkEntity1.LinkCriteria = new FilterExpression();
linkEntity1.LinkCriteria.FilterOperator = LogicalOperator.And;
ConditionExpression condition2 = new ConditionExpression();
condition2.AttributeName = "showend";
condition2.Operator = ConditionOperator.Null;
linkEntity1.LinkCriteria.Conditions = new ConditionExpression[] { condition2 };
query.LinkEntities = new LinkEntity[] { linkEntity1 };
There is no equivalent in SQL Server of a natural join where table intersect is based on column names by the RDBMS.
I'm glad of that because it is at best ambiguous and at worst dangerous. JOINs should be explicit. Examples why:
having a InsertedBy column in both tables (quite common): should we have to prefix with the table name to remove ambiguity?
future DDL that add columns that change JOIN semantics
See
Natural join in SQL Server
SQL Server - lack of NATURAL JOIN / x JOIN y USING(field)
Edit:
It looks like natural join means "don't repeat the column in the output" (like USING in MySQL would do) according to the JoinOperator Enumeration.
If I understand this (debatable!) it's misleading. Especially when I read the "LeftOuter" narrative..
A natural join compares all columns in the two tables that have the same column names. It's equivalent to an inner join with the matching columns explicitly listed.
Yes - the natural join is inner join - so you can write:
select * from tab1, tab2 where tab1.col1 = tab2.col1
as
select * from tab1 inner join tab2 on tab1.col1 = tab2.col1