converting SQL to ACCESS - sql

here's a regular SQL statement:
SELECT SUM(CASE WHEN [Column2] = 'Cylinder' THEN 1 ELSE 0 END) as 'Cylinder count',
SUM(CASE WHEN [Column2] = 'Snap' THEN 1 ELSE 0 END) as 'Snap count',
SUM(CASE WHEN [Column2] = 'Tip' THEN 1 ELSE 0 END) as 'Tip count',
SUM(CASE WHEN [Column2] = 'Other' THEN 1 ELSE 0 END) as 'Other count'
FROM [TableName]
WHERE [Column1] = '1.9 QNS-Quantity Not Sufficient'
can you please convert it to ms-access??
based on this question:
https://stackoverflow.com/questions/3153829/question-on-complex-select-statement

Microsoft Access doesn't support CASE, but you can use IIF instead:
SELECT
SUM(IIF([Column2] = 'Cylinder', 1, 0)) as 'Cylinder count',
SUM(IIF([Column2] = 'Snap', 1, 0)) as 'Snap count',
SUM(IIF([Column2] = 'Tip', 1, 0)) as 'Tip count',
SUM(IIF([Column2] = 'Other', 1, 0)) as 'Other count'
FROM [TableName]
WHERE [Column1] = '1.9 QNS-Quantity Not Sufficient'
References
http://office.microsoft.com/en-us/access-help/iif-function-HA001228853.aspx
Case expressions in Access

Related

Sql query to calculate count of email sent from email open and email bounceback

So basically i have to calculate sents against subject line , Here is the query i have used:
Select distinct SubjectLine
,case when ([Activity] in ('Click','Opted In','Unsubscribe - All')) then count([EmailAddress]) else 0 end as 'TotalCT'
,case when ([Activity] in ('Click','Opted In','Unsubscribe - All')) then count(distinct[EmailAddress]) else 0 end as 'UniqueCT'
,case when ([Activity] = 'Open') then count([EmailAddress] ) else 0 end as 'TotalOpens'
,case when ([Activity] = 'Open') then count(distinct[EmailAddress]) else 0 end as 'UniqueOpens'
,case when ( [Activity] = 'Bounceback' ) then count([EmailAddress]) else 0 end as 'Bounces'
,case when ([Activity] in ('Open','Bounceback'))then count([EmailAddress]) else 0 end as 'Sends'
from xyz
Group by SubjectLine, Activity
I think you want conditional aggregation:
Select SubjectLine
sum(case when [Activity] in ('Click', 'Opted In', 'Unsubscribe - All') then 1 else 0) end as TotalCT,
sum(case when [Activity] in ('Click', 'Opted In', 'Unsubscribe - All') then 1 else 0 end) as UniqueCT,
sum(case when [Activity] = 'Open' then 1 else 0 end) as TotalOpens,
sum(case when [Activity] = 'Open' then 1 else 0 end) as UniqueOpens,
sum(case when [Activity] = 'Bounceback' then 1 else 0 end) as Bounces,
sum(case when [Activity] in ('Open', 'Bounceback') then 1 else 0 end) as Sends
from xyz
Group by SubjectLine;

How to do a Sum(case when) using JPA

I don't have much background in converting between SQL and JPA-like code so I was wondering how I would go about converting the following to JPA syntax? Or if it's even possible? Thanks in advance.
SELECT DLR_CD, COUNT(*),
SUM(CASE WHEN CMPLT_DT='0001-01-01' THEN 1 END),
SUM(CASE WHEN CMPLT_DT!='0001-01-01' THEN 1 END),
SUM(CASE WHEN YEAR(CMPLT_DT) = YEAR(CURRENT DATE)-1 THEN 1 END),
SUM(CASE WHEN YEAR(CMPLT_DT) = YEAR(CURRENT DATE) THEN 1 END),
SUM(CASE WHEN (CMPLT_DT IS NULL OR CMPLT_DT='0001-01-01') THEN A.RPR_LBR_HR ELSE 0 END)
FROM <db2 table 1> A
join <db2 table 2> B
on ANUN_IND='A'
AND B.PIP_PSP_NO=A.PIP_PSP_NO
--OPTIONAL PARAMS GO HERE
GROUP BY DLR_CD
FOR FETCH ONLY WITH UR;
Edit: updated with full, barely-modified query.
Using criteria api it would look something like this:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<YourPojo> cq = cb.createQuery(YourPojo.class);
Root<Entity1> root = cq.from(Entity1.class);
Join<Entity1,Entity2> join = root.join(Entity1_.entity2,JoinType.LEFT);
/*
* the join by ID is implicit when using the .join method, the second
* condition is added as follows (Since you don't indicate the alias,
* I assume it is a field from table 2):
*/
join.on(cb.equal(join.get(Entity2_.anunInd),'A'));
Calendar dayOne = Calendar.getInstance();
dayOne.set(1, 0, 1);
//Expression for first and second sum case
Expression<Long> caseDate = cb.<Long>selectCase().when(cb.equal(root.get(Entity1_.cmpltDt),dayOne.getTime()), 1L).otherwise(0L);
Expression<Long> sumDate = cb.sum(caseDate);
//Expression for third sum case
Expression<Long> caseDateWithYearM1 = cb.<Long>selectCase().when(
cb.equal(cb.function("YEAR", Long.class, root.get(Entity1_.cmpltDt)),
cb.sum(cb.function("YEAR", Long.class, cb.literal(new Date())),-1))
, 1L).otherwise(0L);
Expression<Long> sumDateWithYearM1 = cb.sum(caseDateWithYearM1);
//Expression for fourth sun case
Expression<Long> caseDateWithYear = cb.<Long>selectCase().when(
cb.equal(cb.function("YEAR", Long.class, root.get(Entity1_.cmpltDt)),
cb.function("YEAR", Long.class, cb.literal(new Date())))
, 1L).otherwise(0L);
Expression<Long> sumDateWithYear = cb.sum(caseDateWithYear);
Expression<Long> caseNew = cb.<Long>selectCase().when(cb.or(
cb.equal(root.get(Entity1_.cmpltDt),dayOne.getTime()),
cb.equal(root.get(Entity1_.cmpltDt),dayOne.getTime())),
root.get(Entity1_.rprLbrHr)).otherwise(0L);
Expression<Long> sumNew = cb.sum(caseNew);
cq.multiselect(
root.get(Entity1_.dlrCd),
cb.count(root.get(Entity1_.dlrCd)),
sumDate,
sumDate,
sumDateWithYearM1,
sumDateWithYear,
sumNew
);
cq.groupBy(root.get(Entity1_.dlrCd));
List<YourPojo> resultado = entityManager.createQuery(cq).getResultList();
Your Pojo has to have a constructor with the same parameters (order and type) as the multiselect method.
The result query:
select
entity1.DLR_CD as col_0_0_,
count(entity1.DLR_CD) as col_1_0_,
sum(case when entity1.CMPLT_DT=? then 1 else 0
end) as col_2_0_,
sum(case when entity1.CMPLT_DT=? then 1 else 0
end) as col_3_0_,
sum(case when extract(year from entity1.CMPLT_DT)=extract(year from ?)+-1 then 1 else 0
end) as col_4_0_,
sum(case when extract(year from entity1.CMPLT_DT)=extract(year from ?) then 1 else 0
end) as col_5_0_ ,
sum(case when entity1.CMPLT_DT is null or entity1.CMPLT_DT=? then entity1.RPR_LBR_HR else 0
end) as col_6_0_,
from
entity1
left join
entity2 on entity1.PIP_PSP_NO = entity2.PIP_PSP_NO and
entity2.ANUN_IND='A'
group by
entity1.DLR_CD

How do I replace null with 0 in SUM CASE WHEN statement?

I am using a CASE WHEN statement and a SUM function. I want to replace all nulls with 0.
I tried adding a 0 at the end of my statement.
This is my current code
select ID, PT_Number,
ORGANIZATION,
[Approved] = sum(case when PT_TYPE = 'Lend' then Approved END),
[Disbursed] = sum(case when PT_TYPE = 'Give' then Disbursed end),
[Repaid] = sum(case when PT_TYPE = 'Pay' then Payment end)
from TABLE1
group by ID, PT_Number,
ORGANIZATION
This is what I am trying
[Approved] = sum(case when PT_TYPE = 'Lend' then Approved END, 0),
[Disbursed] = sum(case when PT_TYPE = 'Give' then Disbursed end, 0),
[Repaid] = sum(case when PT_TYPE = 'Pay' then Payment end, 0)
Just add an else clause:
select ID, PT_Number,
ORGANIZATION,
[Approved] = sum(case when PT_TYPE = 'Lend' then Approved else 0 END),
[Disbursed] = sum(case when PT_TYPE = 'Give' then Disbursed else 0 end),
[Repaid] = sum(case when PT_TYPE = 'Pay' then Payment else 0 end)
from TABLE1
group by ID, PT_Number, ORGANIZATION;
You could also use coalesce() but that seems like overkill.

I Need to get a count of activities by month grouped by a Department

I am trying to get a count of activities by month and group them by a department name. The report is based on a date parameter that I input to the query "#rptDate". What I have below works, but I'm guessing there is a better way of doing the same thing and hoping someone can shed some light.
Select Count(*) As Month1Count, 0 As Month2Count, 0 As Month3Count,
0 As Month4Count, 0 as Month5Count, 0 as Month6Count, 0 as Month7Count,
0 as Month8Count, 0 as Month9COunt, 0 as Month10Count, 0 as Month11Count,
0 as Month12Count, DepartmentName,ActivityDescription
from reports.WorkOrders
WHERE Month(BeginDate) = Month(#rptDate) and Year(BeginDate) = Year(#rptDate)
Group By DepartmentName, ActivityDescription
UNION
Select 0 As Month1Count, COUNT(*) As Month2Count, 0 as Month3Count,
0 as Month4Count, 0 as Month5Count, 0 as Month6Count, 0 as Month7Count,
0 as Month8Count, 0 as Month9COunt, 0 as Month10Count, 0 as Month11Count,
0 as Month12Count, DepartmentName,ActivityDescription
from reports.WorkOrders
WHERE Month(BeginDate) = Month(#rptDate) + 1 and Year(BeginDate) = Year(DateAdd(month,1,#rptDate))
Group By DepartmentName, ActivityDescription
UNION
Select 0 As Month1Count, 0 As Month2Count, Count(*) as Month3Count,
0 as Month4Count, 0 as Month5Count, 0 as Month6Count, 0 as Month7Count,
0 as Month8Count, 0 as Month9COunt, 0 as Month10Count, 0 as Month11Count,
0 as Month12Count, DepartmentName, ActivityDescription
from reports.WorkOrders
WHERE Month(BeginDate) = Month(#rptDate) + 2 and Year(BeginDate) = Year(DateAdd(month,2,#rptDate))
Group By DepartmentName, ActivityDescription
I haven't tested the following, so it's possible there are some typos or minor errors, but I think the following is what you want:
SELECT
sum(case when Month(BeginDate) = Month(dateadd(month, 0, #rptDate)) then 1 else 0 end) as Month1Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 1, #rptDate)) then 1 else 0 end) as Month2Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 2, #rptDate)) then 1 else 0 end) as Month3Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 3, #rptDate)) then 1 else 0 end) as Month4Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 4, #rptDate)) then 1 else 0 end) as Month5Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 5, #rptDate)) then 1 else 0 end) as Month6Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 6, #rptDate)) then 1 else 0 end) as Month7Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 7, #rptDate)) then 1 else 0 end) as Month8Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 8, #rptDate)) then 1 else 0 end) as Month9Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 9, #rptDate)) then 1 else 0 end) as Month10Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 10, #rptDate)) then 1 else 0 end) as Month11Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 11, #rptDate)) then 1 else 0 end) as Month12Count
DepartmentName,
ActivityDescription
FROM
reports.WorkOrders
WHERE
Year(BeginDate) + right('0'+Month(BeginDate),2)
between Year(#rptDate) + right('0'+Month(#rptDate),2)
and Year(dateadd(month,11,#rptDate)) + right('0'+Month(dateadd(month,11,#rptDate)),2)
GROUP BY
DepartmentName,
ActivityDescription
Do let me know how you get on with this. If I've made typos and you can't correct for them, do report the error message and/or any problems with the results.

What is the correct sql script for this select Statement?

This is my script
SELECT iBranch_num,
CASE WHEN iPatient_typ=1 THEN COUNT(iPatient_num) ELSE 0 END AS [New Patient],
CASE WHEN iPatient_typ=2 THEN COUNT(iPatient_num) ELSE 0 END AS [Buying Patient],
CASE WHEN iPatient_typ=3 THEN COUNT(iPatient_num) ELSE 0 END AS [Active Patient],
CASE WHEN iPatient_typ=4 THEN COUNT(iPatient_num) ELSE 0 END AS [Inactive Patient]
FROM tblsotransaction
WHERE iStatus_typ=1
AND iApply_dt BETWEEN 20130401 AND 20130408
AND iBranch_num=14
GROUP BY iBranch_num, iPatient_typ
Current Result:
14, 25, 0, 0, 0
14, 0, 8, 0, 0
14, 0, 0, 97, 0
14, 0, 0, 0, 2
I want the result to be like this
14, 25, 8, 9, 2
Wrap the whole CASE expression inside an aggregate function COUNT, and remove iPatient_typ from the GROUP BY clause:
SELECT
iBranch_num,
COUNT(CASE WHEN iPatient_typ=1 THEN iPatient_num ELSE 0 END) AS [New Patient],
COUNT(CASE WHEN iPatient_typ=2 THEN iPatient_num ELSE 0 END) AS [Buying Patient],
COUNT(CASE WHEN iPatient_typ=3 THEN iPatient_num ELSE 0 END) AS [Active Patient],
COUNT(CASE WHEN iPatient_typ=4 THEN iPatient_num ELSE 0 END) AS [Inactive Patient]
FROM tblsotransaction
WHERE iStatus_typ = 1
AND iApply_dt BETWEEN 20130401 AND 20130408
AND iBranch_num = 14
GROUP BY iBranch_num;
Or: SUM:
SELECT
iBranch_num,
SUM(CASE WHEN iPatient_typ = 1 THEN 1 ELSE 0 END) AS [New Patient],
SUM(CASE WHEN iPatient_typ = 2 THEN 1 ELSE 0 END) AS [Buying Patient],
SUM(CASE WHEN iPatient_typ = 3 THEN 1 ELSE 0 END) AS [Active Patient],
SUM(CASE WHEN iPatient_typ = 4 THEN 1 ELSE 0 END) AS [Inactive Patient]
FROM tblsotransaction
WHERE iStatus_typ = 1
AND iApply_dt BETWEEN 20130401 AND 20130408
AND iBranch_num = 14
GROUP BY iBranch_num;