I try to get data year wise. I tried this query
select
year(SStartDate) as joinedyear ,
count(*) joined_total
from
Detail
where
client = 81
Group By
YEAR(StartDate)
union all
select
year(SEndDate) as leftyear,
count(*) left_total
from
Detail
where
client = 81
Group By
YEAR(SEndDate)
This shows correct data but this shows data like this
1900 12
2001 1
2012 3
2013 3
2016 45
1900 23
2002 34
2004 34
2015 1
2016 56
where as I want data like this
joinedyear joined_total leftyear left_total
1900 12 1900 45
2001 1 2002 34
2012 3 2004 34
2013 3 2015 1
2016 45 2016 56
Try This below query..it mat help you
select * from (
select year(SStartDate) as joinedyear ,
count(*) joined_total from Detail
where client=81
Group By YEAR(StartDate)
) as a
full outer join
(
select year(SEndDate) as leftyear , count(*) left_total from Detail
where client=81
Group By YEAR(SEndDate)
) as b
on a.joinedyear=b.leftyear
Related
I've the following dataset:
System_Name Description Month Year Value_used YTD
APP01 PC A 4 2016 93 -
APP01 PC A 5 2016 90 1
APP01 PC A 6 2016 97 1
APP01 PC A 7 2016 82 -1
APP01 PC A 8 2016 99 1
CCD12 PC B 4 2016 81 -
CCD12 PC B 5 2016 83 1
CCD12 PC B 6 2016 83 0
CCD12 PC B 7 2016 87 1
CCD12 PC B 8 2016 88 1
This dataset is result from the following query:
SELECT *,
CASE WHEN Collect_Usage > PreviousQuota THEN 1
WHEN Collect_Usage = PreviousQuota THEN 0
ELSE -1
END AS [Flag Indicator]
FROM (
SELECT System_Name,
[Description],
[Monh],
[Year],
LAG(Value_used, 1,0) OVER (ORDER BY System_Name, [Description],[Year],[Monh]) AS PreviousQuota
FROM [CAPM_Reporting_Analytics].[dbo].[Capacity_Data_Reds]
) TB1
I am creating the YTD to use in my matrix on SSRS that is creating the pivot to show the evolution from month.
My problem is: Imagine that I want to create the report from Month 8. I want to add a indicator that shows
- If the last month (only consider the month 8 in this case) is above the previous month then Green
- If the previous month is null then Grey
- Else Red.
I want to get anything like this:
Where the trend is only valid between the value of the last and penultimate column (last and penultimate)
How can I do this?
Many thanks!!
SELECT * FROM (
SELECT YEAR(CreateDate) [Year], MONTH(CreateDate) [Month], DATENAME(MONTH,CreateDate) [Month Name],COUNT(id) [Total],
CASE
WHEN PaymentStatus = 1 THEN 'Pending'
WHEN PaymentStatus = 2 THEN 'Commited'
WHEN PaymentStatus = 3 THEN 'Confirmed'
WHEN PaymentStatus = 4 THEN 'Canceled'
WHEN PaymentStatus = 5 THEN 'Failed'
ELSE ''
END AS PaymentStatus,count(ID) as counts
FROM [dbo].[BankPaymentRequest]
GROUP BY YEAR(CreateDate), MONTH(CreateDate), DATENAME(MONTH, CreateDate), PaymentStatus) as asd
PIVOT( SUM(counts)
FOR PaymentStatus IN ([Pending],[Commited],[Confirmed],[Canceled],[Failed])) AS MNamePivot
I want Year-Month wise data from this query with respective [Pending],[Commited],[Confirmed],[Canceled],[Failed] transaction count. Basically i want Month wise Transaction count for Pending,Commited,Confirmed,Canceled and Failed.
I am using Sql Server 2016
Now i am getting exact data but Months are listed twice or thrice.
I want data like below format
Year Month Total Pending Commited Confirmed Canceled Failed
2016 Jan 34 1 4 63 840 157
2016 Feb 34 8 4 62 8 15
2016 Mar 65 1 4 63 840 157
2016 Dec 56 8 4 62 8 15
2017 Jan 78 1 4 63 840 157
2017 Feb 89 8 4 62 8 15
2017 Mar 67 1 4 63 840 157
2017 Dec 8 4 62 8 15 345
Please help me for such query or any alternative query.
You could try putting your existing query inside of a Common Table Expression and then GROUP BY the year and month while summing the numbers.
WITH preselect AS
(
SELECT * FROM (
SELECT YEAR(CreateDate) [Year], MONTH(CreateDate) [Month], DATENAME(MONTH,CreateDate) [Month Name],COUNT(id) [Total],
CASE
WHEN PaymentStatus = 1 THEN 'Pending'
WHEN PaymentStatus = 2 THEN 'Commited'
WHEN PaymentStatus = 3 THEN 'Confirmed'
WHEN PaymentStatus = 4 THEN 'Canceled'
WHEN PaymentStatus = 5 THEN 'Failed'
ELSE ''
END AS PaymentStatus,count(ID) as counts
FROM [dbo].[BankPaymentRequest]
GROUP BY YEAR(CreateDate), MONTH(CreateDate), DATENAME(MONTH, CreateDate), PaymentStatus) as asd
PIVOT( SUM(counts)
FOR PaymentStatus IN ([Pending],[Commited],[Confirmed],[Canceled],[Failed])) AS MNamePivot
)
SELECT [YEAR],[MONTH],SUM(TOTAL)'TOTAL',SUM(Pending)'Pending'
,SUM(Commited)'Commited',SUM(Confirmed)'Confirmed'
,SUM(Canceled)'Canceled',SUM(Failed)'Failed'
FROM preselect
GROUP BY [YEAR],[MONTH]
this is my query
SELECT *
FROM (SELECT CurDate, YEAR(CurDate) AS orderyear, Warranty_Info
FROM eod_main where year(CurDate)>=2009 and year(CurDate)<=2011) AS D
PIVOT(SUM(Warranty_Info) FOR orderyear IN([2009],[2010],[2011])) AS P
the above query return data but CurDate return date it is is return multiple date for same month.
i want that SUM(Warranty_Info) should return only once for every month and year
output should look like
Month 2009 2010 2011 2012 2013
----- ---- ---- ---- ---- -----
1 10 0 11 32 98
2 20 10 21 11 44
3 0 224 33 77 31
some kind of problem is there in my query and that is why it is returning multiple data for same month like
please help me to have the right query. thanks
Instead of using CurDate as the full date with year, month and day, I would suggest using the Month() function to return the numeric value of each month. This will allow you to group by month instead of the full date:
SELECT dateMonth, [2009],[2010],[2011]
FROM
(
SELECT month(CurDate) dateMonth,
YEAR(CurDate) AS orderyear, Warranty_Info
FROM eod_main
where year(CurDate)>=2009
and year(CurDate)<=2011
) AS D
PIVOT
(
SUM(Warranty_Info)
FOR orderyear IN([2009],[2010],[2011])
) AS P;
See SQL Fiddle with Demo
I have a query that lists employees ratings throughout the years, but the years are all listed in one column. I am trying to get the meeting years to have their own columns with the score, and if they do not have a score for it to be null.
THe Code so far:
SELECT Employee_ID, Meeting_Year, Manager_Readiness_Rating
FROM dbo.v_sc17_TMS_Data_Career_Meeting_Rating
GROUP BY Employee_ID, Meeting_Year, Manager_Readiness_Rating
So if the meeting_year is 2012 I want all 2012 Manager_Readiness_Rating listed in the column for the employees. Here is some examples
ID Year Rating
1 2011 11
2 2012 10
3 2010 09
4 2010 03
4 2011 03
I would like it to look like
ID 2010 2011 2012
1 NULL 11 NULL
2 NULL NULL 1
3 09 NULL NULL
4 03 03 NULL
SELECT Employee_ID, Meeting_Year, GROUP_CONCAT(Manager_Readiness_Rating)
FROM dbo.v_sc17_TMS_Data_Career_Meeting_Rating
GROUP BY Employee_ID, Meeting_Year
This is flagged as MySQL, but the dbo makes me think it's SQL Server. This query should work for either one:
SELECT
Employee_ID AS ID,
MAX(CASE WHEN Year = 2010 THEN Rating END) AS R2010,
MAX(CASE WHEN Year = 2011 THEN Rating END) AS R2011,
MAX(CASE WHEN Year = 2012 THEN Rating END) AS R2012
FROM dbo.v_sc17_TMS_Data_Career_Meeting_Rating
GROUP BY Employee_ID
ORDER BY 1
I have a query that works well but has one problem, it displays other data I don't want.
The data is the latest of each username.
Here is the query:
SELECT
l.USER_KEY AS id,
l.USER_ID AS username,
gl1.CHAR_KEY AS char_id,
gl1.NAME AS charname,
gl1.GATENUM AS server,
CONVERT(VARCHAR(20), l.LOGINTIME, 100) AS user_time,
CONVERT(VARCHAR(20), gl1.OCCUR_TIME, 100) AS char_time
FROM LOG_CONNECT201211 AS gl1
JOIN game.dbo.CHAR_INFOR AS g --character data
ON gl1.CHAR_KEY = g.CHAR0_KEY OR gl1.CHAR_KEY = g.CHAR1_KEY OR gl1.CHAR_KEY = g.CHAR2_KEY
JOIN login.dbo.USER_CHECK_LOGIN AS l --login data
ON g.USER_KEY = l.USER_KEY
JOIN (SELECT char_key, max(OCCUR_TIME) as mostrecent --game logs
FROM LOG_CONNECT201211
WHERE KIND=20 OR KIND=21
GROUP BY char_key) AS gl2
ON gl2.char_key = gl1.char_key and gl2.mostrecent = gl1.OCCUR_TIME
WHERE l.CHECKLOGIN = 1
ORDER BY username DESC
That returns:
id username char_id name map user_time char_time
------------------------------------------------------------------------------------
3667 zr5970 11002 warpath 4 Nov 15 2012 8:54AM Nov 7 2012 6:31AM
3667 zr5970 11004 bloodfines 4 Nov 15 2012 8:54AM Nov 7 2012 6:33AM
3667 zr5970 11003 hanzhou 1 Nov 15 2012 8:54AM Nov 15 2012 8:54AM
14999 yvacosta 52086 Creams 1 Nov 15 2012 8:17AM Nov 15 2012 8:17AM
23433 yurich 1911481 abal 5 Nov 15 2012 8:34AM Nov 9 2012 4:05PM
23433 yurich 1911482 yurich 5 Nov 15 2012 8:34AM Nov 15 2012 8:30AM
23433 yurich 1911483 sharmaine 5 Nov 15 2012 8:34AM Nov 15 2012 8:35AM
10967 yubiwamoi 33376 Dwina 1 Nov 15 2012 4:33AM Nov 15 2012 4:33AM
So the data is correct, but I only want to return one row per username.
On this data the username returns 3, with 3 names, but the only name I want is that one with the latest char_time.
Example correct data:
id username char_id name map user_time char_time
-----------------------------------------------------------------------------------
3667 zr5970 11003 hanzhou 1 Nov 15 2012 8:54AM Nov 15 2012 8:54AM
14999 yvacosta 52086 Creams 1 Nov 15 2012 8:17AM Nov 15 2012 8:17AM
23433 yurich 1911483 sharmaine 5 Nov 15 2012 8:34AM Nov 15 2012 8:35AM
10967 yubiwamoi 33376 Dwina 1 Nov 15 2012 4:33AM Nov 15 2012 4:33AM
Notice that I only displayed the data for zr5970 with the latest char_time
Please advice, Thank you.
Use ROW_NUMBER()
SELECT *
FROM
(
SELECT
l.USER_KEY AS id,
l.USER_ID AS username,
gl1.CHAR_KEY AS char_id,
gl1.NAME AS charname,
gl1.GATENUM AS server,
CONVERT(VARCHAR(20), l.LOGINTIME, 100) AS user_time,
CONVERT(VARCHAR(20), gl1.OCCUR_TIME, 100) AS char_time,
rn = row_number() over (partition by l.USER_KEY order by gl1.OCCUR_TIME DESC)
FROM LOG_CONNECT201211 AS gl1
JOIN game.dbo.CHAR_INFOR AS g --character data
ON gl1.CHAR_KEY = g.CHAR0_KEY OR gl1.CHAR_KEY = g.CHAR1_KEY OR gl1.CHAR_KEY = g.CHAR2_KEY
JOIN login.dbo.USER_CHECK_LOGIN AS l --login data
ON g.USER_KEY = l.USER_KEY
WHERE l.CHECKLOGIN = 1
) X
WHERE rn=1
ORDER BY username DESC
First select just the user name and the max time. This way you will have the correct number of rows. When you have that you can easily the other columns to your query...
select a.USER_KEY AS id,
l.USER_ID AS username,
gl1.CHAR_KEY AS char_id,
gl1.NAME AS charname,
gl1.GATENUM AS server,
CONVERT(VARCHAR(20), l.LOGINTIME, 100) AS user_time,
CONVERT(VARCHAR(20), gl1.OCCUR_TIME, 100) AS char_time
from
(select user_key, max(occur_time) as mostrecent
from log_connect2011
WHERE KIND=20 OR KIND=21
group by user_key) a
join LOG_CONNECT201211 AS gl1 on a.user_key = gl1.user_key and a.mostrecent = gl1.occur_time
JOIN game.dbo.CHAR_INFOR AS g --character data
ON gl1.CHAR_KEY = g.CHAR0_KEY OR gl1.CHAR_KEY = g.CHAR1_KEY OR gl1.CHAR_KEY = g.CHAR2_KEY
JOIN login.dbo.USER_CHECK_LOGIN AS l --login data
ON g.USER_KEY = l.USER_KEY
WHERE l.CHECKLOGIN = 1
ORDER BY username DESC