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
Related
I have a table below where I am trying to sum the order_value grouping by the unique order number. However, the queries I've tried are returning the entire sum. Expected return is: 34.24 but actual return is 50.37. How do I get the correct return value?
Order_Number
Order_Value
id
#1005
16.03
1
#1005
16.03
2
#1006
18.21
3
My query:
SELECT order_number,
SUM(order_value)
FROM customer_response
GROUP BY order_number ;
I've also tried:
SELECT SUM(order_value)
FROM customer_response
GROUP BY order_number, order_value
SELECT SUM(order_value)
FROM customer_response
GROUP BY order_number
This seems like it should be simple but I'm just not sure where I'm going wrong.
Very similar to your query but first extract only distinct rows for order_number.
SELECT order_number, SUM(order_value)
FROM
(
select distinct on (order_number) *
from customer_response
) t
GROUP BY order_number;
If you need to get the overall sum for all orders then remove the grouping.
SELECT SUM(order_value)
FROM
(
select distinct on (order_number) *
from customer_response
) t;
SQL Fiddle
Edit
Actually since only one row is left per order_number summing and grouping is meaningless. So the first query becomes simply
select distinct on (order_number) *
from customer_response;
You can try this :
SELECT sum(t.order_avg)
FROM
( SELECT avg(order_value) AS order_avg
FROM customer_response
GROUP BY order_number
) AS t
If you want to get the sum of all distinct Order_Values per Order_Number, you can group by Order_Number to get the sum in each group and then use SUM() window function to get the total:
SELECT DISTINCT SUM(SUM(DISTINCT Order_Value)) OVER () total_value
FROM customer_response t
GROUP BY Order_Number;
See the demo.
Recently during an interview I was asked a question: if I have a table like as below:
The requirement is: how many orders and how many shipments per day (based on date column) - output needs to be like this:
I have written the following code, but interviewer ask me to write a SQL query without JOIN and UNION, achieve the same output.
SELECT
COALESCE(a.order_date, b.ship_date), orders, shipments
FROM
(SELECT
order_date, COUNT(1) AS orders
FROM
table
GROUP BY 1) a
FULL JOIN
(SELECT
ship_date, COUNT(1) AS shipments
FROM table) b ON a.order_date = b.ship_date
Is this possible? Could you guys please advice?
You can use UNION and GROUP BY with conditional aggregation as follows:
SELECT DATE_,
COUNT(CASE WHEN FLAG = 'ORDER' THEN 1 END) AS ORDERS,
COUNT(CASE WHEN FLAG = 'SHIP' THEN 1 END) AS SHIPMENTS
FROM (SELECT ORDER_DATE AS DATE_, 'ORDER' AS FLAG FROM YOUR_TABLE
UNION ALL
SELECT SHIP_DATE AS DATE_, 'SHIP' AS FLAG FROM YOUR_TABLE) T
In BigQuery, I would express this as:
select date, countif(n = 0) as orders, countif(n = 1) as numships
from t cross join
unnest(array[order_date, ship_date]) date with offset n
group by 1
order by date;
The advantage of this approach (over union all) is two-fold. First, it only scans the table once. More importantly, the unnest() is all on the same node where the data resides -- so data does not need to be moved for the unpivot.
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....
I need to add some values with a condition in GoogleBigQuery
NOTICE: I edited the original question since it was not accurate enough.
Thanks to the two participants who have tried to help me.
I tried to apply the solutions kindly suggested by you but I got the same result from the pct column as a result.
Something like this:
results
Here is the more detailed definition:
TABLE
Columns:
Shop: Shop location
brand: Brands of cars sold at shoplocation
sales: sales of each brand sold at each shop_location
rank: Rank of each brand per shop location (the biggest the greater)
total_sales_shop: SUM of all brand sales per shop location
pct: percentage of sales by brand in relationship with shop location
pct_acc:
What i need to calc is pct_acc which is the cumulative sum of the shops by rank (while it has no relation with brand)
PCT_ACC
My need is to reach something like PCT_ACC, and then save the results in another one like this:endtable
You can use following query to get the required data:
select values, rank,
sum(case when rank<2 then values else 0 end) condition1
from table
group by values, rank
Need to add/remove columns from select and group by as per requirement
To get the cumulative sum you can use following query:
select shop, brand, sales, rank, total_sales_shop, pct ,
sum(pct) over (partition by shop order by rank) as pct_act
from data
And to get the final table you can use combination of case statement and group by
e.g
select shop,
max(case when rank=1 then pct_act end) as rank_1,
max(case when rank=2 then pct_act end) as rank_2,
max(case when rank=3 then pct_act end) as rank_3,
max(case when rank=4 then pct_act end) as rank_4,
max(case when rank=5 then pct_act end) as rank_5
from cumulative_sum
group by shop
If you want only the final sum for the rows with that condition you can try:
SELECT
SUM (IF(Rank < 2, Values, 0) AS condition1
FROM table
If you want to get the rank and the sum only for the rows with that condition you can try doing
SELECT
Rank,
SUM (IF(Rank < 2, Values, 0) AS condition1
FROM table
WHERE
RANK < 2
GROUP BY
Rank
Finally, if you want to get the rank and the sum considering all the rows you can try doing:
SELECT
Rank,
SUM (IF(Rank < 2, Values, 0) AS condition1
FROM table
GROUP BY
Rank
I hope it helps
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