SUM in SQL Server 2014 - sql

Can you help me to make the sum of all the values ​​of the "Total_Profit" row that is in partition 1 of my database.
select
Total_Profit,
$partition.FUNCIONDEPARTICION2(Order_Priority)
from
cliente
where
$partition.FUNCIONDEPARTICION2(Order_Priority) = 1
In this query I already show all the values ​​but I don't know how to add them

Just use SUM():
select SUM(Total_Profit)
from cliente
where $partition.FUNCIONDEPARTICION2(Order_Priority) = 1

Related

How can I convert 2 row into column in tsql?

I have 2 row data which I want to make it to be 2 column,
I tried union syntax but it didn't work.
Here is the data I have:
breed 1 breed2
I tried to convert it with this sql
select a.breed union a.breed
but it didn't work.
Here is what you want from the SQL:
breed1,breed2
SELECT
[breed1],
[breed2]]
FROM
(
SELECT 'breed1' myColumn
union
select 'breed2'
) AS SourceTable
PIVOT
(
AVG(mySecondColumn) FOR
myColumn IN ([breed1], [breed2]])
) AS PivotTable;
You can use a self join. This needs a way to pair rows together (so if you have four rows you get 1 and 2 in one result and 3 and 4 in the other rather than another combination).
I'm going to assume you have sequentially numbered rows in an Id column and an odd numbered row is paired with the one greater even Id:
select odd.Data as 'First', even.Data as 'Second'
from TheData odd
inner join TheData even on odd.Id+1 = even.Id
where odd.Id % 2 = 1;
More generally for more columns use of pivot is more flexible.
How about an aggregation query?
select min(breed) as breed1, max(breed) as breed2
from t;

SUM Query on sql server

I am newbie, I am very confused how to do sum query. Here is my query :
and this is an output from my query :
all i want is Sum my presentase field by month..
i already created sum query, but all i gonna got by my query just error code :((( ..
here my sum query :
SUM(IF( YEAR(PO_HARI) = 2016, PRESENTASE_SATUAN, 0)) AS TOTAL_JANUARY
and also i added TOTAL_JANUARY On my group_by but didnt work.
thank you for attention .
try this
select year(po_date) yeardate, month(po_date) monthdate,
sum(case Point_del
when 1 then 25
when 2 then 50
when 3 then 75
when 4 then 100
else 0
) TOTAL_JANUARY
from jbox2_po..POT01A
where month(po_date)=1
group by year(po_date) , month(po_date)
you can add 'and year(po_date)=2016' into clause where if you want only 2016 year
IF youre using sql server management studio then try this :::
select sum(presentase) from tablename
group by month(PO_HARI)
--here i am assuming that "presentase" is a summable field and 'po_hari' is a date column

SQL: Select Top 2 Query is Excluding Records with more than 2 Records

I just joined after having a problem writing a query in MS Access. I am trying to write a query that will pull out the first two valid samples in from a list of replicated sample results and then would like to average the sample values. I have written a query that does pull samples with only two valid samples and averages these values. However, my query doesn't pull samples where there are more than two valid sample results. Here's my query:
SELECT temp_platevalid_table.samp_name AS samp_name, avg (temp_platevalid_table.mean_conc) AS fin_avg, count(temp_platevalid_table.samp_valid) AS sample_count
FROM Temp_PlateValid_table
WHERE (Temp_PlateValid_table.id In (SELECT TOP 2 S.id
FROM Temp_PlateValid_table as S
WHERE S.samp_name = S.samp_name and s.samp_valid=1 and S.samp_valid=1
ORDER BY ID))
GROUP BY Temp_PlateValid_table.samp_name
HAVING ((Count(Temp_PlateValid_table.samp_valid))=2)
ORDER BY Temp_PlateValid_table.samp_name;
Here's an example of what I'm trying to do:
ID Samp_Name Samp_Valid Mean_Conc
1 54d2d2 1 15
2 54d2d2 1 20
3 54d2d2 1 25
The average mean_conc should be 17.5, however, with my current query, I wouldn't receive a value at all for 54d2d2. Is there a way to tweak my query so that I get a value for samples that have more than two valid values? Please note that I'm using MS Access, so I don't think I can use fancier SQL code (partition by, etc.).
Thanks in advance for your help!
Is this what you want?
select pv.samp_name, avg(pv.value_conc)
from Temp_PlateValid_table pv
where pv.samp_valid = 1 and
pv.id in (select top 2 id
from Temp_PlateValid_table as pv2
where pv2.samp_name = pv.samp_name and pv2.samp_valid = 1
)
group by pv.samp_name;
You might need avg(pv.value_conc * 1.0).

Adding a percent column to MS Access Query

I'm trying to add a column which calculates percentages of different products in MS Access Query. Basically, this is the structure of the query that I'm trying to reach:
Product |
Total |
Percentage
Prod1 |
15 |
21.13%
Prod2 |
23 |
32.39%
Prod3 |
33 |
46.48%
Product |
71 |
100%
The formula for finding the percent I use is: ([Total Q of a Product]/[Totals of all Products])*100, but when I try to use the expression builder (since my SQL skills are basic) in MS Access to calculate it..
= [CountOfProcuts] / Sum([CountOfProducts])
..I receive an error message "Cannot have aggregate function in GROUP BY clause.. (and the expression goes here)". I also tried the option with two queries: one that calculates only the totals and another that use the first one to calculate the percentages, but the result was the same.
I'll be grateful if someone can help me with this.
You can get all but the last row of your desired output with this query.
SELECT
y.Product,
y.Total,
Format((y.Total/sub.SumOfTotal),'#.##%') AS Percentage
FROM
YourTable AS y,
(
SELECT Sum(Total) AS SumOfTotal
FROM YourTable
) AS sub;
Since that query does not include a JOIN or WHERE condition, it returns a cross join between the table and the single row of the subquery.
If you need the last row from your question example, you can UNION the query with another which returns the fabricated row you want. In this example, I used a custom Dual table which is designed to always contain one and only one row. But you could substitute another table or query which returns a single row.
SELECT
y.Product,
y.Total,
Format((y.Total/sub.SumOfTotal),'#.##%') AS Percentage
FROM
YourTable AS y,
(
SELECT Sum(Total) AS SumOfTotal
FROM YourTable
) AS sub
UNION ALL
SELECT
'Product',
DSum('Total', 'YourTable'),
'100%'
FROM Dual;

Increment Days in SQL query

I am trying to write a query that will output the following table:
|Day_0_Revenue|Day_1_Revenue|Day_2_Revenue|....|Day_90_Revenue|
The data looks like the following table. Each day will have multiple values, and I want to sum the revenue for each day. Day 0 is 11/1.
|Date|Revenue|
11/1 5
11/2 3
11/3 5
11/3 7
11/4 8
11/5 8
11/5 12
11/6 7
I believe this is simple to do if the table I want is vertical. The reason I want to output horizontally is because I will fill the table vertically with other dates. My main question is how to increment the day value without writing a really long SELECT clause? I'm not sure if I can write a loop that will have something like Day+1 until Day=90...
This is not possible in SQLite alone.
The easiest way would be to just use GROUP BY day to create a vertically oriented table, and then reorder the cells when you are displaying them.
If you really must get a horizontally oriented table from the database, you have to create the query dynamically:
SELECT (SELECT SUM(Revenue) FROM MyTable WHERE Date = '11/1') AS Day_0_Revenue,
(SELECT SUM(Revenue) FROM MyTable WHERE Date = '11/2') AS Day_1_Revenue,
(SELECT SUM(Revenue) FROM MyTable WHERE Date = '11/3') AS Day_2_Revenue,
...
You have to do this in whatever programming language you are using to access the database:
sql = "SELECT"
for i in range(90):
sql += " (SELECT SUM(Revenue) FROM MyTable" +
" WHERE Date = '%s') AS Day_%d_Revenue," % (dates[i], i)
sql = sql[:-1] # remove last comma
cursor = db.execute(sql)