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
Related
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.
I have a situation where I need to get DISTINCT values from column "note" and then get the SUM of "price" for above records.
I tried with different queries but none of them are working fine.
SELECT DISTINCT note ,price( select SUM (price) FROM [tableName] where Archived ='0'
SELECT sum(price),(SELECT DISTINCT note , price from [tableName] where Archived ='0')
In a nutshell I need to get the sum of prices for the distinct records.
Are you looking for group by?
SELECT note, SUM(price)
FROM [tableName]
WHERE Archived = '0'
GROUP BY note;
If you want the sum over ALL the records, just use window functions like this:
SELECT note, SUM(price) as note_sum, SUM(SUM(price)) OVER () as total_sum
FROM [tableName]
WHERE Archived = '0'
GROUP BY note;
Maxbe this will be one way to do it:
select distinct sum(price) over(partition by note), note
from tablename
where Archived = 0
Here is a demo on SQLServer
If I have understood you correctly you need distinct note values and only one sum for all of them ... then something like this:
select distinct note, (select sum(price) from tablename) sum_tot
from tablename
where Archived = 0
P.S. do add expected result....
In the data tabel when the PRICE_TYPE = MSRP, the amount should be added to column msrp and when PRICE_TYPE = SELP the amount should be added selp column,
How can i write a query to get the above task done , thanks in advance.
the first image should be the desired output.
One of the ways to get your expected result:
select MATERIAL_NUMBER, [MSRP],[SELP]
FROM
(
SELECT
MATERIAL_NUMBER,
PriceType,
Sum(Amount)
FROM Org_table
GROUP BY
MATERIAL_NUMBER,
PriceType
) tbl
pivot
(
sum(Amount)
for PriceType in ([MSRP],[SELP])
)
hope it helps :)
You seem like want to get pivot, you can try to use condition aggregate function.
CASE WHEN with aggregate function.
SELECT MATERIAL_NUMBER,
SUM(CASE WHEN PRICE_TYPE = 'MSRP' THEN AMOUNT END ) MSRP,
SUM(CASE WHEN PRICE_TYPE = 'SELP' THEN AMOUNT END ) SELP
FROM T
GROUP BY
MATERIAL_NUMBER
sqlfiddle
NOTE
because your Amount seem like a float value, some I use SUM
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 want to retrieve all information as well as sum of amount by the name so i use this query for that purpose.
select SUM(Amount)as total,RecieptNo,Name,UniqueId,Date,Amount,LateFee,Other from Amount where Name='Shaikh'
but this error is occured.
Column 'Amount.RecieptNo' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
so please help me.
Select RecieptNo, Name
, UniqueId, Date
, Amount
, LateFee, Other
, ( Select Sum( A1.Amount )
From Amount As A1
Where A1.Name = A.Name ) As Total
From Amount As A
Where Name='Shaikh'
Another choice available in SQL Server 2005+:
Select RecieptNo, Name
, UniqueId, Date
, Amount
, LateFee, Other
, Sum( Amount ) Over( Partition By Name ) As Total
From Amount As A
Where Name='Shaikh'
select SUM(Amount)as total,RecieptNo,Name,UniqueId,Date,Amount,LateFee,Other from Amount
group by RecieptNo,Name,UniqueId,Date,Amount,LateFee,Other having Name='Shaikh'
When using aggregate SQL functions such as SUM(), you have to use a GROUP BY clause. The example below demonstrates that. This may not be the most ideal approach to tackling your query however. What I've done is added a group by clause that includes all columns that you've selected other than your SUM. For example:
select RecieptNo, Total, UniqueId, Date, Amount, LateFee, Other SUM(Amount)as total from
Amount where Name='Shaikh' group by RecieptNo, Total, UniqueId, Date, Amount, LateFee, Other
You need to group by
select SUM(Amount)as total,Name from Amount where Name='Shaikh' group by Name
If you want to add other thing in select list you require to add this field in you group by clause also otherwise it will give error