confusing query [duplicate] - sql

This question already has answers here:
Grouping WHERE clauses in Codeigniter
(6 answers)
Closed 9 years ago.
I am really sorry to ask this silly question but I am a bit confused about the query.
$this->db->select('user_package.status,user_package.remaining,user_package.date,package.pname,package.pcount');
$this->db->join('user_package','user_package.pid=package.pid','left');
$this->db->where('user_package.status','Active');
$this->db->or_where('user_package.status','Pending');
$this->db->where('user_package.uid',$rows->uid);
$user_package=$this->db->get('package');
and here is the similar query in mysql panel which i run for result to check the out put
Select up.status,up.remaining,up.date,p.pname,up.uid,p.pcount FROM user_package up LEFT JOIN package p ON up.pid=p.pid where up.status="Active" OR up.status="Pending" AND up.uid=7
and here is the output of the query
What i want is that find only those rows whose uid is 7 and check if their status is Pending or Active. but the output is different. it takes out Pending and Active records with 7 and 8 uid.

Instead of using $this->db->where try using $this->db->where_in
This should limit the results to only the IDs you require.
e.g.
$this->db->select('user_package.status,user_package.remaining,user_package.date,package.pname,package.pcount');
$this->db->join('user_package','user_package.pid=package.pid','left');
$this->db->where('user_package.status','Active');
$this->db->or_where('user_package.status','Pending');
$this->db->where_in('user_package.uid',$rows->uid);
$user_package=$this->db->get('package');

You have to do it like this
$select = array(
'up.status',
'up.remaining',
'up.date',
'p.pname',
'up.uid',
'p.pcount'
);
$status = array('Active','Pending');
$this->db
->select($select)
->from('user_package up')
->join('package p','up.pid = p.pid','left')
->where_in('up.status',$status)
->where('up.uid',$rows->uid)
->get();

Related

How can I update this column with an aggregate function? [duplicate]

This question already has answers here:
SQL update query using joins
(13 answers)
Closed 2 years ago.
I calculated a sum for OREB that is unique to every pair of GameId and TeamId
using this query,
Query Image.
select DGAM.GameID, HomeTeamID, SUM(OREB) as OREB_Home
from dimGames as DGAM
join dimPerformance as DPERF on DGAM.GameID = DPERF.GAME_ID and DGAM.HomeTeamID = DPERF.TEAM_ID
group by DGAM.GameID, HomeTeamID
Using the results of this query I want to update the null column in this table
Table called dimGames.
How can I write an update statement to accomplish this?
Adapting this answer, I think you want:
with agg as (
select DGAM.GameID, HomeTeamID, SUM(OREB) as OREB_Home
from dimGames as DGAM
join dimPerformance as DPERF on DGAM.GameID = DPERF.GAME_ID and DGAM.HomeTeamID = DPERF.TEAM_ID
group by DGAM.GameID, HomeTeamID
)
UPDATE dimGames dg
SET dg.OREB_home = agg.OREB_Home
FROM agg
WHERE dg.GameId = agg.GameId and dg.HomeTeamId = agg.HomeTeamId

MSSQL WHERE YEAR clause returning all dates instead of date specified [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Junior here with no one to help me but the void of the internet and my googling skills are mediocre at best.
The following syntax returns the information I need however, it's giving me ALL dates in the table and ignoring the WHERE clause. There are 3 tables I'm trying to Join: 'ProductHistory' is the main one, with one column needed from 'Product' and a date field from the third table 'MainJobDetails'.
SELECT [ProductHistory].[Type]
,[ProductHistory].[Ref]
,[ProductHistory].[JobNo]
,[ProductHistory].[Quantity]
,[ProductHistory].[PurchasePrice]
,[ProductHistory].[UnitPrice]
,[ProductHistory].[UnitDesc]
,[ProductHistory].[ProductID]
,[ProductHistory].[Location]
,[ProductHistory].[UserID]
,[Product].[Description]
,[Product].[StkRef1]
,[MainJobDetails].[DespatchDate]
FROM [ProductHistory]
JOIN [Product] ON [ProductHistory].[ProductID] = [Product].[ID]
JOIN [MainJobDetails] ON [ProductHistory].[JobNo] = [MainJobDetails].[JobNo]
WHERE YEAR([MainJobDetails].[DespatchDate]) = 2020
AND MONTH([MainJobDetails].[DespatchDate]) = 11
AND [ProductHistory].[Type] = 5
OR [ProductHistory].[Type] = 6
ORDER BY [MainJobDetails].[DespatchDate] DESC
I've tried changing the WHERE clause to:
WHERE [MainJobDetails].[DespatchDate] BETWEEN '2020/10/31' AND '2020/11/30'
But it made no difference.
This is another similar query I've used previously and it works fine:
SELECT [MainJobDetails].[JobNo]
,[MainJobDetails].[EstimateHeaderRef]
,[MainJobDetails].[InvoiceCustomerName]
,[MainJobDetails].[JobDesc]
,[MainJobDetails].[DespatchDate]
,[FinishingInput].[Code]
,[FinishingInput].[Description]
,[FinishingInput].[Runs]
,[FinishingInput].[Timedb]
FROM [MainJobDetails]
LEFT JOIN [FinishingInput]
ON [MainJobDetails].[EstimateHeaderRef]=[FinishingInput].[EstimateHeaderRef]
WHERE [MainJobDetails].[DespatchDate] BETWEEN '2020/11/01' AND '2020/12/31'
ORDER BY [MainJobDetails].[DespatchDate] DESC
What am I getting wrong in the first statement?
Many thanks in advance for your help.
Put the OR condition within parentheses:
WHERE
YEAR([MainJobDetails].[DespatchDate]) = 2020
AND MONTH([MainJobDetails].[DespatchDate]) = 11
AND ([ProductHistory].[Type] = 5 OR [ProductHistory].[Type] = 6)
--^-- here and here --^--
Why you need that is because OR has lower priority than AND in logical expressions. So your original code (without the parentheses) is equivalent to:
WHERE
(
YEAR([MainJobDetails].[DespatchDate]) = 2020
AND MONTH([MainJobDetails].[DespatchDate]) = 11
AND ([ProductHistory].[Type] = 5
)
OR [ProductHistory].[Type] = 6
You can see that this allows any product with Type 6, regardless of other conditions.
I would also suggest further optimizations:
use direct filtering on the date rather than applying date functions no the stored value - this is much more efficient
use IN instead of ORed conditions
So:
WHERE
[MainJobDetails].[DespatchDate] >= '20201101'
AND [MainJobDetails].[DespatchDate] < '20201201'
AND [ProductHistory].[Type] IN (5, 6)

SQL Error (Missing Operator) In query operation error [duplicate]

This question already has answers here:
SQL multiple join statement
(3 answers)
Closed 3 years ago.
I am building an application and using MS Access as database(Normally I don't use MS Access but the client needs service-less database). Anyways, I have multiple tables and using joins, I am trying to acquire output, here is the query
SELECT tblSalesDetail.Sales_Details_ID, tblProduct.Product_Name,
tblSalesDetail.Rate, tblSalesDetail.Quantity, tblSalesDetail.TaxableAmount,
tblSalesDetail.TaxableAmount + tblSalesDetail.CGST_Amt +
tblSalesDetail.SGST_Amt + tblSalesDetail.IGST_Amt AS TotalAmt
FROM tblSalesMain
INNER JOIN tblSalesDetail
ON tblSalesMain.Sales_Main_ID = tblSalesDetail.Sales_Main_ID
INNER JOIN tblProduct
ON tblSalesDetail.Product_ID = tblProduct.Product_ID
WHERE tblSalesMain.Sales_Main_ID=1
This query works perfectly in SQL Server but having the following error when trying to run in MS Access with text selected at _Name at tblProduct.Product_Name in the first line.
I tried by changing or removing that column, but it's not working.
I tried StackOverflow answers posted but none of them worked by the way. So it's not like I am directly posting a question here without trying to solve this.
I know this might be a simple problem but I am stuck. Let me know workaround this.
I know this is off the above topic but can anyone suggest me good service-less, localdb (not mdf based SQL Db) for visual studio?
Regards
MS Access is quite finicky about syntax. Try this:
SELECT tblSalesDetail.Sales_Details_ID, tblProduct.Product_Name, tblSalesDetail.Rate,
tblSalesDetail.Quantity, tblSalesDetail.TaxableAmount,
(tblSalesDetail.TaxableAmount + tblSalesDetail.CGST_Amt + tblSalesDetail.SGST_Amt + tblSalesDetail.IGST_Amt) AS TotalAmt
FROM (tblSalesMain INNER JOIN
tblSalesDetail
ON tblSalesMain.Sales_Main_ID = tblSalesDetail.Sales_Main_ID
) INNER JOIN
tblProduct
ON tblSalesDetail.Product_ID = tblProduct.Product_ID WHERE tblSalesMain.Sales_Main_ID = 1

Select max(date) in Firebird [duplicate]

This question already has answers here:
Select first row in each GROUP BY group?
(20 answers)
Firebird Query- Return first row each group
(4 answers)
Closed 5 years ago.
I have a table in a Firebird 2.5 Database where I have to find out the latest entry for one specific articel no. Therefore the table has a colum "date".
I tried several queries but I always get more than 1 entry for the article no.
I used the following query:
select max(lp.datum) as Wareneingangsdatum, lp.nr, lp.artikelnr, lp.belegnr, lf.nr, lf.name, lf.plz, lf.ort, lf.laenderkuerzelnr, lae.bezeichnung
from lagerprotokoll lp
left join waeinpos we on we.idnr = lp.belegposidnr
left join waein we1 on we1.nr = we.nr
left join liefrant lf on lf.nr = we1.lieferantnr
left join laender lae on lae.nr = lf.laenderkuerzelnr
where lp.belegtyp = 14
group by 2,3,4,5,6,7,8,9, 10
order by 1 desc
How can I achieve the result so that I can only get the latest entry for one specific article in the table.

PDO connection select last id from table [duplicate]

This question already has answers here:
Get the last insert id with doctrine 2?
(8 answers)
Closed 6 years ago.
I am trying to get the last id from my selected table. I know lastinsertid(); sould work but it keeps returning a 0. What am i doing wrong here?
public function getlastadded()
{
$Sql = 'SELECT * FROM agendaevent';
$Stm = $this->getEntityManager()->getConnection()->prepare($Sql);
$Stm->execute();
return $this->getEntityManager()->getConnection()->lastInsertId('id');
}
ps i am working in symfony if that makes a difference.
Try this, friend:
"SELECT * FROM agendaevent ORDER BY id DESC LIMIT 1"