I need to create size categories in my SQL query from DB2 database.
I was thinking about windowed function but I do not know how to start.
Any thoughts?
You seem to be looking for something like this
select region
,month
,amount
,sum(amount) over (partition by region, month) as sum_amount
from tableA;
You can group by region and month:
select region,month,sum(amount) as amount
from tableA
group by region,month
Hope this helps.
Related
My dataset:
enter image description here
I want to write SQL statement to group by and 1) add Quantity; 2) calculate earliest date based on the group by. In Dax I wrote this:
Table 2 = GROUPBY(
'Table',[Region],"Total Quantity",sumx(CURRENTGROUP(),[Quantity]),"First Available",MinX(CURRENTGROUP(),[Availability])
)
What is the SQL equivalent? Thank you
If the RDBMS is MS-SQL then the equivalent is
select
Region,
SUM(Quantity) as [Total Quantity],
MIN(Availabilty) as [First Available]
from
#t1
group by
Region /*you need to add the grouping columns manually here*/
Nevermind, after trial and error, I solved this myself:
SELECT product, region, min(availability) as 'First Available', sum (quantity) as N from [table2] group by product, region
I am writing a sql query to get sales for different stores on a given day.
The query is run against ingres/vectorwise.
I want to add a column rank where there is the ranking of the store in regard of sales made in comparaison to all the stores.
My select statement is like follows:
SELECT store_number, sum(sales) as sales
FROM stores_sales_indicators
WHERE day = '2019-07-24'
GROUP BY store_number
I tried different things that I am familiar with from sql-server but none of it worked.
I think this is similar to what you're describing (no day included here but you'll get the idea):
declare global temporary table session.stores_sales_indicators
(
store_number integer not null,
sales integer not null
)
on commit preserve rows with norecovery, structure=x100;
insert into session.stores_sales_indicators
values(1,100),(1,200),(2,500),(2,50),(3,50),(3,300);
select
store_number,
sum(sales) as sales,
rank() over (order by sum(sales) desc) as rank
from session.stores_sales_indicators
group by store_number;
See also the fine manual, here's a link to the section on analytic functions:
https://docs.actian.com/vector/5.1/index.html#page/SQLLang%2FAnalytical_Functions.htm
When my sql query returns the results it looks like this:
Driver Amount
A $120.00
A -$5.00
A $10.00
B $90.00
But now I've been requested to add a column to the query results that will present the total for each driver. So it should now look like this:
Driver Amount Subtotal
A $120.00 $125.00
A -$5.00 $125.00
A $10.00 $125.00
B $90.00 $90.00
Possible?
Use window functions.
with cte as (
<your query here>
)
select cte.*,
sum(amount) over (partition by driver) as subtotal
from cte;
You can probably incorporate this logic directly into the query that returns the first results as well.
Sorry that I can't provide you with the full query, but the following link may help accomplish what you need with GROUP BY and ROLLUP, if you're using MS SQL.
https://technet.microsoft.com/en-us/library/bb522495(v=sql.105).aspx
one other way of doing this would be to use CTE (Common Table Expression)
With cteName as (
Select DriverId,
Sum(DriverAmount) As TotalDriverAmount
FROM YourTable
GROUP BY DriverId
),
Select YourTable.DriverId, DriverAmount, cteName.TotalDriverAmount
FROM YourTable LEFT JOIN cteName ON(cteName.DriverId = YourTable.DriverId)
SELECT T.DRIVER, T.AMOUNT, S.TOT [SUBTOTAL] FROM YOURTABLE T JOIN
(SELECT DRIVER, SUM(AMOUNT) TOT FROM YOURTABLE GROUP BY DRIVER ) S
ON T.DRIVER = S.DRIVER
This should work on just about any MS-SQL version.
From
https://www.sqlservercentral.com/Forums/Topic1541134-3412-1.aspx
This is really easy thanks to some (Windowing) functionality born in 2005.
SELECT OrderNo
,Something
,Amount
,SubTotal = SUM(Amount) OVER (PARTITION BY OrderNo)
FROM #TestTable
ORDER BY OrderNo, Something
Create a function that get driver Id, query driver data and return total, call that function in query
Select DriverId, Amount, func(driverId) as TotalAmount
FROM Table
function func(ID){
return(Select sum(amount) from table where driverid=ID);
}
I have a table with structure:
id(INT PK), title(VARCHAR), date(DATE)
How do I select all distinct titles with their earliest date?
Apparently, SELECT DISTINCT title, MIN(date) FROM table doesn't work.
You need to use GROUP BY instead of DISTINCT if you want to use aggregation functions.
SELECT title, MIN(date)
FROM table
GROUP BY title
An aggregate function requires a GROUP BY in standard SQL
This is "Get minimum date per title" in plain language
SELECT title, MIN(date) FROM table GROUP BY title
Most RDBMS and the standard require that column is either in the GROUP BY or in a functions (MIN, COUNT etc): MySQL is the notable exception with some extensions that give unpredictable behaviour
You are missing a GROUP BY here.
SELECT title, MIN (date) FROM table GROUP BY title
Above should fix this. And you don't even need a DISTINCT now.
If you want to get updated records then you can use the following query.
SELECT title, MAX(date) FROM table GROUP BY title
SELECT MIN(Date) AS Date FROM tbl_Employee /*To get First date Of Employee*/
To get the titles for dates greater than a week ago today, use this:
SELECT title, MIN(date_key_no) AS intro_date FROM table HAVING MIN(date_key_no)>= TO_NUMBER(TO_CHAR(SysDate, 'YYYYMMDD')) - 7
SELECT MIN(t.date)
FROM table t
I'm stuck at a very simple t-sql query. Please help !
Following is my result set :
Percentage---FirstName---SessionId
34-----------ABC---------222
67-----------ABC---------333
11-----------ABC---------444
83-----------XYZ---------555
23-----------XYZ---------666
64-----------XYZ---------777
From above, I want records of each user with maximum percentage e.g.
Percentage---FirstName---SessionId
67-----------ABC---------333
83-----------XYZ---------555
I can't seem to do that and I'm in hurry. Please help at the earliest. Any help would be greatly appreciated.
Thanks.
SELECT MAX(Percentage) AS Percentage,
FirstName
FROM mytbl
GROUP BY FirstName
Or if you need session id:
SELECT mytbl.*
FROM mytbl
INNER JOIN (SELECT MAX(Percentage) AS Percentage,
FirstName
FROM mytbl
GROUP BY FirstName) x ON x.Percentage = mytbl.Percentage
AND x.FirstName = mytbl.FirstName
If you need to deal with ties, then you might want to use the windowing functions (assuming you're on SQL Server 2005 or later):
SELECT
*
FROM
(SELECT mytbl.*,RANK() OVER (PARTITION BY FirstName ORDER BY Percentage desc) as rn) t
WHERE
t.rn = 1
So if there are two rows with the same percentage, they'll both be returned. If you only want one result, then you can add additional "tie-breaker" columns to the ORDER BY clause, or switch from RANK() to ROW_NUMBER().