i have a query like this:
SELECT
MIN(F_Exhibition_Name) AS F_Site_name,
(SELECT
SUM(F_Quantity)
FROM T_assets
WHERE T_assets.F_ref_code = T_Item_movement.ItemCode
AND F_State = 'A')
AS openingqty,
CASE
WHEN dbo.T_Item_Movement.F_Status = 1 AND
dbo.T_Item_Movement.F_Site_Code <> dbo.T_Item_Movement.F_Frm_Site_Code THEN SUM(dbo.T_Item_Movement.F_Quantity)
ELSE 0
END,
CASE
WHEN dbo.T_Item_Movement.F_Status = 2 AND
dbo.T_Item_Movement.F_Site_Code <> dbo.T_Item_Movement.F_Frm_Site_Code THEN SUM(dbo.T_Item_Movement.F_Quantity)
ELSE 0
END
FROM T_Item_movement
LEFT OUTER JOIN T_L2Category
ON T_L2Category.F_ItemCode = T_Item_movement.ItemCode
LEFT OUTER JOIN T_Exhibition
ON T_Exhibition.F_Exhibition_Code = T_Item_movement.F_Site_Code
WHERE F_Cat_code = 'FN'
--AND F_Cat_code IN ('EC', 'EL', 'FL', 'FN', 'GR', 'MX', 'OT', 'SH')
AND F_L1Cat_code = 'TT'
AND itemcode = 'TT015-BLK'
AND CONVERT(varchar(10), F_datetime, 112) >= '20130915'
AND CONVERT(varchar(10), F_datetime, 112) <= '20150915'
AND F_Exhibition_Name IS NOT NULL
GROUP BY ItemCode,
dbo.T_Item_Movement.F_Status,
dbo.T_Item_Movement.F_Site_Code,
dbo.T_Item_Movement.F_Frm_Site_Code
i am getting out put like this:
I am getting gulf glass 2015 in two column. I want to get in column and show the result in same column only.
Expected output :
Gulf class 2015 6 5
ATM 2015 0 3
It's the GROUP BY columns that are causing it.
You could change your CASE Statement to SUM the result, and therefore remove some of the columns in the GROUP BY:
...
SUM(CASE WHEN dbo.T_Item_Movement.F_Status = 1 AND ...
THEN dbo.T_Item_Movement.F_Quantity
ELSE 0
END),
...
GROUP BY ItemCode
SELECT F_Site_name, openingqty,
SUM(col1) AS col1, SUM(col2) AS col2
FROM
(
SELECT
MIN(F_Exhibition_Name) AS F_Site_name,
(SELECT
SUM(F_Quantity)
FROM T_assets
WHERE T_assets.F_ref_code = T_Item_movement.ItemCode
AND F_State = 'A')
AS openingqty,
CASE
WHEN dbo.T_Item_Movement.F_Status = 1 AND
dbo.T_Item_Movement.F_Site_Code <> dbo.T_Item_Movement.F_Frm_Site_Code THEN SUM(dbo.T_Item_Movement.F_Quantity)
ELSE 0
END AS col1,
CASE
WHEN dbo.T_Item_Movement.F_Status = 2 AND
dbo.T_Item_Movement.F_Site_Code <> dbo.T_Item_Movement.F_Frm_Site_Code THEN SUM(dbo.T_Item_Movement.F_Quantity)
ELSE 0
END AS col2
FROM T_Item_movement
LEFT OUTER JOIN T_L2Category
ON T_L2Category.F_ItemCode = T_Item_movement.ItemCode
LEFT OUTER JOIN T_Exhibition
ON T_Exhibition.F_Exhibition_Code = T_Item_movement.F_Site_Code
WHERE F_Cat_code = 'FN'
--AND F_Cat_code IN ('EC', 'EL', 'FL', 'FN', 'GR', 'MX', 'OT', 'SH')
AND F_L1Cat_code = 'TT'
AND itemcode = 'TT015-BLK'
AND CONVERT(varchar(10), F_datetime, 112) >= '20130915'
AND CONVERT(varchar(10), F_datetime, 112) <= '20150915'
AND F_Exhibition_Name IS NOT NULL
GROUP BY ItemCode,
dbo.T_Item_Movement.F_Status,
dbo.T_Item_Movement.F_Site_Code,
dbo.T_Item_Movement.F_Frm_Site_Code
) AS t
GROUP BY F_Site_name, openingqty
Related
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
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;
I am using SQL Server 2012.
Here is my query, I want the ordering in this order and not alphabetical.
Here is the order I want them in :
Foreign,
Out of State,
Texas,
Unknown,
Total Enrollment
I used order by cases, but I am getting an error:
Incorrect syntax near '5'
How do I fix it?
Query :
declare #Term int = 20172;
select
[Category], [Last_Term], [Current_Term], [#Change], [%Change]
from
(select
[Category],
cast(round(((cast(y.Last_Term as float)) * 1), 2) as varchar(10)) as 'Last_Term',
cast(round(((cast(y.Current_Term as float)) * 1), 2) as varchar(10)) as 'Current_Term',
cast(round(((cast(y.Current_Term as float) -
cast(y.Last_Term as float)) * 1), 2) as varchar(10)) as '#Change',
cast(round((((cast(y.Current_Term as float) -
cast(y.Last_Term as float)) /
(cast(y.Last_Term as float))) * 100), 2) as varchar(10)) + '%' as '%Change'
from
(select
Category,
Case
when year = substring(CAST (#Term- 10 as Varchar(10)),1,4)
then 'Current_Term'
when year = substring(CAST (#Term - 20 as Varchar(10)),1,4)
then 'Last_Term'
End As ACAD_YEAR,
a.enroll
from
(select
case
when dh.HOME = 'F'
then 'Foreign'
when dh.HOME = 'O'
then 'Out of State'
when dh.Home = 'I'
then 'texas'
when dh.Home = 'U'
then 'UnKnown'
end as Category,
(CONVERT(INT, SUBSTRING(ACADEMIC_PERIOD, 1, 4))) - 1 as year,
count(*) as enroll
from
[IR_DW].[dbo].[dw_enrollment_F] d
inner join
dim_Time t on d.TIME_KEY = t.TIME_KEY
inner join
dim_Home dh on d.HOME_KEY = dh.HOME_KEY
inner join
dim_Student_Level sl on d.STUDENT_LEVEL_KEY = sl.STUDENT_LEVEL_KEY
where
ACADEMIC_PERIOD_ALL = #Term - 10
or ACADEMIC_PERIOD_ALL = #Term
group by
dh.HOME, (CONVERT(INT,SUBSTRING(ACADEMIC_PERIOD,1,4))) - 1) a
) src
PIVOT
(SUM(Enroll) FOR ACAD_YEAR in ([Current_Term] , [Last_Term])) y
) a
order by
case
when a.[Category] = 'Foreign' then 1
when a.[Category] = 'Out of State' then 2
when a.[Category] = 'Texas' then 3
when a.[Category] = 'Unknown' then 4
You are missing and END though you are missing the 5 case as well...
when a.[Category] = 'Total Enrollment' then 5
ORDER BY
case when a.[Category] = 'Foreign' then 1
when a.[Category] = 'Out of State' then 2
when a.[Category] = 'Texas' then 3
when a.[Category] = 'Unknown' then 4
when a.[Category] = 'Total Enrollment' then 5 --added this
else 6 --catch all to make anything that fell outside of these categories unsorted afterwards
end --added here
i want to pivot table but i get some error about this image
this is my code
SELECT ARCBG_Abbrev
,ARCIM_Code
,SUM(CASE WHEN TAR_Code = 'O' THEN ITP_Price
ELSE 0 END) AS O
,SUM(CASE WHEN TAR_Code = 'I' THEN ITP_Price ELSE 0 END)
AS I
,SUM(CASE WHEN TAR_Code = 'F' THEN ITP_Price ELSE 0 END)
AS F
FROM SYSTEM.VS_OrderItem
WHERE (ARCIM_Code = '010004')
GROUP BY ARCBG_Abbrev, ARCIM_Code
i try to distinct column to check value in column is same in every row where ARCIM_Code = 010004 .it's the same value in column ARCBG_Abbrev. Can you check my pivot code plascc. thank you
on the assumption it's an ODBC driver limitation, perhaps nesting the case expressions as a subquery will help.
SELECT
ARCBG_Abbrev
, ARCIM_Code
, SUM(O) AS O
, SUM(I) AS I
, SUM(F) AS F
FROM (
SELECT
ARCBG_Abbrev
, ARCIM_Code
, CASE
WHEN TAR_Code = 'O' THEN
ITP_Price
ELSE
0
END AS O
, CASE
WHEN TAR_Code = 'I' THEN
ITP_Price
ELSE
0
END AS I
, CASE
WHEN TAR_Code = 'F' THEN
ITP_Price
ELSE
0
END AS F
FROM SYSTEM.VS_OrderItem
) AS SQ
WHERE ARCIM_Code = '010004'
GROUP BY
ARCBG_Abbrev
, ARCIM_Code
A workmate has given me this stored procedure and asked for help to create an sql INSERT statement based on its results, but am a little confused to some of the methods. For example, I've never seen a CASE statement in sql. I looked it up but the useage is different from what I was given.
Here is the stored procedure
if #ScheduleType Is Null
SELECT Lu_Schedule_Types.ScheduleType,
SUM(CASE WHEN InductionProduction = 1 THEN CASE WHEN (LEFT(DATENAME(Month,
3)) = 'JAN' THEN ItemQty END END) AS I01,
SUM(CASE WHEN InductionProduction = 1 THEN CASE WHEN (LEFT(DATENAME(Month,
Item_Schedule.ItemScheduleDate), 3))
= 'FEB' THEN ItemQty END END) AS I02,
SUM(CASE WHEN InductionProduction = 2 THEN ItemQty ELSE 0 END) AS PRD,
LmpProjectInfo.PlannedQty,
LmpProjectInfo.AuthorizedQty,
LmpProjectInfo.WbsElementID
FROM Item_Schedule
INNER JOIN Lu_Schedule_Types
ON Item_Schedule.ItemScheduleType = Lu_Schedule_Types.ScheduleTypeId
RIGHT OUTER JOIN LmpProjectInfo
ON Item_Schedule.ItemWbsElement = LmpProjectInfo.WbsElementID
WHERE
(Item_Schedule.IsActive = 1)
AND (Item_Schedule.ItemWbsElement = #WbsElement)
AND (Item_Schedule.ItemScheduleDate < DATEADD(d, 1, #EndDate)) AND
(Item_Schedule.ItemScheduleDate >= #StartDate)
GROUP BY Lu_Schedule_Types.ScheduleType
, Lu_Schedule_Types.ScheduleTypeId
, LmpProjectInfo.PlannedQty
, LmpProjectInfo.AuthorizedQty
, LmpProjectInfo.WbsElementID
ORDER BY Lu_Schedule_Types.ScheduleTypeId
I am supposed to be helping him out but I'm by far a database wizard, and am a little out of my depth here. I'd really appreciate the help/advice/direction.
Thanks much!
This is quick sample that might work for you. This assumes that the table you want to insert data into takes all of the values return from the SELECT statement and that they are of the same type.
As a side note, the reason you might have got a bit confused about the CASE statements is possibly due to the fact there are two main ways to use them in SQL. CASE WHEN... like you have here and CASE #value# WHEN #result# THEN.... A little more searching on the web will lead you to some nice examples. For example this one.
INSERT INTO TABLE_NAME
(
ScheduleType,
I01,
I02,
PRD,
PlannedQty,
AuthorizedQty,
WbsElementID
)
SELECT
Lu_Schedule_Types.ScheduleType,
SUM(CASE WHEN InductionProduction = 1 THEN CASE WHEN (LEFT(DATENAME(Month,
3)) = 'JAN' THEN ItemQty END END) AS I01,
SUM(CASE WHEN InductionProduction = 1 THEN CASE WHEN (LEFT(DATENAME(Month, Item_Schedule.ItemScheduleDate), 3))
= 'FEB' THEN ItemQty END END) AS I02,
SUM(CASE WHEN InductionProduction = 2 THEN ItemQty ELSE 0 END) AS PRD,
LmpProjectInfo.PlannedQty,
LmpProjectInfo.AuthorizedQty,
LmpProjectInfo.WbsElementID
FROM
Item_Schedule INNER JOIN
Lu_Schedule_Types ON Item_Schedule.ItemScheduleType = Lu_Schedule_Types.ScheduleTypeId RIGHT OUTER JOIN
LmpProjectInfo ON Item_Schedule.ItemWbsElement = LmpProjectInfo.WbsElementID
WHERE
(Item_Schedule.IsActive = 1) AND (Item_Schedule.ItemWbsElement = #WbsElement) AND
(Item_Schedule.ItemScheduleDate < DATEADD(d, 1, #EndDate)) AND
(Item_Schedule.ItemScheduleDate >= #StartDate)
GROUP BY Lu_Schedule_Types.ScheduleType, Lu_Schedule_Types.ScheduleTypeId,
LmpProjectInfo.PlannedQty, LmpProjectInfo.AuthorizedQty,
LmpProjectInfo.WbsElementID
ORDER BY Lu_Schedule_Types.ScheduleTypeId
Try this one -
INSERT INTO dbo.table1 -- insert in table
(
ScheduleType
, I01
, I02
, PRD
, PlannedQty
, AuthorizedQty
, WbsElementID
)
SELECT
t.ScheduleType
, I01 = SUM(CASE WHEN InductionProduction = 1 AND MONTH(s.ItemScheduleDate) = 1 THEN ItemQty END)
, I02 = SUM(CASE WHEN InductionProduction = 1 AND MONTH(s.ItemScheduleDate) = 2 THEN ItemQty END)
, PRD = SUM(CASE WHEN InductionProduction = 2 THEN ItemQty END)
, i.PlannedQty
, i.AuthorizedQty
, i.WbsElementID
--INTO #temp_table -- or insert in temp table
FROM dbo.Item_Schedule s
JOIN dbo.Lu_Schedule_Types t ON s.ItemScheduleType = t.ScheduleTypeId
RIGHT JOIN dbo.LmpProjectInfo i ON s.ItemWbsElement = i.WbsElementID
WHERE s.IsActive = 1
AND s.ItemWbsElement = #WbsElement
AND s.ItemScheduleDate < DATEADD(d, 1, #EndDate))
AND s.ItemScheduleDate >= #StartDate
GROUP BY
t.ScheduleType
, t.ScheduleTypeId
, i.PlannedQty
, i.AuthorizedQty
, i.WbsElementID
ORDER BY t.ScheduleTypeId