Adding where 'id IN ' clause to Model with addCondition() - atk4

I was wondering if I can use addCondition() to add a where clause of the type: 'id IN (1,2,3,4)' to an existing model? according to the comments in Model_Tabel it would need to be possible if I give it an DB_dsql instance:
function addCondition($field,$cond=undefined,$value=undefined)
{
// You may pass plain "dsql" expressions as a first argument
if($field instanceof DB_dsql && $cond==undefined && $value==undefined){
$this->_dsql()->where($field);
return $this;
}
but If I try to do this:
$conflicto=$this->add('Model_Conflicto');
$conflicto->addCondition($this->api->db->dsql()->where('id IN'.$str));
I get an invalid SQL statement, which includes the whole select in the new where clause:
select SQL_CALC_FOUND_ROWS *,conflicto.id from conflicto where (select * where id IN(14, 60, 37, 39, 41, 43, 52, 57, 141, 144, 145, 183, 187, 14) )
I'm probably doing something really wrong here?
what would be the correct way to include 'where id IN ' to get this to work with my model?
Thanks,

Did you try
$model->addCondition('id',array(14,60,37,....));

Related

Using an alias to fill out data from one column for all rows

I am currently trying to pull data from an off the shelf product relating to documents in a library.
We're needing to look at two specific fields to narrow down what we need so we've found the database they live in but the "field" sits in the same column as the rest of the fields - only differentiated with a prop_id.
What I'm looking to achieve is below - For every instance that the Site is "NULL" - how can I fill this with for example, Ace Technologies based on the matching tocid which relates to the document. For all instances of 994 tocid site should just be the same, with no NULLs.
The query to create the stored procedure is as below
SELECT a.tocid,
a.prop_id,
b.prop_name,
a.str_val,
CASE WHEN a.prop_id = '57' AND a.tocid = a.tocid
THEN a.str_val
END as Site
FROM LaserRepository.dbo.toc AS d INNER JOIN
LaserRepository.dbo.propset AS c ON d.pset_id = c.pset_id INNER JOIN
LaserRepository.dbo.propval AS a INNER JOIN
LaserRepository.dbo.propdef AS b ON a.prop_id = b.prop_id ON d.tocid = a.tocid
WHERE (b.prop_id IN (59, 57, 127, 76, 86, 40, 686, 75, 637, 199, 85, 125, 128, 99, 675, 82, 688, 689))
AND (c.pset_id IN (4, 17, 24, 16, 7, 11))
AND c.pset_name = #template
END

How to give sum statement conditions in a case statement?

I want to use the sum function in the ELSE part of the CASE Statement. I need to give the SUM function conditions but I'm unable to do so.
I tried to use the GROUP BY Function within the sum function but it doesn't work.
CASE WHEN SectionCrossList = NULL THEN InstructorCredits ELSE InstructorCredits/Sum(Instructorcredits) GROUP BY (AcademicYear,SectionCrossList) END NumCreditSplit
Msg 156, Level 15, State 1, Line 220 Incorrect syntax near the keyword 'GROUP'.
Msg 102, Level 15, State 1, Line 221 Incorrect syntax near 'NumCreditSplit'.
You should use CTE or window function sum
SELECT AcademicYear,SectionCrossList,
CASE
WHEN SectionCrossList = NULL
THEN
InstructorCredits
ELSE
InstructorCredits/Sum(Instructorcredits)
end NumCreditSplit
FROM <yourTable>
group by AcademicYear,SectionCrossList

Data type mismatch on a join using dates

I have a function, lastday, that returns the last day of a given month. Code below...
Function lastday(dteAny As Date) As Date
lastday = DateSerial(Year(dteAny), Month(dteAny) + 1, 1) - 1
End Function
I am attempting to run a query you see below..
Select cc.cred_id, lastday(dateadd("m", 5, date())) as [due],
lastday(dateadd("m", 36, a.activity_date)) as [report]
from cred_core as cc
inner join activity as a on (a.cred_id = cc.cred_id and a.activity_type =
"recred sent" and lastday(dateadd("m", 5, date())) = lastday(dateadd("m",
36, a.activity_date)) )
I get an error alerting me of a data type mismatch on the second half of the join, where I use the lastday function.
I tried surrounding the lastday function with the # delimeters hoping that it would convert it to something it could actually compare, but I assume those delimeters don't work on functions?
Any idea what I can do to solve this problem? activity_date is, of course, a date/time data type. Using Access 2007-2013.
Those type of joins are not supported in Access. Try to set the dates in criteria.
SELECT cc.cred_id, lastday(DateAdd("m", 5, Date())) As [due],
lastday(DateAdd("m", 36, a.activity_date)) As [report]
FROM cred_core as cc INNER JOIN activity As a ON (a.cred_id = cc.cred_id)
WHERE a.activity_type = "recred sent"
AND lastday(DateAdd("m", 5, date())) = lastday(DateAdd("m", 36, a.activity_date));

SQL Server if Statement >=

These are the questions:
Write a script that will give you six columns of output.
a) the customer first name
b) the customer last name
c) the order number
d) the item name
e) the discount
f) a column with the following words for the discount----either NONE, LESS THAN TWENTY, LESS THAN FORTY, or MORE THAN FORTY
I have done this:
SELECT
Customers.FirstName,
Customers.LastName,
Orders.OrderID,
Products.ProductName,
OrderItems.DiscountAmount,
IF(OrderItems.DiscountAmount = 0, 'NONE', IF(OrderItems.DiscountAmount <= 20, 'LESS THAN TWENTY', IF(OrderItems.DiscountAmount <= 40, 'LESS THAN FORTY', 'MORE THAN FORTY'))) AS discount
FROM
Customers
INNER JOIN
Orders ON (Customers.ShippingAddressID = Orders.ShipAddressID
AND Customers.BillingAddressID = Orders.BillingAddressID)
INNER JOIN
OrderItems ON (Orders.OrderID = OrderItems.OrderID)
INNER JOIN
Products ON (OrderItems.ProductID = Products.ProductID)
It gives me an error on my if statement? Can you see what I'm doing wrong?
This is the database I'm using:
Error messages:
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'IF'.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ','.
Your database diagram and errors suggest you are using Microsoft SQL Server, not MySQL. The IF command/expression/keyword is not standardized and implemented differently in different platforms.
Therefore you should not use MySQL proprietary IF-syntax but the MSSQL proprietary IIF-syntax. IF is a control-of-flow statement in MSSQL.
This is SQL Server. There is no if statement. Use case:
(CASE WHEN OrderItems.DiscountAmount = 0 THEN 'NONE'
WHEN OrderItems.DiscountAmount <= 20 THEN 'LESS THAN TWENTY'
WHEN OrderItems.DiscountAmount <= 40 THEN 'LESS THAN FORTY'
ELSE 'MORE THAN FORTY'
END) AS discount
Use "case..when" statement here. Usage link here: case statement
Case
when OrderItems.DiscountAmount = 0 then 'NONE'
when OrderItems.DiscountAmount <= 20 then 'LESS THAN TWENTY'
when OrderItems.DiscountAmount <= 40 then 'LESS THAN FORTY'
else 'MORE THAN FORTY'
End AS discount

SQLAlchemy: Omit attribute in INSERT column list

How do I omit a table attribute from being included upon insertion?
I have the following class:
class PurchaseOrder(Base):
__tablename__ = 'PPurchaseOrder'
id = Column('PurchaseOrderId', Integer, Sequence('transactions_id_seq'), primary_key = True)
code = Column(String(50))
ris_id = Column('RisId', Integer, ForeignKey('BRIS.RisId'))
creator = Column('CreatedBy', String(256))
created = Column('CreateDate', DateTime)
date_needed = Column('DateNeeded', DateTime)
timestamp = Column('Timestamp', TIMESTAMP)
supplier_items = AssociationProxy('purchase_order_items', 'supplier_item', creator='SupplierItem')
purchase_order_items = relationship('PurchaseOrderItem', cascade="all, delete, delete-orphan",
backref='purchase_order', lazy='joined')
def __init__(self, code, created, date_needed):
self.code = code
self.creator = 'Marco'
self.created = created
self.date_needed = date_needed
When trying to add an object, it does the following insert statement:
2011-06-30 22:37:26,953 INFO sqlalchemy.engine.base.Engine INSERT INTO [PPurchaseOrder] (code, [RisId], [CreatedBy], [CreateDate], [DateNeeded], [Timestamp]) OUTPUT inserted.[PurchaseOrderId] VALUES (?, ?, ?, ?, ?, ?)
2011-06-30 22:37:26,953 INFO sqlalchemy.engine.base.Engine ('POF000872008', None, 'Marco', datetime.datetime(2008, 5, 27, 8, 47, 18), datetime.datetime(2008, 5, 28, 15, 48), None)
I want the Timestamp column out so that the insert statement would look like this:
2011-06-30 22:37:26,953 INFO sqlalchemy.engine.base.Engine INSERT INTO [PPurchaseOrder] (code, [RisId], [CreatedBy], [CreateDate], [DateNeeded]) OUTPUT inserted.[PurchaseOrderId] VALUES (?, ?, ?, ?, ?, ?)
2011-06-30 22:37:26,953 INFO sqlalchemy.engine.base.Engine ('POF000872008', None, 'Marco', datetime.datetime(2008, 5, 27, 8, 47, 18), datetime.datetime(2008, 5, 28, 15, 48))
IIRC, providing default or server_default argument will move the value generation to the server side. See here for more

Resources