Select with subquery select not working - sql

I keep getting that error on this code. I am not using EXISTS.Please Help.
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
SQL:
If ISDATE(#grpsearch) = 1
SELECT grp.GroupID,
vlanlist.InternetType,
grp.GroupName, cast(grp.StartDateTime as date) as StartDate,
convert(varchar,cast(grp.StartDateTime as time),100) as starttime,
(select case when datepart(hh,convert(datetime,grp.StartDateTime)) > 12 then datepart(hh,convert(datetime,grp.StartDateTime))-12
else case when datepart(hh,convert(datetime,grp.StartDateTime)) = 0 then '12' else datepart(hh,convert(datetime,grp.StartDateTime)) end end as starthour,
datepart(mi,convert(datetime,grp.StartDateTime)) as startmin, case when datepart(hh,convert(datetime,grp.StartDateTime)) >= 12 then 'PM' else 'AM' end as startperiod),
cast(grp.enddatetime as date) as EndDate,
convert(varchar,cast(grp.EndDateTime as time),100) as EndTime,
grp.UserInitials,
grp.UserComments,
roomlist.RoomName,
jacklist.JackNumber
FROM a_Cisco.dbo.grp_internet as grp left outer join
dbo.jacklist as jacklist ON grp.intJack = jacklist.intJack left outer join
dbo.roomlist as roomlist ON grp.intRoom = roomlist.intROom left outer join
dbo.vlanlist as vlanlist ON grp.VlanID = vlanlist.VlanID
WHERE (convert(varchar,cast(grp.StartDateTime as date),100) = #grpsearch)

The problem is this part of the query:
(select case when datepart(hh,convert(datetime,grp.StartDateTime)) > 12
then datepart(hh,convert(datetime,grp.StartDateTime))-12
else case when datepart(hh,convert(datetime,grp.StartDateTime)) = 0
then '12' else datepart(hh,convert(datetime,grp.StartDateTime))
end
end as starthour,
First, you don't need the select at all. Second, you are missing the closing parentheses ()). I would suggest:
(case when datepart(hh,convert(datetime,grp.StartDateTime)) > 12
then datepart(hh,convert(datetime,grp.StartDateTime))-12
else (case when datepart(hh,convert(datetime,grp.StartDateTime)) = 0
then '12'
else datepart(hh,convert(datetime,grp.StartDateTime))
end)
end) as starthour,

Related

SQL code to set a specific date as compared to "today's" date

In the first column (Report_Date) of this query the date is being constructed and is today's day, and is used in the following lines to select is the following columns. I'd like to use a specific day in the query.
I'd like help to write the format to replace column 1 (Report_Date) with a different date to create aging, like June 1st - 2021-06-01.
SELECT
convert(datetime,convert(char,datepart(mm,getdate()))+'/'+convert(char,datepart(dd,getdate()))+'/'+convert(char,datepart(yyyy,getdate())))Report_Date,
sum(case
when datediff(dd, (convert(datetime,convert(char,B.Original_Bill_Date,101))), getdate())
Between 0 and 30 then B.CurrentBalance else 0
end) '0-30',
sum(case
when datediff(dd, (convert(datetime,convert(char,B.Original_Bill_Date,101))), getdate())
Between 31 and 60 then B.CurrentBalance else 0
end) '31-60',
sum(case
when datediff(dd, (convert(datetime,convert(char,B.Original_Bill_Date,101))), getdate())
Between 61 and 90 then B.CurrentBalance else 0
end) '61-90',
sum(case
when datediff(dd, (convert(datetime,convert(char,B.Original_Bill_Date,101))), getdate())
Between 91 and 120 then B.CurrentBalance else 0
end) '91-120',
B.Primary_Payer_Code,
B.Secondary_Payer_Code,
B.CurrentResponsibility_desc,
Case When G.PAYER_GROUP IS NOT NULL Then G.PAYER_GROUP
Else Case When B.Primary_Payer_Code = 'PP' Then 'SELF PAY' Else
F.Description End End
FROM
Date D with (nolock)
JOIN TEL_BILLED_LINE_ITEM_ODS B with (nolock) ON ( convert(datetime,convert(char,B.Original_Bill_Date,101)) = D.DATE)
LEFT OUTER JOIN TBLPAYER_ODS P with (nolock) ON B.Primary_Payer_Code=P.Code
LEFT OUTER JOIN TBLFINANCIALCLASS_ODS F with (nolock) ON F.tblfinancialclassid = P.FinancialClassID
LEFT OUTER JOIN PAYER_GROUP G ON G.PAYER_CODE = P.CODE
WHERE
( B.CurrentBalance != 0)
AND B.BadDebtFlagYN=0
AND B.Primary_Payer_Code !='CL' -- exclude CLIENT 07/01/2010 jdw
GROUP BY
B.Primary_Payer_Code,
B.Secondary_Payer_Code,
B.CurrentResponsibility_desc,
Case When G.PAYER_GROUP IS NOT NULL Then G.PAYER_GROUP
Else Case When B.Primary_Payer_Code = 'PP' Then 'SELF PAY' Else
F.Description End End

INCORRECT SUM() OUTPUT on LEFT JOIN CASE QUERY

when i select the specific table and use the case method to get only the max value of 8 there is no problem with it.
but when i use the 1st query and join it to get the SUM value of totalhrs it gave me different value
REFERENCE TABLE WORKING
SELECT l1.userid,(l2.fname+' '+l2.lname) as empname,l2.department,l1.date,MIN(l1.time_in) as timein,MAX(l1.time_in) as timeout,CASE WHEN DATEDIFF(MINUTE,CONVERT(datetime,MIN(l1.time_in)) ,CONVERT(datetime,MAX(l1.time_in)))/60.0 > 8 THEN 8 ELSE DATEDIFF(MINUTE,CONVERT(datetime,MIN(l1.time_in)) ,CONVERT(datetime,MAX(l1.time_in)))/60.0 END as totalhrs
,CASE WHEN DATEDIFF(minute,'08:00',MIN(l1.time_in)) > 0 THEN DATEDIFF(minute,'08:00',MIN(l1.time_in)) ELSE 0 END as late,
CASE WHEN DATEDIFF(minute,MAX(l1.time_in),'17:00') > 0 THEN DATEDIFF(minute,MAX(l1.time_in),'17:00') ELSE 0 END as undertime,MAX(image_in) as imgo,MIN(image_in) as imgi
from hgs_hr_attendancelogs l1
LEFT JOIN (SELECT userid,lname,fname,department from user_acc)l2
ON l1.userid = l2.userid
WHERE l1.userid = '442' and l1.date between '2021-03-16' and '2021-03-31'
group by l1.userid,l1.date,l2.fname,l2.lname,l2.department
ORDER BY l1.userid
OUTPUT IN SUM INSTEAD ADD THE 4th column(THE SUM IS 78) it gives 2668.17
SELECT l1.userid,SUM(l2.totalhrs),COUNT(l2.late)
FROM hgs_hr_attendancelogs l1
LEFT JOIN(SELECT l1.userid,(l2.fname+' '+l2.lname) as empname,l2.department,l1.date,MIN(l1.time_in) as timein,MAX(l1.time_in) as timeout,
CAST(CASE WHEN DATEDIFF(MINUTE,CONVERT(datetime,MIN(l1.time_in)) ,CONVERT(datetime,MAX(l1.time_in)))/60.0 > 8 THEN 8 ELSE DATEDIFF(MINUTE,CONVERT(datetime,MIN(l1.time_in)) ,CONVERT(datetime,MAX(l1.time_in)))/60.0 END as DECIMAL(18,2)) totalhrs
,CASE WHEN DATEDIFF(minute,'08:00',MIN(l1.time_in)) > 0 THEN DATEDIFF(minute,'08:00',MIN(l1.time_in)) ELSE 0 END as late,
CASE WHEN DATEDIFF(minute,MAX(l1.time_in),'17:00') > 0 THEN DATEDIFF(minute,MAX(l1.time_in),'17:00') ELSE 0 END as undertime,MAX(image_in) as imgo,MIN(image_in) as imgi
from hgs_hr_attendancelogs l1
LEFT JOIN (SELECT userid,lname,fname,department from user_acc)l2
ON l1.userid = l2.userid
group by l1.userid,l1.date,l2.fname,l2.lname,l2.department ) l2
ON l1.userid = l2.userid
WHERE l2.date between '2021-03-16' and '2021-03-31'
GROUP BY l1.userid
Think you dont need the second join hgs_hr_attendancelogs (in not working query). A query like below should work. please check.
select
t1.userid, SUM(t1.totalhrs),COUNT(t1.late)
from
(
--working
SELECT
l1.userid,(l2.fname+' '+l2.lname) as empname,l2.department,l1.date,MIN(l1.time_in) as timein,MAX(l1.time_in) as timeout,
CASE
WHEN DATEDIFF(MINUTE,CONVERT(datetime,MIN(l1.time_in)) ,CONVERT(datetime,MAX(l1.time_in)))/60.0 > 8
THEN 8
ELSE
DATEDIFF(MINUTE,CONVERT(datetime,MIN(l1.time_in)) ,CONVERT(datetime,MAX(l1.time_in)))/60.0
END as totalhrs,
CASE
WHEN DATEDIFF(minute,'08:00',MIN(l1.time_in)) > 0
THEN DATEDIFF(minute,'08:00',MIN(l1.time_in))
ELSE
0
END as late,
CASE
WHEN DATEDIFF(minute,MAX(l1.time_in),'17:00') > 0
THEN DATEDIFF(minute,MAX(l1.time_in),'17:00')
ELSE
0
END as undertime,
MAX(image_in) as imgo,
MIN(image_in) as imgi
from
hgs_hr_attendancelogs l1
LEFT JOIN (SELECT userid,lname,fname,department from user_acc)l2 ON l1.userid = l2.userid
WHERE
l1.userid = '442'
and l1.date between '2021-03-16' and '2021-03-31'
group by
l1.userid,l1.date,l2.fname,l2.lname,l2.department
) t1
group by
l1.userid
ORDER BY
l1.userid
And reg the 'query not working', did you try moving the l2.date between '2021-03-16' and '2021-03-31' inside the left join?
Thanks

How to split data in SQL

I have the following code:
select
FeeEarnerID,
(
select
(select [name] from [User] AS u where u.userid=f.userid)
from
feeearner AS f
where
f.FeeEarnerID=aa.FeeEarnerID
) FeeEarner,
sum(aa.FEES) Fees,
sum(aa.DISB) Disbursements,
sum(aa.CREDITORS) Creditors
from
(
SELECT
FeeEarner.FeeEarnerID,
case when WIPTransaction.WIPTransactionTypeID IN (1,17,18,20,21,25) then WIPTransaction.Amount else 0 end 'FEES',
case when WIPTransaction.WIPTransactionTypeID IN (2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,26,27,28,29) then WIPTransaction.Amount else 0 end 'DISB',
case when WIPTransaction.WIPTransactionTypeID IN (24) then WIPTransaction.Amount else 0 end 'CREDITORS'
FROM
(
(
FeeEarner
JOIN
WIPTransaction ON FeeEarner.FeeEarnerID = WIPTransaction.FeeEarnerID
)
JOIN
WIPTransactionType ON WIPTransactionType.WIPTransactionTypeID = WIPTransaction.WIPTransactionTypeID
)
WHERE
(WIPTransaction.TransactionDate BETWEEN '2020-10-01' AND '2020-12-31')
)
AS aa
group by
FeeEarnerID
Used Table names: WIPtransaction, WIPtransactiontype, Feeearner
I want to display two more columns at the end of the output, namely: Invoiced and Uninvoiced.
The "Invoicenumber" field in the "WIPtransaction" database will be tested for this. If the "Invoicenumber" is NULL - the transaction amount will be added to a sum in the uninvoiced column and if "Invoicenumber" contains a number - the transaction amount will be added to a sum in the invoiced column.
What is the code that I would need to write and where would it be placed?
select
FeeEarnerID,
(
select
(select [name] from [User] AS u where u.userid=f.userid)
from
feeearner AS f
where
f.FeeEarnerID=aa.FeeEarnerID
) FeeEarner,
sum(aa.FEES) Fees,
sum(aa.DISB) Disbursements,
sum(aa.CREDITORS) Creditors,
----------
SUM( InvoicedAmount) AS InvoicedAmount,
SUM(UnInvoicedAmount) AS UnInvoicedAmount
----------
from
(
SELECT
FeeEarner.FeeEarnerID,
case when WIPTransaction.WIPTransactionTypeID IN (1,17,18,20,21,25) then WIPTransaction.Amount else 0 end 'FEES',
case when WIPTransaction.WIPTransactionTypeID IN (2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,26,27,28,29) then WIPTransaction.Amount else 0 end 'DISB',
case when WIPTransaction.WIPTransactionTypeID IN (24) then WIPTransaction.Amount else 0 end 'CREDITORS',
----------
CASE WHEN WIPTransaction.Invoicenumber IS NOT NULL THEN WIPTransaction.Amount END AS InvoicedAmount,
CASE WHEN WIPTransaction.Invoicenumber IS NULL THEN WIPTransaction.Amount END AS UnInvoicedAmount
----------
FROM
FeeEarner
JOIN
WIPTransaction ON FeeEarner.FeeEarnerID = WIPTransaction.FeeEarnerID
JOIN
WIPTransactionType ON WIPTransactionType.WIPTransactionTypeID = WIPTransaction.WIPTransactionTypeID
WHERE
WIPTransaction.TransactionDate BETWEEN '2020-10-01' AND '2020-12-31'
)
AS aa
group by
FeeEarnerID
You can remove your derived query and combine it all into one. The FeeEarner double sub-query can also be optimized:
select
FeeEarnerID,
(
select [name] from [User] AS u where u.userid=FeeEarner.userid
) FeeEarner,
sum(case when WIPTransaction.WIPTransactionTypeID IN (1,17,18,20,21,25) then WIPTransaction.Amount else 0 end) Fees,
sum(case when WIPTransaction.WIPTransactionTypeID IN (2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,26,27,28,29) then WIPTransaction.Amount else 0 end) Disbursements,
sum(case when WIPTransaction.WIPTransactionTypeID IN (24) then WIPTransaction.Amount else 0 end) Creditors,
SUM(CASE WHEN WIPTransaction.Invoicenumber IS NOT NULL THEN WIPTransaction.Amount END) AS InvoicedAmount,
SUM(CASE WHEN WIPTransaction.Invoicenumber IS NULL THEN WIPTransaction.Amount END) AS UnInvoicedAmount
FROM
FeeEarner
JOIN
WIPTransaction ON FeeEarner.FeeEarnerID = WIPTransaction.FeeEarnerID
JOIN
WIPTransactionType ON WIPTransactionType.WIPTransactionTypeID = WIPTransaction.WIPTransactionTypeID
WHERE
WIPTransaction.TransactionDate BETWEEN '2020-10-01' AND '2020-12-31'
group by
FeeEarnerID;

Is it possible to use 'case' with and in 'count'?

Is it possible to use case with and in count
SELECT branches.NAME AS agence,
count(
CASE loanstatus
WHEN '1'
AND Datepart(month,loanaccount.issuedate)= 2 THEN 1
ELSE NULL
END )AS nombre_de_credits_demande ,
count(
CASE loanstatus
WHEN '2' datepart(month,loanaccount.chargeoffdate)= 2 THEN 1
ELSE NULL
END )AS nombre_de_credits_approuve
please help me
You can use it with count(). I prefer sum():
select Branches.Name as Agence,
sum(case when LoanStatus = '1' and
datepart(MONTH, LoanAccount.IssueDate) = 2
then 1 else 0
end ) as Nombre_de_Crédits_Demandé ,
sum(case when LoanStatus = '2' and
datepart(MONTH, LoanAccount.IssueDate) = 2
then 1 else 0
end ) as Nombre_de_Crédits_Approuvé
The issue with your code was not the count() versus sum() it is the mixing of two different case syntaxes. When you use case <var> when <val>, you cannot include any other conditions. Just use when with the full conditions that you want.
And, if you like, you can use count() instead of sum().
And, for conciseness, I prefer the month() function:
select Branches.Name as Agence,
sum(case when LoanStatus = '1' and MONTH(LoanAccount.IssueDate) = 2
then 1 else 0
end ) as Nombre_de_Crédits_Demandé ,
sum(case when LoanStatus = '2' and MONTH(LoanAccount.IssueDate) = 2
then 1 else 0
end ) as Nombre_de_Crédits_Approuvé
you can achieve your goal through this query.
select
branches.name as agence
,(select COUNT(1) from <table_name> where loginstatus=1 and Datepart(month,loanaccount.issuedate)= 2) as nombre_de_credits_demande
,(select COUNT(1) from <table_name> where loginstatus=2 and Datepart(month,loanaccount.issuedate)= 2) as AS nombre_de_credits_approuve
from <Table_name>

Not a single-group group function while using case when statements

In a query I am trying to use case when keywords. I have to check for 3 conditions there. But I am getting Not a single-group group function error. Any syntax error in my query? Please guide.
Query is
SELECT
CASE WHEN DIST_TYPE_ID IN (5033,5034,5035,5036)
THEN MIN (b2b_start_dt +NVL(access_lead_Days,0))
ELSE
CASE WHEN MAX(overridden) = 0
THEN NVL (MIN (src_start_dt), MIN (b2b_start_dt))
ELSE MAX(B2B_START_dT) END
END as start_date
FROM prog_access_movie_v
WHERE trunc(SYSDATE) BETWEEN b2b_start_dt AND b2b_end_dt
AND (user_id IS NOT NULL OR GROUP_ID IS NOT NULL)
AND dist_type_id IN (5034) AND prog_id = (432899)
This is the select:
SELECT (CASE WHEN DIST_TYPE_ID IN (5033, 5034, 5035, 5036)
THEN MIN(b2b_start_dt + NVL(access_lead_Days, 0))
ELSE (CASE WHEN MAX(overridden) = 0
THEN NVL(MIN(src_start_dt), MIN(b2b_start_dt))
ELSE MAX(B2B_START_dT)
END)
END) as start_date
In terms of aggregation, everything is fine except for the condition on DIST_TYPE_ID. If you had:
SELECT (CASE WHEN MAX(DIST_TYPE_ID) IN (5033, 5034, 5035, 5036)
THEN MIN(b2b_start_dt + NVL(access_lead_Days, 0))
ELSE (CASE WHEN MAX(overridden) = 0
THEN NVL(MIN(src_start_dt), MIN(b2b_start_dt))
ELSE MAX(B2B_START_dT)
END)
END) as start_date
Or:
SELECT (CASE WHEN SUM(CASE WHEN DIST_TYPE_ID IN (5033, 5034, 5035, 5036) THEN 1 ELSE 0 END) > 0
THEN MIN(b2b_start_dt + NVL(access_lead_Days, 0))
ELSE (CASE WHEN MAX(overridden) = 0
THEN NVL(MIN(src_start_dt), MIN(b2b_start_dt))
ELSE MAX(B2B_START_dT)
END)
END) as start_date
Or a myriad of other possibilities, then the query should parse and execute.
It was a silly mistake from me . Group by dist_type_id worked out.
SELECT CASE WHEN DIST_TYPE_ID IN (5033,5034,5035,5036) THEN MIN (b2b_start_dt +NVL(access_lead_Days,0))
ELSE CASE WHEN MAX(overridden) = 0 THEN NVL (MIN (src_start_dt), MIN (b2b_start_dt)) ELSE MAX(B2B_START_dT) END END as start_date
FROM prog_access_movie_v
WHERE trunc(SYSDATE) BETWEEN b2b_start_dt AND b2b_end_dt
AND (user_id IS NOT NULL OR GROUP_ID IS NOT NULL)
AND dist_type_id IN (5034) AND prog_id = (432899)
group by access_lead_days, dist_type_id