I have this SQL query, which gives me the numbers of orders within a date range, and that is OK:
SELECT CardName, ItemNumber, COUNT(*) AS antal, year(date) as aarstal, SUM(Numbers) AS total, sum(case when year(date)=2012 then 1 else 0 end) as newtest
FROM tblOrders
WHERE (Publisher = 3) AND (Date > '01-01-2012 00:00:00') AND (Date < '30-09-2014 23:59:59') AND (Status = 3) and ItemNumber!=9130 and ItemNumber!=9180 and ItemNumber!=9170 and ItemNumber!=9190
GROUP BY ItemNumber, CardName, year(date)
This gives me ie
CompanyA 0 76 2012 10900 76
CompanyA 0 42 2013 6300 0
CompanyB 0 1 2012 100 1
CompanyB 0 7 2013 1100 0
CompanyC 0 1 2014 300 0
CompanyD 0 7 2014 700 0
CompanyE 0 2 2012 300 2
CompanyE 0 1 2013 200 0
From this, I can see that CompanyA bought 10900 units in 2012, from a total of 76 orders that year, and in 2013, they bought 6300 units. For sales reasons, we would like (or need) this output as
CompanyA 0 76 2012 10900 42 2013 6300
CompanyB 0 1 2012 100 7 2013 1100
CompanyC 0 1 2014 300 0 2013 0
CompanyD 0 7 2014 700 0 2013 0
CompanyE 0 2 2012 300 1 2013 200
So you could see if a customer is going up or down in sales. I've tried various things, but I don't know if there is any possibility to have the Numbers column SUM'ed up per year in a query like this, any suggestions?
PS: The
sum(case when year(date)=2012 then 1 else 0 end) as newtest
was just for a test, I tried something like SUM(Numbers case...) but that was not accepted!
EDIT: I just made a demo in SQL Fiddle, just to give a better understanding of my problem:
http://sqlfiddle.com/#!3/072a1/5
In there, you can see that my customer A has bought a total of 300 in 2012 (on 2 orders), 700 in 2013 (on 2 orders), and finally 200 in 2014 (on 1 order). The pivot examples I've found only counts the number of orders, and not the value of my Numbers - could somebody edit the SQL Fiddle, making that one work, then I'm sure I could learn and adapt the solution into my "real" database?
/Tommy
Related
This is my table
Month Year count of subscription per date
---- ---- ----------------------------
1 2010 10
2 2010 30
4 2010 20
7 2010 40
2 2011 60
Please need your help, I need to get the result like below table, summation second row with first row, same thing for another rows: Here some months are missed in above table got added with 0 as count of subscription column
Month Year count of subscription per date sum of subscription
---- --- ---------------------------- ---------------------
1 2010 10 10
2 2010 30 40
3 2010 0 40
4 2010 20 60
5 2010 0 60
6 2010 0 60
7 2010 40 100
8 2010 0 100
9 2010 0 100
10 2010 0 100
11 2010 0 100
12 2010 0 100
1 2011 0 100
2 2011 60 160
3 2011 0 160
and so on till
12 2011 0 160
You can try this way
create table #temp
(
[Month] int,
[year] int,
[count of subscription per date] int,
[sum of] int
)
declare #id int,#sumall int
set #id=1
set #sumall=0
while(#id<=12)
begin
if exists(select month from [your table] where month=#id)
begin
set #sumall=#sumall+(select count from [your table] where month=#id)
insert into #temp
select month,year,count,#sumall as summ from [your table] where month=#id
end
else
begin
insert into #temp
select #id,'2010',0,#sumall
end
set #id=#id+1
end
select * from #temp
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!!
I have a table [tblCalls] containing a date and a telephone number.[logdate] & [telephone]
I'm trying to produce an output similar to the following:
Date Mobiles Landlines
Nov 2016 28 47
Dec 2016 65 98
Jan 2017 11 17
... and so on
Using the following:
SELECT MONTH(logdate) as myDate,
SUM(CASE WHEN telephone LIKE '07%' THEN 1 ELSE 0 END) mobiles,
sum(CASE WHEN telephone NOT LIKE '07%' THEN 1 ELSE 0 END) landlines
FROM tblCalls
WHERE username='myusername' AND YEAR(logdate)='2016'
GROUP BY MONTH(logdate)
I can produce this:
myDate Mobiles Landlines
1 28 47
2 65 98
3 11 17
4 09 14
5 32 8
... and so on
My question is in 2 parts.
1. How do I combine the month and year together and span years (not just 2016).
2. I have another table [tblUsersLogs] which also contains a date [logdate] and a username, I'd like include a count from this table on the number of entries for a specific user grouped by months in the year to produce something like this...
Date Mobiles Landlines UsernameCount
Nov 2016 28 47 50
Dec 2016 65 98 44
Jan 2017 11 17 45
... and so on
Is this possible?
UPDATE (some sample data)
[tblCalls]
logDate telephone
2017-01-04 12:18:36.243 01507443000
2017-01-04 11:23:17.313 07880507000
2017-01-04 11:23:16.760 01216286000
2017-01-04 11:23:15.837 07541360000
2017-01-04 11:23:15.570 01970611000
[tblUserLogs]
logDate username
2017-01-04 12:23:51.530 usera
2017-01-04 12:23:38.350 usera
2017-01-04 12:23:08.530 userb
2017-01-04 12:22:45.020 userc
2017-01-04 12:22:35.437 usera
Hope that helps
Not clear on UserNameCount so I made an assumption and removed the WHERE
Select Date = Format(myDate,'MMM yyyy')
,mobiles = sum(CASE WHEN telephone LIKE '07%' THEN 1 ELSE 0 END)
,landlines = sum(CASE WHEN telephone NOT LIKE '07%' THEN 1 ELSE 0 END)
,UserNamesCount = count(Distinct Usernames)
From tblCalls
Where Year(logdate)=2016
Group By Format(myDate,'MMM yyyy')
Order By Month(myDate)
All you need to do is to change your dates all to the same day of the month and then leave the formatting to your presentation/reporting layer:
SELECT dateadd(m,datediff(m,0,logdate),0) as myDate -- This will return the first day of the month.
,SUM(CASE WHEN telephone LIKE '07%' THEN 1 ELSE 0 END) mobiles
,SUM(CASE WHEN telephone NOT LIKE '07%' THEN 1 ELSE 0 END) landlines
FROM tblCalls
WHERE username = 'myusername' -- Avoid using functions in your WHERE criteria.
AND logdate >= '20160101' -- Doing so makes your filtering much less efficient.
AND logdate < '20170101' -- For more info, google 'SARGability'.
GROUP BY dateadd(m,datediff(m,0,logdate),0)
If you absolutely have to format within your SQL query, you can use:
left(datename(month,logdate),3) + ' ' + cast(year(logdate) as nvarchar(4))
I have two tables Distributors and Orders. I want to get the order counts for each month (INCLUDING 0 counts) I am Grouping by CustId Month and Year.
NOTE : The client is using SQL 2000 :(
This is what I want
DistID Month Year Orders
------------------------------
1 1 2012 4
1 2 2012 13
1 3 2012 5
2 1 2012 3
2 2 2012 0
2 3 2012 0
3 1 2012 8
3 2 2012 0
3 3 2012 3
4 1 2012 1
4 2 2012 0
4 3 2012 1
5 1 2012 6
5 2 2012 6
5 3 2012 0
This is what I get
DistID Month Year Orders
------------------------------
1 1 2012 4
1 2 2012 13
1 3 2012 5
2 1 2012 3
3 1 2012 8
3 3 2012 3
4 1 2012 1
4 3 2012 1
5 1 2012 6
5 2 2012 6
I know why. Its because there isnt a row in the Orders table for certain months. Is there a way to put a count of 0 if there arent any rows in the Orders table for that month and year?
Here is what I have so far
SELECT
D.DistID,
DATEPART(MONTH, Order_Date) AS [Month],
DATEPART(YEAR, Order_Date) AS [Year],
SUM(Total_PV) AS TotalPV,
COUNT(D.DistId) AS Orders
FROM Distributor D
LEFT OUTER JOIN Order O ON D.DistID = O.Distributor_ID
WHERE DATEPART(YEAR, Order_Date) > 2005
GROUP BY DistID, DATEPART(MONTH, Order_Date), DATEPART(YEAR, Order_Date)
Thanks for any input
You could create a table containing all months and years, like:
create table MonthList(year int, month int);
If you fill it with all available years, you can then left join:
select o.distributor_id
, ml.month
, ml.year
, sum(o.total_pv) as totalpv
, count(d.distid) as orders
from monthlist ml
left join
[order] o
on datepart(year, o.order_date) = ml.year
and datepart(month, o.order_date) = ml.month
where ml.year > 2005
group by
o.distributor_id
, ml.month
, ml.year
There is no need to join in Distributor if you don't use columns from that table.
I have following tables
Order
ID OrderDate Quantity Item
1 1 jan 2012 5 A
2 6 jan 2012 10 A
3 9 jan 2012 3 B
4 16 jan 2012 2 C
Order Shipped
ID SuppliedOn Quantity Item OrderId
1 7 Jan 2012 3 A 1
2 9 Jan 2012 2 A 1
3 9 Jan 2012 10 A 2
4 17 jan 2012 3 B 3
I want to list order got and order supplied by week
like
Week Total_Order_got Total_Order_Supplied Item
1st week Jan(2 jan 2012) 15 3 A
2nd Week Jan(9 jan 2012) 0 12 A
2nd Week Jan(9 jan 2012) 3 0 B
3rd Week Jan(16 jan 2012) 0 3 B
3rd Week Jan(16 jan 2012) 2 0 C
Here is the solution:
Select my.Mydate, MAX(ThisWeekStart) as ThisWeekStart , SUM(oqty) as oqty,SUM(sqty) as sqty,item from
(Select DatePart(week,o.orderdate) as Mydate,
dateadd(wk, datediff(wk, 0, o.orderdate), 0) as ThisWeekStart,
o.qty as oqty, 0 as sqty, o.item
FROM [order] o
UNION
SELECT DatePart(week,s.suppliedon) as suppliedon,
dateadd(wk, datediff(wk, 0, s.suppliedon), 0) as ThisWeekStart,
0 as oqty, s.qty as sqty, s.item
FROM supply s) as my
GROUP BY my.Mydate, item
Let me know whether it worked for you.