Get multiple rows from query - sql

SELECT USERINFO.UserID
,SUM(DATEDIFF(DAY, DateFrom, DateTo) + 1) AS total_leave_days
FROM USERINFO
INNER JOIN CHECKINOUT ON USERINFO.USERID = CHECKINOUT.USERID
LEFT OUTER JOIN DEPARTMENTS ON DEPARTMENTS.DEPTID = USERINFO.DEFAULTDEPTID
left join AuthLeave on AuthLeave.userid = userinfo.userid
and AuthLeave.DATEFROM>='2014-01-01'
and AuthLeave.DATETO<='2014-06-30'
WHERE (CHECKINOUT.CHECKTIME >= '2014-01-01')
AND (CHECKINOUT.CHECKTIME <= '2014-06-30')
AND DEPARTMENTS.DEPTNAME = 'GEN/SUP-TBL'
GROUP BY USERINFO.UserID
here is my code from this i can get below out put
UserID total_leave_days
35 NULL
350 NULL
30 NULL
10 735
167 NULL
21 920
1 621
224 NULL
so it is not correct my Authleave table data is below:
UserID DATEFROM DATETO
1 2014-03-10 2014-03-15
10 2014-05-28 2014-05-29
21 2014-05-27 2014-05-27
1 2014-04-10 2014-04-15
from now i want output like below:
UserID total_leave_days
35 NULL
350 NULL
30 NULL
10 2
167 NULL
21 1
1 12
224 NULL
so how can i do this ?

Can you try This SQL
SELECT U.[UserID],
C.[Leave]
FROM [USERINFO] U
LEFT JOIN (SELECT [UserID],
SUM(DATEDIFF(DAY,[DATEFROM],[DATETO])) [Leave]
FROM [CHECKINOUT]
WHERE [CHECKTIME] >= '2014-01-01' AND [CHECKTIME] <= '2014-06-30'
GROUP BY [UserID]
) C ON U.[USERID] = C.[USERID]
LEFT JOIN [DEPARTMENTS] D ON D.[DEPTID] = U.[DEFAULTDEPTID]
LEFT JOIN [AuthLeave] A on A.[userid] = U.[userid]
WHERE D.[DEPTNAME] = 'GEN/SUP-TBL'
enter code here
Edit:
See the below SQL, I got a bit confused by Table Names.
SELECT U.[UserID],
L.[Leave]
FROM [USERINFO] U
JOIN CHECKINOUT C ON U.USERID = C.USERID
LEFT JOIN [DEPARTMENTS] D ON D.[DEPTID] = U.[DEFAULTDEPTID]
LEFT JOIN (SELECT [UserID],
SUM(DATEDIFF(DAY,[DATEFROM],[DATETO])) [Leave]
FROM [AuthLeave]
WHERE [DATEFROM] >= '2014-01-01' AND [DATETO] <= '2014-06-30'
GROUP BY [UserID]
) L ON L.[UserID] =U.[UserID]
WHERE D.[DEPTNAME] = 'GEN/SUP-TBL'
AND C.[CHECKTIME] >= '2014-01-01' AND C.[CHECKTIME] <= '2014-06-30'

Related

How do I merge 2 rows into 1 row in SQL?

ReportDateTime EuId1 EuId2
2020-02-01 1:00 1576 Null
2020-02-01 1:00 Null 1579
2020-02-01 2:00 Null 1573
2020-02-01 2:00 1566 Null
This is what I have and this is what I want...
ReportDateTime EuId1 EuId2
2020-02-01 1:00 1576 1579
2020-02-01 2:00 1566 1573
Here is my code...
;WITH cteEq AS (
SELECT e.EntHID, e.EntCode, e.EntName
FROM cfEntity fac (NOLOCK)
JOIN cfEntityRelation er (NOLOCK) ON fac.EntHID = er.EntParentHID AND er.EntRelEffEnd = '12/31/2078'
JOIN cfEntity e (NOLOCK) ON er.EntChildHID = e.EntHID AND e.EntTypeTID = -200020
WHERE e.EntHID IN (SELECT ea.EaHID FROM cfEntityAttribute ea (NOLOCK) WHERE ea.EaKey = 'AirPermitXref_Diesel')
)
SELECT
ReportDateTime = a.EqReportDate
, EuId1 = CASE
WHEN EntName LIKE '%EU1%' THEN a.EqPwr
END
, EuId2 = CASE
WHEN EntName LIKE '%EU2%' THEN a.EqPwr
END
FROM eqHourlyAir a (NOLOCK)
JOIN cteEq e (NOLOCK) ON a.EqHID = e.EntHID
OUTER APPLY (
SELECT ad.EqHID, ad.EqReportDate, ad.EqRunTime
FROM eqHourlyAir ad (NOLOCK)
LEFT JOIN cfEntityAttribute ea (NOLOCK) ON ad.EqHID = CAST(ea.EaValue AS INT) AND ea.EaKey = 'AirPermitXref_DWI'
WHERE ea.EaHID = a.EqHID
AND ad.EqReportDate = a.EqReportDate
) dwi
OUTER APPLY (
SELECT ad.EqHID, ad.EqReportDate, ad.EqRunTime
FROM eqHourlyAir ad (NOLOCK)
LEFT JOIN cfEntityAttribute ea (NOLOCK) ON ad.EqHID = CAST(ea.EaValue AS INT) AND ea.EaKey = 'AirPermitXref_Diesel'
WHERE ea.EaHID = a.EqHID
AND ad.EqReportDate = a.EqReportDate
) dsl
WHERE CAST(a.EqReportDate AS DATE) >= #StartDate AND CAST(a.EqReportDate AS DATE) <= #EndDate
ORDER BY a.EqReportDate
using nolock hint is not the best idea !
if you have one null value and only one non value for each reportingdate you can grooup by ReportDateTime and get the max value :
...
SELECT
ReportDateTime = a.EqReportDate,
EuId1 = MAX(CASE WHEN EntName LIKE '%EU1%' THEN a.EqPwr END),
EuId2 = MAX(CASE WHEN EntName LIKE '%EU2%' THEN a.EqPwr END)
FROM
{...}
WHERE
CAST(a.EqReportDate AS DATE) >= #StartDate
AND CAST(a.EqReportDate AS DATE) <= #EndDate
GROUP BY a.EqReportDate
ORDER BY
a.EqReportDate
From you sample data it's very easy to select your desired result as below:
select ReportDateTime ,max(EuId1) EuId1,max(EuId2) EuId2 from
(select * from table1 join table2 on .... )t
group by ReportDateTime
But your query indicates that there is more to the story. Please share some more information.

SQL: How to get Max Order number row from multiple rows

How can I Select Max Order_number row from given multiple rows? per example below results 9010305604 is the max number. I need to select 9010305604row
SELECT DISTINCT
CUST.PRIMARY_EMAIL_ADDRESS [EMAIL],
OD.ORDER_NO [ORDER_NUMBER],
CUST.MASTER_CUSTOMER_ID [CUSTOMER ID],
CUST.SUB_CUSTOMER_ID,
CUST.USR_ORIG_JOIN_DATE,
OD.CYCLE_END_DATE
, OD.RATE_CODE
,OD.CYCLE_BEGIN_DATE
--, OD.ORIGINAL_ORDER_NO
FROM CUSTOMER CUST (NOLOCK)
INNER JOIN ORDER_DETAIL OD (NOLOCK)
ON CUST.MASTER_CUSTOMER_ID = OD.SHIP_MASTER_CUSTOMER_ID
AND OD.SHIP_SUB_CUSTOMER_ID = CUST.SUB_CUSTOMER_ID
INNER JOIN ORDER_MASTER OM (NOLOCK)
ON OD.ORDER_NO = OM.ORDER_NO
INNER JOIN PRODUCT PROD
ON [PROD].[PRODUCT_ID] = [OD].[PRODUCT_ID]
WHERE OD.SUBSYSTEM = 'MBR'
AND OD.PRODUCT_CODE IN ('PROFESSIONAL')
AND OD.LINE_STATUS_CODE = 'A'
-- AND OD.CYCLE_BEGIN_DATE <= GETDATE()
AND OD.CYCLE_END_DATE >= GETDATE()
--AND OD.ORIGINAL_ORDER_NO IS NULL
AND CUST.PRIMARY_EMAIL_ADDRESS IS NOT NULL
AND CUST.USR_ORIG_JOIN_DATE >cast(DATEADD(day, -120,GETDATE()) AS DATE)
AND OM.USR_BULK_ORDER_NO IS NULL
AND MASTER_CUSTOMER_ID = '4655302'
EMAIL ORDER_NUMBER CUSTOMER ID SUB_CUSTOMER_ID USR_ORIG_JOIN_DATE CYCLE_END_DATE RATE_CODE CYCLE_BEGIN_DATE
sannyastari#gmail.com 9010305603 4655302 0 2020-11-21 00:00:00.000 2020-12-31 00:00:00.000 1YR 2020-11-21 00:00:00.000
sannyastari#gmail.com 9010305604 4655302 0 2020-11-21 00:00:00.000 2021-12-31 00:00:00.000 1YR 2021-01-01 00:00:00.000
Use a TOP query?
SELECT DISTINCT TOP 1
CUST.PRIMARY_EMAIL_ADDRESS [EMAIL],
OD.ORDER_NO [ORDER_NUMBER],
...
(rest of your query)
ORDER BY
OD.ORDER_NO DESC;

Sql select sub query with count

Ok so i have 3 tables :
[AXprod].[dbo].[RMSPOSINVOICE],[AXPROD].[dbo].[discountcard] ,[IntegrationProd].[dbo].[POS_KvitoGalva]. And i want to find out when discount card was used more than once in one inventlocation and time when it was used. The table [IntegrationProd].[dbo].[POS_KvitoGalva] has these times. I use this code to get the time each card was used each day is:
sELECT a.discountcardid,count(a.discountcardid)
FROM [AXprod].[dbo].[RMSPOSINVOICE] a
inner join [AXPROD].[dbo].[discountcard] b
on a.discountcardid = b.discountcardid
inner join [IntegrationProd].[dbo].[POS_KvitoGalva] c
on a.possalesid = c.id
where a.dataareaid = 'ermi' and len(a.discountcardid) > '0' and b.dataareaid = 'ermi' and ('500' = a.inventlocationid )
and (a.invoicedate >= '2015-04-22 00:00:00.000' and a.invoicedate <= '2015-04-22 00:00:00.000')
group by a.discountcardid,a.inventlocationid,a.posnumber
having count(a.discountcardid) > '1'
And i get the following result:
DISCOUNTCARDID COUNT
123456 2
145962 2
and i have a query to find when each card was used (date and time)
SELECT a.discountcardid,a.inventlocationid,a.posnumber,year,month,day,hour,minute,c.id
FROM [AXprod].[dbo].[RMSPOSINVOICE] a
inner join [AXPROD].[dbo].[discountcard] b
on a.discountcardid = b.discountcardid
inner join [IntegrationProd].[dbo].[POS_KvitoGalva] c
on a.possalesid = c.id
where a.dataareaid = 'ermi' and len(a.discountcardid) > '0' and b.dataareaid = 'ermi' and ('500' = a.inventlocationid )
and (a.invoicedate >= '2015-04-22 00:00:00.000' and a.invoicedate <= '2015-04-22 00:00:00.000')
group by a.discountcardid,a.inventlocationid,a.posnumber,year,month,day,hour,minute,c.id
order by DISCOUNTCARDID
And i get the result:
discountcardid inventlocationid posnumber year month day hour minute id
123456 500 7 2015 4 22 12 44 6355302
123456 500 7 2015 4 22 14 24 6355302
145962 500 7 2015 4 22 13 56 6355302
145962 500 7 2015 4 22 13 24 6355302
145555 500 7 2015 4 22 12 11 5465465
The problem:
I dont want to get discount cards that were only used once so i try this:
SELECT a.discountcardid,a.inventlocationid,a.posnumber,year,month,day,hour,minute,c.id,
( sELECT count(s.discountcardid)
FROM [AXprod].[dbo].[RMSPOSINVOICE] s
inner join [AXPROD].[dbo].[discountcard] b
on s.discountcardid = b.discountcardid
inner join [IntegrationProd].[dbo].[POS_KvitoGalva] c
on s.possalesid = c.id
where s.dataareaid = 'ermi' and len(s.discountcardid) > '0' and b.dataareaid = 'ermi' and ('500' = s.inventlocationid )
and (s.invoicedate >= '2015-04-22 00:00:00.000' and s.invoicedate <= '2015-04-22 00:00:00.000') and s.DISCOUNTCARDID = a.DISCOUNTCARDID
group by s.discountcardid,s.inventlocationid,s.posnumber
having count(a.discountcardid) > '1')
FROM [AXprod].[dbo].[RMSPOSINVOICE] a
inner join [AXPROD].[dbo].[discountcard] b
on a.discountcardid = b.discountcardid
inner join [IntegrationProd].[dbo].[POS_KvitoGalva] c
on a.possalesid = c.id
where a.dataareaid = 'ermi' and len(a.discountcardid) > '0' and b.dataareaid = 'ermi' and ('500' = a.inventlocationid )
and (a.invoicedate >= '2015-04-22 00:00:00.000' and a.invoicedate <= '2015-04-22 00:00:00.000')
group by a.discountcardid,a.inventlocationid,a.posnumber,year,month,day,hour,minute,c.id
order by DISCOUNTCARDID
But all i get is the same number of values and NULL in the last field in all columns. I hope i made myself clear ;).
You should be able to call the query once and use an windowed function in order to get the count. I don't believe you can use an analytic function in the where statement so I added an additional SELECT statement in order to add the WHERE > 1 for the count.
SELECT *
FROM (SELECT
a.discountcardid,
a.inventlocationid,
a.posnumber,
year,
month,
day,
hour,
minute,
c.id,
COUNT(*) OVER (PARTITION BY a.discountcardid, a.inventlocationid, a.posnumber) AS CardCount
FROM AXprod.dbo.RMSPOSINVOICE a
JOIN AXprod.dbo.discountcard b
ON b.discountcardid = a.discountcardid
JOIN IntegrationProd.dbo.POS_KvitoGalva c
ON c.id = a.possalesid
WHERE a.dataareaid = 'ermi'
AND len(a.discountcardid) > '0'
AND b.dataareaid = 'ermi'
AND a.inventlocationid = 500
AND a.invoicedate >= '2015-04-22 00:00:00.000'
AND a.invoicedate <= '2015-04-22 00:00:00.000'
) d
WHERE d.CardCount > 1
ORDER BY d.discountcardid

How to do sum of the two different time

i have the following code :
SELECT distinct userinfo.userid,userinfo.name,timeframe,deptname from EarlyOut
INNER JOIN userinfo ON USERINFO.USERID = earlyout.USERID
INNDER JOIN DEPARTMENTS ON DEPARTMENTS.DEPTID = EarlyOut.DEFAULTDEPTID
where date>='2015-02-01' and date<='2015-02-28' and
DEPARTMENTS.DEPTNAME = 'abc'
Now from above code i am getting following answer:
userid name timeframe deptname
111 xyz 2015-02-05 08:00:00 abc
111 xyz 2015-02-10 09:15:00 abc
Now i want the following output:
userid name timeframe deptname
111 xyz 17:15:00 abc
I want the total of time
So How can i do that?
Try this:
SELECT DISTINCT userinfo.userid,
userinfo.name,
Dateadd(ms, Sum(Datediff(ms, '00:00:00.000', timeframe)), '00:00:00.000') AS newtimeframe,
deptname
FROM EarlyOut
INNER JOIN userinfo
ON USERINFO.USERID = earlyout.USERID
INNER JOIN DEPARTMENTS
ON DEPARTMENTS.DEPTID = EarlyOut.DEFAULTDEPTID
WHERE date >= '2015-02-01'
AND date <= '2015-02-28'
AND DEPARTMENTS.DEPTNAME = 'abc'
GROUP BY userinfo.userid,
userinfo.name,
DEPARTMENTS.DEPTNAME

sql query to find out sum of the slots for each department

I have a Table tempslotsassignedfordept as below
DEPT ScheduledStartDate ScheduledEndDate TotalSlots
DevEng 2012-07-17 00:00:00.000 2012-08-28 00:00:00.000 39
DevEng 2012-07-17 00:00:00.000 2012-08-31 00:00:00.000 18
DevEng 2012-07-18 00:00:00.000 2012-08-29 00:00:00.000 9
DevEng 2012-07-19 00:00:00.000 2012-08-30 00:00:00.000 40
Prod 2012-07-19 00:00:00.000 2012-08-30 00:00:00.000 8
Rel 2012-07-19 00:00:00.000 2012-08-30 00:00:00.000 19
Rel 2012-07-19 00:00:00.000 2013-08-15 00:00:00.000 1
I have to capture the above sum of the totalslots of each department. if there are serial 3days exists for a department.
If you see the above table, there is department deveng has the serial three scheduled start dates as 2012-07-17,2012-07-18,2012-07-19.
I'm trying to see the resuls by using the below query, please could you help what is the more implementations to see the exact result?
SELECT Tmain.DepartmentCode,
CONVERT(VARCHAR(20), Tmain.ScheduledStartDate, 101) 'ScheduledStartDate',
SUM(Tmain.TotalSlots) 'Tmain.TotalSlots',T1Sub.TotalSlots 'T1SubTotalSlots', T2Sub.TotalSlots 'T2SubTotalSlots'
FROM tempSlotsAssignedForDept Tmain
INNER JOIN (Select DATEADD(DAY,1,T1.ScheduledStartDate) 'ScheduledStartDate',SUM(T1.TotalSlots) 'TotalSlots'
FROM tempSlotsAssignedForDept T1
INNER JOIN tempSlotsAssignedForDept Tmain
ON (T1.ScheduledStartDate = dateadd(day, -1, tmain.ScheduledStartDate ) and
T1.DepartmentCode = tmain.DepartmentCode)
GROUP BY T1.ScheduledStartDate ) T1Sub
ON Tmain.ScheduledStartDate = T1Sub.ScheduledStartDate
INNER JOIN (Select DATEADD(DAY,2,T2.ScheduledStartDate) 'ScheduledStartDate',SUM(T2.TotalSlots) 'TotalSlots'
FROM tempSlotsAssignedForDept T2
INNER JOIN tempSlotsAssignedForDept Tmain
ON (T2.ScheduledStartDate = dateadd(day, -2, tmain.ScheduledStartDate ) and
T2.DepartmentCode = tmain.DepartmentCode)
GROUP BY T2.ScheduledStartDate ) T2Sub
ON Tmain.ScheduledStartDate = T2Sub.ScheduledStartDate
GROUP BY Tmain.DepartmentCode,Tmain.ScheduledStartDate,T1Sub.TotalSlots , T2Sub.TotalSlots
ORDER BY Tmain.ScheduledStartDate
The Outof the Above query is
DepartmentCode ScheduledStartDate Tmain.TotalSlots T1SubTotalSlots T2SubTotalSlots
DevEng 07/19/2012 40 9 57
Prod 07/19/2012 8 9 57
Rel 07/19/2012 20 9 57
The final result should be as below.
Becuase, departments prod and rel don't have the series of scheduledstartdate for 3days
DepartmentCode ScheduledStartDate Tmain.TotalSlots T1SubTotalSlots T2SubTotalSlots
DevEng 07/19/2012 40 9 57
please help.
Here is the answer you're looking for:
Select * from
(
Select DepartmentCode,ScheduledStartDate,[Tmain.TotalSlots]
,(Select SUM(T1.TotalSlots) from tempSlotsAssignedForDept T1 where t1.departmentCode=a.DepartmentCode
and T1.ScheduledStartDate = dateadd(day, -1, a.ScheduledStartDate )) T1SubTotalSlots
,(Select SUM(T1.TotalSlots) from tempSlotsAssignedForDept T1 where t1.departmentCode=a.DepartmentCode
and T1.ScheduledStartDate = dateadd(day, -2, a.ScheduledStartDate )) T2SubTotalSlots
from
(
SELECT Tmain.DepartmentCode,
CONVERT(VARCHAR(20), Tmain.ScheduledStartDate, 101) ScheduledStartDate,
SUM(Tmain.TotalSlots) 'Tmain.TotalSlots'
--,(Select SUM(T1.TotalSlots) from tempSlotsAssignedForDept t1 where t1.DepartmentCode=tmain.departmentcode
--and T1.ScheduledStartDate = dateadd(day, -1, tmain.ScheduledStartDate ))
FROM tempSlotsAssignedForDept Tmain
GROUP BY Tmain.DepartmentCode,Tmain.ScheduledStartDate
) a) b where [Tmain.TotalSlots] is not null and T1SubTotalSlots is not null and T2SubTotalSlots is not null