i friend i have a database table
amount_id,
year,
amount,
and data like this
1 2002 2
2 2002 3
3 2007 2
4 2004 6
5 2004 10
i wan to run a query to select data like this
2002 4
2007 2
2004 16
thanks for help
SELECT `year`, SUM(amount)
FROM databasetable
GROUP BY `year`
Should be all you need.
It looks like you need to use a sum(amount) in your select statement and group by year after your where.
Related
I'm trying to build a query for a MS SQL database that will find records with more than one year but not the records with only one.
Lets say I have a car dealership and I have 1 Chevy from 2015 and 2 from 2017 then I would want to find Chevy 2015 1 and chevy 2017 2 but if I have a three Fords from 2018 and only 2018 then I don't want that at all.
I have tweeked with groups and joins but I don't get any where. So I need Select from table something. I'm leaning toward a pivot table but not sure what to do. Thanks for the help
MyTable Contents
Model year count
Chevy 2012 1
Chevy 2012 1
Chevy 2015 1
Ford 2018 1
Ford 2018 1
Ford 2018 1
Buick 2017 1
Lexus 2017 1
Lexus 2015 1
Desired Result Set
Chevy 2012 2
Chevy 2015 1
Lexus 2017 1
Lexus 2015 1
Because it has 2 different years for the model
The below query should help you. Need not hardcode model values.
Select T.Model,T.[year] ,count(T.[year])
from T
join (select distinct * from T) S on T.model = S.model and T.year!=S.year
group by T.Model,T.[year]
You need to use SUM function and group by on subquery,Because there might be Multiple count on count column. then join itself and distinct to exclude duplicate data.
Select distinct t1.*
from (
SELECT Model,[year] ,sum([count]) totle
FROM T
group by Model,[year]
) t1
inner join T t2 on t1.Model = t2.Model and t1.[year] !=t2.[year]
sqlfiddle:http://sqlfiddle.com/#!18/e8756/55
Note:[table],[year] are keyword in sql avoid naming it as column name
i have a table calculating the installments. in that table i'm saving all the data recording to that. For example if i'm calculating for 60 installments and saving all the data,so it is like 60 months. so now i need to sum up the value of one column for every 12 months. sometimes v start paying the installments from the middle of the year also.
my DB looks like this.the highlighted column must sum up for every 12 months. two images are one table only
suppose i have 30 installments from starting on jun 2012.suppose i started paying installment from jun 2012 then should sum up the installments from jun 2012 to may 2013. v can't use group by year. i must sum up like this ................................................................................
sum jun 2012 to may 2013
sum jun 2013 to may 2014
sum jun 2014 to nov 2014 ( only 6 months left)
You can use ROW_NUMBER to generate a group of 12 months:
WITH Cte AS(
SELECT *,
RN = (ROW_NUMBER() OVER(ORDER BY InstallmentMonth) - 1)/ 12
FROM your_table
)
SELECT
SUM(InteresetPerInstallment)
FROM Cte
GROUP BY RN
I have an SQL query in SQL Server 2014 that outputs the following (extract shown, real output is around 45,000 records):
ResaID Agency Sales MTH Market Property
235 Smith 500 February 2015 UK RAV
451 John 1600 February 2015 France PLN
258 Alan 800 January 2015 UK BLS
I need an SQL Query that will RANK the agency column based on the following criteria: MTH, Market and Property and give me the following output (fictitious ranking shown below):
ResaId Rank
235 10
451 2
258 9
I will then use a JOIN based on ResaID to join the "Rank output" with my initial query.
In simpler terms, the ranking of the Agency will need to be done after grouping MTH, Market and Property.
Can this be achieved using T-SQL syntax?
Edit: I want the ranking to be done based on the Sales amount.
Yes, you can write something like this:
SELECT *, RANK() OVER(PARTITION BY Agency, mht ORDER BY sales DESC)
FROM [yourTable]
In SQL Server 2012, I am tring to recreate a detailed sales transaction record from two tables that have historical summary information but can't seem to limit the records based on an customer start date. (Actually there are 3 tables, one with customer item categories and % of sales by category, but I'm not having trouble with that part of the cross join). Any help would be appreciated.
Imagine two table:
Customer
ID Customername Sales_Monthly Date_start
1 Acme $80,000.00 1/15/2012
2 Universal $50,000.00 1/3/2013
3 SuperMart $12,000.00 4/14/2013
Calendar
ID Date
1 1 /31/2014
2 2 /28/2014
3 3 /31/2014
4 4 /30/2014
5 5 /30/2014
6 6 /30/2014
7 7 /30/2014
8 8 /30/2014
9 9 /30/2014
10 10/30/2014
11 11/30/2014
12 12/30/2014
A simple cross join:
SELECT Calendar.Date, Customer.ID, Customer.Customername, Customer.Sales_2013
FROM Calendar, Customer
produces 36 entries as you'd expect (3 customers x 12 months)
However, I only want to produce entries 28 entries, where [Calendar.Date] > [Customer.Date_start]
I can't seem to find WHERE CLAUSE and any join type or subquery that will limit my records based on the Customer.Date_start field. Any suggestions on this?
If you're joining on a field in a cross join, it's no longer a cross join. Assuming your customer data in the question is incorrect, and you meant it to be 2014 on all the customer records, you can do the join like this.
SELECT *
FROM Customer a
JOIN Calendar b ON b.Date > a.Date_start
This produces 33 rows (12 for customer 1, 12 for customer 2, and 9 for customer 3, not 28 like you're expecting), but hopefully my answer will point you in the right direction.
I have the following report I need to generate . I am new to SSRS.
Number of Stores with June July Aug
0 sales 12 34 32
1 to 9 Sales 12 34 45
10+ sales 15 45 54
The tables I have are as follows :
Store :
YEAR MONTH StoreName
2013 10 ABC
2013 10 DEF
2013 09 JKL
2013 06 FGH
Store Sales : (Contains only stores whose sales are > 0)
YEAR MONTH StoreName NumberOfSales
2013 10 ABC 3
2013 09 JKH 14
2013 10 FRH 9
I am not really sure what I need to do to get the report in the above format ?
I can write a query for sales in a single month , but how do I write a query to
get the report for all 3 months ? Or is there a better way to do these reports using ssrs ?
If you can direct me on what to do it will be great ?
You want to set up your query like this:
SELECT Year, Month, (CASE
WHEN NumberOfSales = 0 THEN '0 Sales'
WHEN NumberOfSales BETWEEN 1 AND 9 THEN '1-9 Sales'
WHEN NumberOfSales > 9 THEN '10+ Sales' END) [SaleGroup]
FROM FactStoreSales
Then you basically want to go into SSRS and just throw it into a table with the month in the columns and the group in the rows and let it pivot the data itself rather than trying to do that in SQL.