this is my code:
SELECT SUM
(CASE
WHEN (dbo.EMBARQUE.EmbEst) = 0 THEN
0
WHEN (dbo.EMBARQUE.EmbEst) = 3 THEN
0
WHEN (dbo.EMBARQUE.EmbEst) = 6 THEN
0
WHEN (dbo.EMBARQUE.EmbEst) = 7 THEN
CASE
WHEN (SELECT COUNT (dbo.CUMPLIDO.CumpCod) from dbo.CUMPLIDO where dbo.CUMPLIDO.EmbCod = dbo.EMBARQUE.EmbCod and dbo.CUMPLIDO.CumpVol = 0) > 0 THEN
0
ELSE
dbo.EMBARQUE.EmbVol
END
ELSE
dbo.EMBARQUE.EmbVol
END) FROM dbo.EMBARQUE
You have a subquery inside sum(). That isn't allowed. This version uses a subquery to calculate the flag you want and then moves the filtering logic to the WHERE clause:
SELECT SUM(e.EmbVol)
FROM (SELECT e.*,
(CASE WHEN EXISTS (SELECT 1 FROM dbo.CUMPLIDO c WHERE c.EmbCod = e.EmbCod AND c.CumpVol = 0)
THEN 1 ELSE 0
END) as is_CumpVol_0
FROM dbo.EMBARQUE e
WHERE e.EmbEst NOT IN (0, 3, 6)
) e
WHERE e.EmbEst <> 7 OR is_CumpVol_0 = 0;
I have this query
SELECT DISTINCT IP.IRId
FROM cmp.NPTable NP
INNER JOIN IPTable IP ON IP.IPtId = NP.IPd
LEFT JOIN IPCTable IPC ON IPC.IPId = NP.IPId
WHERE NP.PCN Id = #PCNId
AND (IP.IsCompliant = 1 AND IPC.CheckId = 1) OR (IP.IsCompliant = 0 AND IPC.CheckId = 1)
This is not working correcty
THe IPTable either has IsCompliant = null, 0 or 1 -- this is basically to indicate true or false
IPC.CheckId is either 1 or 2 -- this is the primary ID in this table
The only match integer match is the value 1 between the two tables.
I only want to bring back rows if IsCompliant = 1 and CheckId = 1
else if IsCompliant is null or 0, then CheckId = 2.
The clause I added to my where which is
AND (IP.IsCompliant = 1 AND IPC.CheckId = 1) OR (IP.IsCompliant = 0 AND IPC.CheckId = 1)
This does not work. Help. Will be very appreciated.
Thanks
WHERE NP.PCN Id = #PCNId
AND
(
(IP.IsCompliant = 1 AND IPC.CheckId = 1) OR
((IP.IsCompliant is null or IP.IsCompliant = 0) AND IPC.CheckId = 2)
)
This is the failed query.
Failed SQL:
Select SO_DOC_CD,
SO_DOC_DEPT_CD,
SO_DOC_ID,
SO_DOC_VERS_NO,
SR_DOC_CD,
SR_DOC_DEPT_CD,
SR_DOC_ID,
SR_DOC_VERS_NO,
DOC_LAST_DT,
DOC_PHASE_CD,
DOC_PHASE_CD_SO,
DOC_STA_CD,
VEND_CUST_CD,
DOC_SH_DSCR,
RESP_STA,
RESP_TM_WEB,
"Created By",
QRY_SRCH_STRING,
SO_DOC_REF,
SR_DOC_REF,
SO_DOC_FUNC_CD,
RESP_DT_ADV,
SO_SR_QRY.RESP_STA_ORD,
APPL_STA_CD,
SO_SR_QRY.APPL_STA_CD_ORD,
SO_SR_QRY.AWD_FL
From (
Select SO_DOC_HDR.DOC_CD As SO_DOC_CD,
SO_DOC_HDR.DOC_DEPT_CD As SO_DOC_DEPT_CD,
SO_DOC_HDR.DOC_ID As SO_DOC_ID,
SO_DOC_HDR.DOC_VERS_NO As SO_DOC_VERS_NO,
SO_DOC_HDR.SO_CLSNG_DT As SO_CLSNG_DT,
SO_DOC_HDR.SO_CLSNG_TM As SO_CLSNG_TM,
SR_DOC_HDR.DOC_CD As SR_DOC_CD,
SR_DOC_HDR.DOC_DEPT_CD As SR_DOC_DEPT_CD,
SR_DOC_HDR.DOC_ID As SR_DOC_ID,
SR_DOC_HDR.DOC_VERS_NO As SR_DOC_VERS_NO,
SR_DOC_HDR.DOC_LAST_DT As DOC_LAST_DT,
SR_DOC_HDR.DOC_PHASE_CD As DOC_PHASE_CD,
SR_DOC_HDR.APPL_STA_CD As APPL_STA_CD,
SO_DOC_HDR.DOC_PHASE_CD As DOC_PHASE_CD_SO,
SR_DOC_HDR.DOC_STA_CD As DOC_STA_CD,
SR_DOC_HDR.VEND_CUST_CD As VEND_CUST_CD,
SO_DOC_HDR.DOC_SH_DSCR As DOC_SH_DSCR,
SR_DOC_HDR.RESP_STA As RESP_STA,
SR_DOC_HDR.RESP_TM_WEB As RESP_TM_WEB,
SR_DOC_HDR.DOC_CREA_USID As "Created By",
SR_DOC_HDR.QRY_SRCH_STRING As QRY_SRCH_STRING,
SO_DOC_HDR.DOC_REF As SO_DOC_REF,
SR_DOC_HDR.DOC_REF As SR_DOC_REF,
SO_DOC_HDR.DOC_FUNC_CD As SO_DOC_FUNC_CD,
SR_DOC_HDR.RESP_DT_ADV As RESP_DT_ADV,
Case When RESP_STA = 7 Then 1
When RESP_STA = 1 Then 2
When RESP_STA = 2 Then 4
When RESP_STA = 3 Then 3
When RESP_STA = 4 Then 5
When RESP_STA = 5 Then 6
End As RESP_STA_ORD,
Case When APPL_STA_CD = 5 Then 1
When APPL_STA_CD = 4 Then 2
When APPL_STA_CD = 1 Then 3
When APPL_STA_CD = 3 Then 4
When APPL_STA_CD = 6 Then 5
When APPL_STA_CD = 2 Then 6
When APPL_STA_CD = 7 Then 7
End As APPL_STA_CD_ORD,
Case When Exists (
Select 1
From SR_DOC_COMMLN
Where SR_DOC_COMMLN.SO_DOC_ID = SR_DOC_HDR.SO_DOC_ID
And SR_DOC_COMMLN.SO_DOC_DEPT_CD = SR_DOC_HDR.SO_DOC_DEPT_CD
And SR_DOC_COMMLN.SO_DOC_VERS_NO = SR_DOC_HDR.SO_DOC_VERS_NO
And SR_DOC_COMMLN.SO_DOC_CD = SR_DOC_HDR.SO_DOC_CD
And (
SR_DOC_COMMLN.AWARD_CREATED = 1
Or SR_DOC_COMMLN.AWARD_FINALIZED = 1
)
) Then 1
Else 0
End As AWD_FL
From av3112jm1.dbo.SR_DOC_HDR SR_DOC_HDR,
av3112jm1.dbo.SO_DOC_HDR SO_DOC_HDR
Where SO_DOC_HDR.DOC_CD = SR_DOC_HDR.SO_DOC_CD
And SO_DOC_HDR.DOC_DEPT_CD = SR_DOC_HDR.SO_DOC_DEPT_CD
And SO_DOC_HDR.DOC_ID = SR_DOC_HDR.SO_DOC_ID
And SO_DOC_HDR.DOC_PHASE_CD = 3
) SO_SR_QRY
Where 1 = 1
And (
SR_DOC_HDR.DOC_VERS_NO = 1
And SR_DOC_HDR.DOC_DEPT_CD = '010'
And SR_DOC_HDR.DOC_CD = 'GFA'
And SR_DOC_HDR.DOC_ID = 'ESR09141700000000002'
)
Order By Case When SO_DOC_FUNC_CD = 3 Then 4
When SO_SR_QRY.AWD_FL = 1 Then 2
When Convert(DateTime, SO_CLSNG_DT, (108)) + Convert(DateTime, SO_CLSNG_TM, 114) <= '2017-09-20 02:44:23' Then 3
Else 1
End Asc,
SO_CLSNG_DT Desc,
SO_CLSNG_TM Desc;
The problem is that in your outer query, you're referencing SR_DOC_HDR.DOC_VERS_NO = 1, but your inner query has that column aliased as SR_DOC_VERS_NO.
You need to use the aliases given in the inner query.
Change your outer WHERE statement to:
Where 1 = 1
And (
SR_DOC_VERS_NO = 1
And SR_DOC_DEPT_CD = '010'
And SR_DOC_CD = 'GFA'
And SR_DOC_ID = 'ESR09141700000000002'
)
in your subquery you use twice the alias
sr_doc_hdr.doc_vers_no AS SR_DOC_VERS_NO
In MS Access 2003 I have a report with 4 possible scenarios
1.possible scenario:
If (Me.Ocjena1 = 2 Or 3 Or 4 Or 5) And (Me.Ocjena2 = 1) And IsNull(Me.Ocjena3) Then
Me.Konacna_ocjena = 1
End If
2.possible scenario
If (Me.Ocjena1 = 2 Or 3 Or 4 Or 5) And (Me.Ocjena2 = 2 Or 3 Or 4 Or 5) And IsNull(Me.Ocjena3) Then
Me.Konacna_ocjena = (Me.Ocjena1+Me.Ocjena2) /2
End If
3.possible scenario
If (Me.Ocjena1 = 2 Or 3 Or 4 Or 5) And l(Me.Ocjena3 =1) and (Me.Ocjena3 = 2 Or 3 Or 4 Or 5) Then
Me.Konacna_ocjena = (me.Ocjena1+me.Ocjena3) /3
End If
4.possible scenario
If (Me.Ocjena1 = 2 Or 3 Or 4 Or 5) And IsNull(Me.Ocjena2) And IsNull(Me.Ocjena3) Then
Me.Konacna_ocjena = Me.Ocjena1
End If
Sometimes is included all 4 scenarios. With this code work max 2 scenarios but 2 rest not working... Where am I wrong?
Would have to repeat the conditional field, like:
(Me.Ocjena1 = 2 Or Me.Ocjena1 = 3 Or Me.Ocjena1 = 4 Or Me.Ocjena1 = 5)
Consider:
With Me
If .Ocjena1 > 1 And .Ocjena1 < 6 Then
If .Ocjena2 = 1 And IsNull(.Ocjena3) Then
.Konacna_ocjena = 1
ElseIf .Ocjena2 > 1 And .Ocjena2 < 6 And IsNull(.Ocjena3) Then
.Konacna_ocjena = (.Ocjena1 + .Ocjena2) / 2
ElseIf .Ocjena2 = 1 And .Ocjena3 > 1 And .Ocjena3 < 6 Then
.Konacna_ocjena = (.Ocjena1 + .Ocjena3) / 3
ElseIf IsNull(.Ocjena2) And IsNull(.Ocjena3) Then
.Konacna_ocjena = .Ocjena1
End If
End With
I'm having problems with what I thought was a simple query to count records:
SELECT req_ownerid, count(req_status_lender) AS total6
FROM bor_requests
WHERE (req_status_lender = 0 AND req_status_borrower = 0) OR
(req_status_lender = 1 AND req_status_borrower = 1)
GROUP BY req_ownerid
HAVING req_ownerid = 70
I thought this would count all the records where (req_status_lender = 0 AND req_status_borrower = 0) and (req_status_lender = 1 AND req_status_borrower = 1) and then give me the total but it only gives me the total for either (req_status_lender = 0 AND req_status_borrower = 0) or (req_status_lender = 1 AND req_status_borrower = 1).
Any ideas what I'm doing wrong?
You should use the HAVING clause only to limit on something that's been aggregated in your query above - e.g. if you want to select all those rows where a SUM(....) or COUNT(...) is larger than say 5, then you'd use HAVING SUM(...) > 5
What you're doing here is a standard WHERE clause - add it there!
SELECT req_ownerid, count(req_status_lender) AS total6
FROM bor_requests
WHERE req_ownerid = 70
AND ((req_status_lender = 0 AND req_status_borrower = 0) OR
(req_status_lender = 1 AND req_status_borrower = 1))
GROUP BY req_ownerid