SQL Server - sum a column using group by - sql-server-2008

I have columns in the below format
and I am looking for an output by adding the last column based on the first column
Please could you someone suggest the best approach?
Best regards

used Over(Partition by.....)
in the following way and it worked
,Sum(NewBill) OVER (PARTITION BY wouid) AS Newfield
Thanks for all your support guys.

Related

how can i put multiple records into 1 record in mysql

I was trying to move multiple records from one table to one record in another. with MYSQL,I tried Group_concat(), Concat() Group by, and Stored Procedure to breakdown the data and regroup. Unfortunately, my attempts have failed. I know you can help me because you are the experts. I thank you in advance. Please see image I attached.
Probably you require aggregation here, something like:
select Matrial, max(Qty_In_Jan) Qty_In_Jan, max(Qty_In_feb) Qty_In_feb....
from t
group by Matrial;

Very basic question about dividing two MySQL table columns

So, I'm just beginning learning SQL today. I have a table in MySQL and need to find the gdp per capita and make a new column out of it (or potentially just assign it to a column I have already made if making a new one isn't possible), I have a column with the countries' gdp and the countries' population, both of which have been given the type BIGINT. But how might I divide those two numbers together and have them in descending order and I want to print them out?
The code I have so far is:
SELECT countries_name, countries_population, countries_gdp
FROM countries.countries
WHERE (countries_population / countries_gdp) countries_per_capita_gdp
ORDER BY DESC
Would anyone be able to give me a hint as to what I might be doing wrong please? Our lecturer is giving us very little guidance on this so I think most of us are confused, unfortunately.
I think what you're looking for is this:
SELECT countries_name, countries_population, countries_gdp, (countries_population / countries_gdp) AS countries_per_capita_gdp
FROM countries
ORDER BY countries_per_capita_gdp DESC
I noticed your FROM had countries.countries, not sure if that was intentional, might just have to be countries.

mysql min and max values in subquery

thanks in advance for the help. I'm a noob with sql and this is probably a pretty basic question, but I have been working on it for hours and can't seem to figure out what I'm doing wrong.
Here is the question posed:
Using aggregate functions, display the employee name (first and last name concatenated) in a field named Employee Name and the hire date of the employee who has been employed the longest and the employee who was hired last.
I have tried to code this different ways to no avail. I always get one result instead of the two I know I should get. (I know I should get two results because I'm working with a very small table and can calculate the answer by looking at it - easy to make sure I'm getting the right results that way.)
Here is one way I coded it:
subquery
Here, I used a sub query for the min and max. I used 'and' in between because I want both results. I only get one. If I use 'or', I get too many results
I have also tried it this way:
min/max in select statement
Here, I still get only one employee name back, but now I get both dates. The dates are the correct dates, but I need the output to look like the first query output.
Any help anyone can provide would be greatly appreciated!
How about
select concat(first_name,' ',last_name), hire_date
from l_employees
where hire_date = (select max(hire_date) from l_employees)
or hire_date = (select min(hire_date) from l_employees)

Why is my count function only working on certain tables?

I know this is an amateur question, but I've searched every resource I can think of, and now I'm at my wit's end. The following query works perfectly on most of my tables, but for some reason it is not working on the tables that I desperately need it for:
SELECT COUNT(*)
FROM radio_r1_own_it
WHERE daypart LIKE 'AM';
The query works exactly how I want it to for nearly all of my tables, but for some reason it is returning a value of "0" on the tables I need it for (even though there are over 20 instances of "AM" in the "daypart" column on this table). I have checked and double-checked everything I can think of.
I'm relatively new to SQL but I've never encountered a problem like this before. Anyone have any ideas or resources that might help? Thanks so much for your time!
EDIT: I don't have enough reputation points to post a screen shot on here... but here's a link where you can see one: http://imgur.com/ZhyEqJY
There are 29 columns in this table. If there's any other info that might help just let me know, thanks!
You need to add the like part as shown below:
where column_name like '%AM%'
when you write like 'AM' it is searching for the full match
Try this.
SELECT COUNT(*) FROM radio_r1_own_it WHERE daypart LIKE '%AM%';
If you want to order it using the count,
SELECT COUNT(*) FROM radio_r1_own_it WHERE daypart LIKE '%AM%' ORDER BY COUNT(*) DESC;
DESC - Descending order
ASC - Ascending order
LIKE statements are really slow in sql. If you have a daypart column then I assume you have a date column. I recommend something like this instead:
SELECT COUNT(*)
FROM radio_r1_own_it
WHERE HOUR(dateColumn) < 13
Unless the string you're matching is exactly AM you need to use wildcard characters to match the string:
SELECT COUNT(*) FROM radio_r1_own_it WHERE daypart LIKE '%AM%';
The % matches any number of characters before and after AM.
See the documentation for more information.

I want to use field from select statement in AVG function in Mysql

select count(distinct installation_id), SUM(hit_block), AVG(???)
This is the select statement. I want to divide the value returned by first field (count(distinct installation_id)) by the second field. Please suggest me how to do it.
select count(distinct installation_id)/SUM(hit_block),AVG(??)
Basically you can do numerical operations in between what could be separate selectable clauses.
I'm not sure what you want to find the average of?
i dont think i understand very clearly what is it that you want to do, perhaps if you add an example data set, and the a description of the problem you are trying to solve by running that query, if what you want to do is just divide you can apply the arithmetic operands
note that this is just an example and wont probably work as i dont have a way of testing this on your enviroment
SELECT (COUNT(DISTINCT installation_id) / SUM(hit_block))
,AVG(???)
FROM ????
GROUP BY ????
note that you should probably replace the question marks with real values ???, is this what you were looking for? if not please explain i can probably still help