Calculating totals with multiple occurrences of the same ID and values in same column - sql-server

Recently I am coming across some strange tables and data.
I have a table that looks like the below image:
I would like to calculate the total hours worked and place in a new or alias column:
I would like to calculate the red header table to look like the blue table.
I am familiar with TSQL but never seen something like this and don't know where to start.
Thanks guys!

It seems like you just want to use SUM():
select userid,
sum(hrsworked) hrsworked
from yourtable
group by userid;
The SUM() function is an aggregate function that when used along with GROUP BY will get you the total hours worked for each userId

You can start with SUM and GROUP BY
select userid, sum(hrsworked) hrsworked
from TableName
group by userid

Related

Find the Min and Max date from two tables from a sql select statement

Cant seem to wrap my head round this problem.
I have two tables one which has the following sample values:
Second table had the following values:
What i am trying to achieve is like the following:
So you can see the first table has the modules, what year and what term.
Based on these there is a start week and and end week.
The lookup table for the start and the finish unfortunatley is in a week basis and i need the begin week to match the second tables weekNo based on the season i guess and taking the start date being Sdate from that table to match what i am looking for and then the same applies to the end date.
Match the season and the endweek with the second tables WeekNo and Edate and only bring that date in.
Hope i made a bit of sense but i am hoping the third image shows what i am look for.
I've tried CTE, Group by, Partition by, order by, min, max and got nowhere :(
Dont really want to hard code anything, so was hoping you wonderful peps can help me out !!
Many thanks in advance :)
I suspect you are trying to achieve this by using one a single join between the tables - whereas what you actually need is two separate joins:
SELECT table1.module as mod_code,
table1.season as psl_code,
table2.Sdate as ypd_sdate,
table3.Edate as ypd_edate
FROM t1 as table1
JOIN t2 as table2 ON table2.yr = table1.year AND table2.season = table1.season AND table2.weekNo = table1.BeginWeek
JOIN t2 as table3 ON table3.yr = table1.year AND table3.season = table1.season AND table3.weekNo = table1.EndWeek

SQL Query Help on separating return result

Example: On the phone table, someone has two phone number SQL would give me the a second row of the same person with different phone number instead of second column. What query do I use to check if person_id appears more than once insert second row of data in a separate column?
I hope this make sense. Thanks in advance!
Try something like this:
SELECT person_id, COUNT(person_id) AS 'PersonIDCount' FROM phone_table
GROUP BY person_id
HAVING COUNT(person_id) > 1
The query will return all records where the same person_id key was inserted more than once.

How to get top 10 record by its score field

I'm working on a project which there are shop's and customer's.customer's can score shop's.
my Score Data Table is like this:
Table Name is:ShopScores
I want to retrieve top 10 shops based on their Score field result.Is it possible to do that and if the answer is Yes, How can I do it?
By the way, I want to do that using LINQ Query.
With linq:
var result = Context.ShopScores.OrderByDescending(c=>c.Score).Take(10);
SELECT * FROM `ShopScores` ORDER BY `score` DESC LIMIT 10
this gives the records ordered by score ,but this is for mysql
This works perfectly fine

Grouping by single column but returning all the columns without including other columns in aggregate function

I am working on an SQL query which should group by a column bidBroker and return all the columns in the table.
I tried it using the following query
select Product,
Term,
BidBroker,
BidVolume,
BidCP,
Bid,
Offer,
OfferCP,
OfferVolume,
OfferBroker,
ProductID,
TermID
from canadiancrudes
group by BidBroker
The above query threw me an error as follows
Column 'canadiancrudes.Product' is invalid in the select list because it is not contained in either an aggregate function or the
GROUP BY clause.
Is there any other way which returns all the data grouping by bidBroker without changing the order of data coming from CanadadianCrudes?
First if you are going to agregate, you should learn about agregate functions.
Then grouping becomes much more obvious.
I think you should explain what you are trying to accomplish here, because I suspect that you are trying to SORT bu Bidbroker, rather than grouping.
If you mean you want to sort by BidBroker, you can use:
SELECT Product,Term,BidBroker,BidVolume,BidCP,Bid,Offer,OfferCP,OfferVolume,OfferBroker,ProductID,TermID
FROM canadiancrudes
ORDER BY BidBroker
If you want to GROUP BY, and give example-data you can use:
SELECT c1.Product,c1.Term,c1.BidBroker,c1.BidVolume,c1.BidCP,c1.Bid,c1.Offer,c1.OfferCP,c1.OfferVolume,c1.OfferBroker,c1.ProductID,c1.TermID
FROM canadiancrudes c1
WHERE c1.YOURPRIMARYKEY IN (
select MIN(c2.YOURPRIMARYKEY) from canadiancrudes c2 group by c2.BidBroker
)
Replace YOURPRIMARYKEY with your column with your row-unique id.
As others have said, don't use "group by" if you don't want to aggregate something. If you do want to aggregate by one column but include others as well, consider researching "partition."

MSSQL Finding Total of a column and carry other data with it

I have the following table to work with, which I can not change. I have to work with what I have.
Id (int auto int)
CustomerName (varchar)
CustomerNumber (int)
Date (date)
WeeklyAmount (int)
What I would like to do is grab all the data per customer and add all the weekly amounts for a specific year. Eventually I will want to compare two years together, but right now I am working on the data to sum up the weekly totals per CustomerNumber.
I am using:
Select
CustomerNumber, SUM (WeeklyAmount) as Total from
Customers.RECORDS GROUP BY CustomerNumber;
This works fine, however, I want to return the CustomerName as well. Eventually I will have to place in the SQL for getting specific years and compare them. However, I have to tackle this part first.
Assuming there is a 1-to-1 relationship between CustomerName and CustomerNumber:
Select
CustomerNumber, CustomerName, SUM (WeeklyAmount) as Total from
Customers.RECORDS GROUP BY CustomerNumber, CustomerName;
If the relationship is not 1-to-1, then I suppose you'd need to define what exactly represents a customer in the phrase grab all the data per customer.

Resources