I was wondering what method is most used when creating a query that displays sales for two different months (selected in the parameter).
My database looks something like this:
Posting Date Company Size Sales
01/01/2011 Microsoft 1000 900
I already have a parameter where "year month" is selected.
What I want is to have two parameters so that I can compare the sales in "year month" side by side in Microsoft Visual Studio.
So the query should have two parameters, #PostingDate1 and #PostingDate2
Thanks for any help!
--UPDATE--
Trying to make this more understandable.
The two parameters to select from will be "year month"
So that the result table will look like this when "year month" is selected for parameter 1: january 2011, and parameter 2: february 2011 (doesn´t matter what months are selected, just that the results will show the different months)
Company Size Sales1 Sales2
Microsoft 1000 100 200
That is if sales for january 2011 was 100
and sales for february 2011 was 200
I think that you want to do a CROSS JOIN, but I'm not completely sure that I understood your question. If the results of your query are onle ONE row, then I recommend a CROSS JOIN, of not, then its probably better not to use it. It should be something like this:
SELECT A.[Posting Date] [Posting Date 1], A.Company Company1, A.Size Size1, A.Sales Sales1,
B.[Posting Date] [Posting Date 2], B.Company Company2, B.Size Size2, B.Sales Sales2
FROM (SELECT [Posting Date], Company, Size, Sales
FROM YourTable
WHERE [Posting Date] = #PostingDate1) A
CROSS JOIN (SELECT [Posting Date], Company, Size, Sales
FROM YourTable
WHERE [Posting Date] = #PostingDate2) B
My answer:
I ended up using "UNION", don´t know whether this is more appropriate, but it got rid of the redundant data from using "CROSS JOIN".
SELECT
A.Company, A.Size, SUM(A.Sales) as Sales1, SUM(B.Sales2)
FROM
(
(SELECT Company, Size, Sales as Sales, 0 as Sales2
FROM Sales
WHERE Posting date = #PostingDate1) AS A
UNION
(SELECT Company, Size, 0 as Sales, Sales as Sales2
FROM Sales
WHERE Posting date = #PostingDate2)
) AS B
)
GROUP BY
A.Company, A.Size
Related
Pretty new to SQL here. Thank you so much to anyone reading this.
I have a table
ProductID, Year, Sales
------------------------
Product1 2019 100
Product1 2018 50
With a lot of products, but the two years are always 2019 and 2018.
I need to show the 100 products that had the biggest % increase in sales.
I believe that this requires "pivoting" the data, so that you can calculate the 2019 (as one column) and 2018 (second column) difference in a third column called "% increase."
But I'm totally stuck--Ive never done anything this difficult in SQL before.
Help?
Another solution would be this, which would help you if there are more than one record per product - year:
SELECT
ProductId,
Sales_2018,
Sales_2019,
(Sales_2019 - Sales_2018) / Sales_2018 * 100 AS Percentage_increase
FROM (SELECT
ProductID,
SUM(CASE WHEN Year = 2018 THEN Sales END) AS Sales_2018,
SUM(CASE WHEN Year = 2019 THEN Sales END) AS Sales_2019
FROM TABLE
GROUP BY ProductID)
ORDER BY (Sales_2019 - Sales_2018) / Sales_2018 DESC
You can do this with self-join on almost any SQL engine.
Please find the sample implementation below.
You may need to fix some depending on the dialect that you use.
SELECT
a.ProductId,
(1.0 * b.Sales / a.Sales - 1.0) AS IncreaseRate
FROM
tbl AS a
INNER JOIN
tbl AS b
ON
a.ProductId = b.ProductId
AND a.year = 2018
AND b.year = 2019
ORDER BY
IncreaseRate DESC
LIMIT 100
I am trying to produce a query result, that allows for the sales to appear in two columns. One column for 2012 and one for 2013. I am able to yield the results I am lookingn for but it is producing them in three columns. One with two nulls, one with a null in 2012, and one in 2013.
I have tried adding a group by in SQL query, and also moving the whole query to the from statement, but I am rather new to sub queries.
select distinct
ResellerName,
(Select Sum(Salesamount) where Year(Orderdate) = 2012) as "2012 sales" , (Select Sum(Salesamount) where Year(Orderdate) = 2013) as "2013 sales"
from [dbo].[DimReseller] as R, [dbo].[FactResellerSales] as S
where R.ResellerKey = s.resellerkey
group by ResellerName, Year(OrderDate)
Expected Results: [ResellerName], [2012 Sales], [2013 Sales] in one column
Actual results: Three Columns are produced where they each have a null.
I guess you need a simple conditional aggregation (with proper explicit join syntax) -
SELECT ResellerName
,SUM(CASE WHEN Year(Orderdate) = 2012 THEN Salesamount END) as "2012 sales"
,SUM(CASE WHEN Year(Orderdate) = 2013 THEN Salesamount END) as "2013 sales"
FROM [dbo].[DimReseller] as R
JOIN [dbo].[FactResellerSales] as S ON R.ResellerKey = s.resellerkey
GROUP BY ResellerName
I need to drill into detail on to an existing report which shows me the total profitability per customer, per month on a telecoms company.
The company sells calls, service charges and has discounts.
This is the query that I'm using, using one customer in particular:
SELECT
custumer_name,
ISNULL(SUM(CASE CONVERT(VARCHAR(10), month_end, 103)
WHEN '31/10/2016'
THEN totcall + isnull(totrec, 0) - discount
ELSE 0
END), 0) AS '31/10/2016',
SUM(totcall) AS 'Total Calls',
SUM(totrec) AS 'Total Rec',
SUM(discount) AS 'Discounts'
FROM
total_sales
INNER JOIN
customer_details ON billingaddress = customer_details .siteid
INNER JOIN
sales_month d ON total_sales.periodid = d.monthid
INNER JOIN
customer b ON customer_details .id = b.id AND b.is_customer = 1
WHERE
b.custumer_name = '2
GROUP BY
b.custumer_name
ORDER BY
b.custumer_name ASC
This is bringing back the correct total however when I need to show the drilldown of the total on calls, rec & discounts it is showing me the actual sum of every months' data which is stored on that table.
I'm not sure how can I get the last 3 columns to itemise the total without specifying the actual month (as the report has columns for the last 12 months of data.
Please help?
Thank you!
PS. The DB is a SQL Server 2008
The SQL below give me the columns for Account, Name and the Total Sale for the year of 2015.But how, if possible, can I add another column for the previous year of 2014 ?
select a.AcctNo, b.Name, Sum(a.TotSold) as [ Total Sold ]
from Orders as A
Join Accounts as b on a.AcctNo = b.AcctNo
where (a.PurchaseDate between '1/1/2015' and '12/31/2015' )
Group by a.AcctNo, b.Name
You can use conditional aggregation:
select o.AcctNo, a.Name,
Sum(case when year(PurchaseDate) = 2015 then TotSold else 0 end) as tot_2015,
Sum(case when year(PurchaseDate) = 2014 then TotSold else 0 end) as tot_2014
from Orders o Join
Accounts a
on a.AcctNo = o.AcctNo
where PurchaseDate >= '2014-01-01'
PurchaseDate < '2016-01-01'
Group by o.AcctNo, a.Name ;
Notes:
Use ISO standard formats for date constants.
When using dates, try not to use between. It doesn't work as expected when there is a time component. The above inequalities work regardless of a time component.
Use table aliases that are abbreviations of the table name. That makes the query easier to follow.
I have the following query which provides me with the item and item details, values, rate and quantity across each location.
I am trying to get the yearly revenue based on the Start and End Date. Example, if the chosen date was 2013-2015. The final result will create 3 columns one for 2013 revenue, one for 2014 revenue and one for 2015 revenue.
I am a newbie and still not an expert in writing queries, but here is what I have currently:
SELECT
department,
item,
itemdesc,
qty1,
qty2,
rate_1,
rate_2,
SUM(mm.days*mm.rate*mm.qty)
FROM
items it
LEFT JOIN
(SELECT
i.days, i.rate, i.days, ii.todate, ii.itemid
FROM
invoiceofitems ii
JOIN
invoices i on i.id = ii.id
WHERE
ii.todate BETWEEN #StartDate and #EndDate) mm ON mm.itemid = it.itemid
GROUP BY
department,
item,
itemdesc,
qty1, qty2,
rate_1, rate_2
ORDER BY
item
However, this does not provide me with a year to year aggregation of invoice revenue that I require.
I know this is possible to achieve via iterating through this. But how would I accomplish this and where would I start on this?
Would I need to know the start and end date of each year and iterate through that and then add a counter to the year until year= EndDate?
I'm extremely confused. Help would be appreciated.
I hope that PIVOT and YEAR help you to solve this problem (some columns are omitted):
;WITH SRC(department,item, ... , rate_2, yr, calculation) AS
(SELECT it.department, it.item, ..., it.rate_2, YEAR(ii.todate) as yr,
(i.days * i.rate *i.qty) as calculation
FROM items it
LEFT JOIN invoiceofitems ii ON ii.itemid = it.itemid
JOIN invoices i ON i.id = ii.id)
SELECT department,item, ..., [2013],[2014],[2015]
FROM SRC
PIVOT
(SUM(calculation) FOR yr IN ([2013],[2014],[2015])) PVT
The YEAR function returns only 'year' part of your date and makes grouping easier. PIVOT just rotates grouped data from rows to columns.