Assuming there is a table with 100 columns, how can I select all columns with a sum without having to type out all the columns?
For example something like this:
select *, sum(price) as sales
from table
group by *
order by date
try this
select table.* , t.sales from table
inner join (select id, sum(price) as sales from table group by id ) t
on table.id=t.id
order by date
But in general it is not recommended to use an stare in a select statement,
for example dont use * in table-valued function
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.
I want to do a count grouping by the first column but omitting the others columns in the group by. Let me explain:
I have a table with those columns
So, what I want to get is a new column with the work orders total by Instrument, something like this:
How can I do that? Because if I do a count like this:
SELECT INSTRUMENT, WORKORDER, DATE, COUNT(*)
FROM TABLE1
GROUP BY INSTRUMENT, WORKORDER, DATE;
I get this:
Just use a window function:
select t.*,
count(*) over (partition by instrument) as instrument_count
from table1 t;
Although answer given by Gordon is perfect but there is also another option by using group by and subquery. You can add date column to this query as well
SELECT * FROM
(
SELECT A.INSTRUMENT, B.TOTAL_COUNT_BY_INSTRUMENT
FROM work_order A,
(SELECT COUNT(1) AS TOTAL_COUNT_BY_INSTRUMENT,
INSTRUMENT
FROM WORK_ORDER
GROUP BY INSTRUMENT
) B
WHERE A.INSTRUMENT = B.instrument);
What is the best way to get count of rows and distinct rows in a single query?
To get distinct count we can use subquery like this:
select count(*) from
(
select distinct * from table
)
I have 15+ columns and have many duplicates rows as well and I want to calculate count of rows as well as distinct count of rows in one query.
More if I use this
select count(*) as Rowcount , count(distinct *) as DistinctCount from table
This will not give accurate results as count(distinct *) doesn't work.
Why don't you just put the subquery inside another query?
select count(*),
(select count(*) from (select distinct * from table))
from table;
create table tbl
(
col int
);
insert into tbl values(1),(2),(1),(3);
select count(*) as distinct_count, sum(sum) as all_count
from (
select count(col) sum from tbl group by col
)A
I think I have understood what you are looking for. You need to use some window function. So, you query should be look like =>
Select COUNT(*) OVER() YourRowcount ,
COUNT(*) OVER(Partition BY YourColumnofGroup) YourDistinctCount --Basic of the distinct count
FROM Yourtable
NEW Update
select top 1
COUNT(*) OVER() YourRowcount,
DENSE_RANK() OVER(ORDER BY YourColumn) YourDistinctCount
FROM Yourtable ORDER BY TT DESC
Note: This code is written sql server. Please check the code and let me know.
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 have a question on the sql IN query. For example you have table with columns id, amount name.
With a query:
SELECT SUM(amount) FROM table WHERE id IN (101,101);
What I want with this is to add amount of a certain id. Whatever the id is inside the IN statement. If like this, two 101, amount of 101 + amount of 101.
The problem is it consider it is one instance. How do I do this? its suppose to be:
SELECT SUM(amount) FROM table WHERE id IN (SELECT id FROM table.........);
Which the sub select return "101, 101".
How
SELECT SUM(tbl.amount)
FROM tbl
JOIN (select 101 id UNION ALL
select 101) InList on InList.id = tbl.id
Expand this way.
I do something like this
Select * From table1
inner join dbo.fnSplit(#ArgList, ',')
That would definitely work for me