i want to show these records column wise for particular month and year, like below table format
Source Total
Organic 1252
Paid 121
Email Campaign 121
Total 1494
select Organic,Paid ,EmailCampaign ,Total from tbl_leads where Month='Aug' and Year='2015'
below is sample date
Organic Paid EmailCampaign Total ProjectName Month Year
4444 5555 2222 1111 demo project Feb 2015
1252 121 121 1494 debug test Aug 2015
In Sql Server you can use Cross Apply with Tabled Valued Constructor to unpivot the data
SELECT cs.Source,
cs.Total
FROM tbl_leads
CROSS apply (VALUES ('Organic',Organic),
('Paid',Paid),
('EmailCampaign',EmailCampaign),
('Total',Total)) cs(Source, Total)
WHERE Month = 'Aug'
AND Year = '2015'
Or Generic Sql solution
SELECT 'Organic' AS Source,
Organic AS Total
FROM tbl_leads
UNION ALL
SELECT 'Paid',
Paid
FROM tbl_leads
UNION ALL
SELECT 'EmailCampaign',
EmailCampaign
FROM tbl_leads
UNION ALL
SELECT 'Total',
Total
FROM tbl_leads
Related
I have recently started working with SQL and I am trying to write a query that will display all cities that has the highest volume and listing on a yearly basis.
this query is able to handle the year 2011 alone
select city, year, month,max(volume),max(listings)
from kaydata
where year = 2011
result
city year month max(volume) max(listings)
0 Abilene 2011 2 6505000.0 746.0
sample data
city year month volume listings
0 modak 2011 1 5380000.0 701.0
1 Abilene 2011 2 6505000.0 746.0
2 ipetu 2010 3 9285000.0 784.0
2 oyog 2010 4 7085000.0 204.0
desired result
city year month max(volume) max(listings)
0 Abilene 2011 2 6505000.0 746.0
1 ipetu 2010 3 9285000.0 784.0
If I understand correctly, this would achieve what you're after:
select city,year,month,volume,maxlisting from (
select * , row_number() over (partition by year order by volume desc) rn
, max(listing) over (partition by year) maxlisting
from kaydata
) t
where rn = 1
Im trying to select only ids of customers that have ordered atleast once every year in a specific time period for example 2010 - 2017
example:
1. customer ordered in 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 should be shown
2. customer ordered in 2010, 2011, 2012,2013,2014,2015, 2017 should not be shown
my query counts in all years not within the period
o_id o_c_id o_type o_date
1345 13 TA 2015-01-01
7499 13 TA 2015-01-16
7521 14 GA 2015-01-08
7566 14 TA 2016-01-24
7654 16 FB 2016-01-28
c_id c_name c_email
13 Anderson example#gmail.com
14 Pegasus example#gmail.com
15 Miguel example#gmail.com
16 Megan example#gmail.com
my query:
select c.id, c.name, count(*) as counts, year(o.date)
from orders o
join customer c on o.c_id=c.id
where year(o.date) > 2009
group oy c.id
having count(*) > 7
You need a table with all the years so you can check if user order that year. I create a sample with only two years because that is what in your sample data.
You can use this to create a list of years:
How to get list of dates between two dates in mysql select query
Also I use ranges for years so you can use index at the moment of the join.
If you already have a table users you can replace the subquery
SQL DEMO
SELECT user_id, COUNT(o_id) as total_years
FROM years y
CROSS JOIN (SELECT DISTINCT `o_c_id` as `user_id` FROM `orders`) as users
LEFT JOIN orders o
ON o.`o_date` >= y.`year_begin`
AND o.`o_date` < y.`year_end`
AND o.`o_c_id` = `user_id`
GROUP BY user_id
HAVING total_years = (SELECT COUNT(*) FROM years)
;
I want to join two tables, and display year(date),total and vehicle type.
If year and vehicle_type is the same then their total should be combined. This is my query.
select extract(year from ra.roadAccident_date) as 'Year',
c.casualties_death + c.casualties_serious + c.casualties_minor as 'Total',
ra.vehicle_type as 'Types of Vehicle'
from casualties c
join roadAccidents ra
on (c.accident_id = ra.accident_id)
My results are
Year Total Types of Vehicle
2014 6 taxi
2014 9 lorry
2014 3 bus
2014 16 bus
2015 7 taxi
2015 5 lorry
2015 7 lorry
2016 2 bus
2016 5 lorry
2016 9 bus
For 2014, i want vehicle type bus to be combined to one row with total 19. I tried multiple group by methods but could not find the one I am looking for.
I don't see why a simple GROUP BY won't work:
SELECT
EXTRACT(YEAR FROM ra.roadAccident_date) AS 'Year',
SUM(c.casualties_death + c.casualties_serious + c.casualties_minor) AS 'Total',
ra.vehicle_type AS 'Types of Vehicle'
FROM casualties c
INNER JOIN roadAccidents ra
USING accident_id
GROUP BY ra.vehicle_type, YEAR(ra.roadAccident_date)
Wrap the whole thing (as a subquery) with a grouping outer one, like
Select yr,vtype, sum(total) as tot from
(
select extract(year from ra.roadAccident_date) as yr,
c.casualties_death +c.casualties_serious+c.casualties_minor as total
ra.vehicle_type as vtype
from casualties c
join roadAccidents ra on (c.accident_id = ra.accident_id)
) stats group by yr, vtype
I have following data , numbers recruited/applied for a particular office, and would like to find the difference between previous and current year and their percentage increase or decrease.
Mentioned the formulas to use in brackets in expected output.
Office year recruited applied
Pune 2015 10 15
Pune 2016 7 20
Mumbai 2015 10 23
Mumbai 2016 15 18
My expected output should be like:
Office Difference %recruited
Pune -3 (7-10) -30%(7-10/10)
Mumbai 5(15-10) 50%
Please help.
If you were using SQL Server 2012 or higher you could use the LAG (or LEAD) function. Since you aren't you can get creative with a CTE. This approach is taken from http://blog.sqlauthority.com/2013/09/22/sql-server-how-to-access-the-previous-row-and-next-row-value-in-select-statement/.
SELECT 'Pune' AS Office,
'2015' AS year,
10 AS recruited,
15 AS applied
INTO #Temp
UNION
SELECT 'Pune' AS Office,
'2016' AS year,
7 AS recruited,
20 AS applied
UNION
SELECT 'Mumbai' AS Office,
'2015' AS year,
10 AS recruited,
23 AS applied
UNION
SELECT 'Mumbai' AS Office,
'2016' AS year,
15 AS recruited,
18 AS applied;
WITH cte AS (
SELECT rownum = ROW_NUMBER() OVER (PARTITION BY t.office ORDER BY t.year), * FROM #Temp t)
SELECT cte.office, cte.recruited - prv.recruited AS DifferenceRecruited,
((cte.recruited - prv.recruited) / CONVERT(FLOAT, prv.recruited) * 100) AS RecruitedChangePercentage,
cte.applied - prv.applied AS DifferenceApplied,
((cte.applied - prv.applied) / CONVERT(FLOAT, prv.applied) * 100) AS AppliedChangePercentage
FROM cte
LEFT JOIN cte prv ON prv.Office = cte.office AND prv.rownum = cte.rownum - 1
WHERE prv.recruited IS NOT null
ORDER BY cte.Office DESC
Hope this helps.
I have two queries that give respectively the number of working unit bought, and the number of working unit consumed by a client.
I am working on a SQL Server 2014
The WUBought query returns something like this example :
Customer Year Month UnitBought
Cust1 2015 6 50
Cust2 2014 7 100
Cust1 2013 10 30
Cust3 2015 2 40
The other query returns the number that were consumed by a client :
Customer Year Month UnitConsumed
Cust1 2015 2 6
Cust1 2015 5 20
Cust2 2015 3 8
Cust1 2015 4 3
Cust3 2015 2 10
What I am basically trying to do, is a sum of what has been bought for every month, minus what has been consumed. Here is an example of what I want as a result for the first six months for Cust1 :
Customer Year Month Remaining
Cust1 2015 1 30
Cust1 2015 2 24
Cust2 2015 3 24
Cust1 2015 4 21
Cust3 2015 5 1
Cust3 2015 6 51
The query that returns the WU bought with a UNION ALL from a table that lists every month, to get each month even if there is no value :
SELECT Customer, [Year], [Month], SUM(UOBought) AS UORest
FROM WU_Bought
GROUP BY [Customer], [PurchaseDate]
UNION ALL
SELECT '' AS Customer, [Year], [Month], '' AS UORest
FROM Months
GROUP BY [Year], [Month]
Here is the query that sums every bought unit every month, with the same union statement :
SELECT Customer, [Year], [Month], SUM(TotalConsumed) * -1 AS UORest
FROM WUConsumed
GROUP BY Customer, Year, Month
UNION ALL
SELECT '' AS Customer, [Year], [Month], '' AS UORest
FROM EveryMonths
GROUP BY Year, Month
Right now I think I must adjust the first one, forcing it to keep the previous sum, but I am not sure how I can do that.
Does this work for you?
SELECT b.customer_id, b.year, b.month, SUM(b.units_bought) AS units_bought, ISNULL(c.units_consumed,0) AS units_consumed, SUM(b.units_bought) - ISNULL(c.units_consumed,0) AS units_remaining
FROM Bought b
LEFT JOIN Consumed c
ON b.customer_id = c.customer_id AND b.year = c.year AND b.month = c.month
GROUP BY b.customer_id, b.year, b.month
Ok, I got it working.
What I did was really "simple", using a SQL Server feature, available since 2012 :
ROWS UNBOUNDED PRECEDING
Here is a pretty clear article about this feature.
I created an other view grouping the results from the queries about consumed and bought units with a UNION ALL clause, called "WU_Closing_View", then used the ROWS UNBOUNDED PRECEDING within it :
SELECT Customer, Year, Month, SUM(Closing) OVER(PARTITION BY Customer ORDER BY Year, Month ROWS UNBOUNDED PRECEDING) AS Closing
FROM WU_Closing_View
GROUP BY Customer, Year, Month, Closing
UNION ALL
SELECT '' AS Customer, Year, Month, '' AS Sum_bought
FROM Months
GROUP BY Year, Month
ORDER BY Customer, Year, Month
Note that I used PARTITION BY, in order to sum by client. Because I wanted to show every month in a SSRS matrix, I added a "UNION ALL" pointing to a table that has every year and month on it for an empty client, from 2010 to 2017. But it is optional if you don't need the evolution for every month.
There may be an easier way, but that's the one I found so far.