I am not coming right with this.
I have this table "bids" in SQL 2012:
| custDecDate | forBid | valueXX | valueYY |
I would like to sum the values relevant "valueXX" and "ValueYY" separately and grouped per year "custDecDate" where the value of column "forBid" = For Contract
"custDecDate" is a datetime and contains date dd/MM/yyyy, valueXX and valueYY are int and "forBid" is a nvarchar.
I am very lost. Any help is welcomed, I need a starting point to learn this.
SELECT YEAR(custDecDate), SUM(valueXX), SUM(valueYY)
FROM bids
WHERE forBid = "For Contract"
GROUP BY YEAR(custDecDate)
the query should be really simple....
Oracle:
Select trunc(custDecDate,'YYYY'), sum(valueXX), sum(valueYY)
from bids
where forBid = '...'
group by trunc(custDecDate,'YYYY')
MSSQL:
Select year(custDecDate), sum(valueXX), sum(valueYY)
from bids
where forBid = '...'
group by year(custDecDate)
Did I answer what you meant?
Related
What do I have to change to get different results from different names.The table should give me the debts of each of them, this is calculated by the amount and the price of the drink. Now it should show all the names with the corresponding invoice that happens after the select
%sql select name, sum(getraenk.preis*schulden.menge) schulden from schulden \
join person on (fk_person = person.id)\
join getraenk on (fk_getraenk = getraenk.id)\
where name like ("dani")
Edit: it should spend all the names with their debts, that is:
dani = 8.5
michael = 12.5
...
Just in case your problem is very simple, you should be able to see all names and values with an SQL that looks like this:
select name, getraenk.preis*schulden.menge schulden
from schulden
join person on (fk_person = person.id)
join getraenk on (fk_getraenk = getraenk.id)
Note that I removed the where clause... this was the part that limited it to one name.
You also don't need the sum clause here unless you are doing a group by
Have you considered simply using GROUP BY name at the end of this query?
https://www.w3schools.com/sql/sql_groupby.asp
This will give you the sum of total debt for all names in your table which sounds like the result you are looking for.
You're missing
GROUP BY name
in the query.
I am working on a MS-Access table, and would like to have a query to result all the information on the last entry from a certain id.
My table (DEPOSIT_MOVEMENTS) is the following:
MOV_CODE | WORK | DEPOSIT_CODE | TYPE | DATE | DESTINATION
I am looking to obtain for each DEPOSIT_CODE the latest register (on date), and obtain the *MOV_CODE so that I can get the DESTINATION of the item.
DEPOSIT_CODE may have many MOV_CODE on different dates.
I have tried with different options posted on stackoveflow, but I coudl not get any of these to work properly.
Right now I am trying with the GROUP BY, but cannot get it working.
SELECT t1.[DEPOSIT_CODE], MAX(t1.[DATE]), t1.[MOV_CODE]
FROM [DEPOSIT_MOVEMENTS] AS t1
GROUP BY t1.[DEPOSIT_CODE];
Any help or guidance is welcome.
Kind regards,
Here is one method:
SELECT dm.*
FROM [DEPOSIT_MOVEMENTS] AS dm
WHERE dm.DATE = (SELECT MAX(dm2.DATE)
FROM [DEPOSIT_MOVEMENTS] AS dm2
WHERE dm2.DEPOSIT_CODE = dm.DEPOSIT_CODE
);
Tables
tblTracker: [ID, Date]
tblTrackerHours: [tblTracker.ID, Hour]
tblTrackerWTGStatus: [tblTracker.ID, TurbineID, Hour, TurbineStatus]
Desired Output
Columns: Date | Hour (from tblTrackerHours) | TurbineIDs .....>
Data: Ordered dates | 1-24 for each date | TurbineStatus
I'm struggling understand where to begin with creating an SQL statement that will do this. Not every TurbineID has an entry for each hour or each day.
Any hints welcomed :-)
I think what you need is a view, which you can create as follows:
create view desiredView as
select
t.date as DATEorWhateverYouWantToNameThisColumn,
h.hour as HOURorWhateverYouWantToNameThisColumn,
s.turbineId as TirbuneIDorWhateverYouWantToNameThisColumn,
s.turbineStatus as STATUSorWhateverYouWantToNameThisColumn
from
tblTracker t,
tblTrackerHours h,
tblTrackerWTGStatus s
where
h.id = t.id AND
s.id = t.id;
Please note that, h.id and s.id in where clause actually the columns which are foreign keys in relevant tables.You can set each column's name using a word after AS. You can find the syntax of the command here.
After you have this view, you can select from it as you wish
select * from desiredView
For this code I need the sum of the serv1 - serv8 where the student is receiving "Speech Language Therapy". I can not for the life of me figure out how to get those. If I say where serv1 equals 'SLT' then it will show up as null for serv 2. If I do it for serv2 it will show up as another service like DD for serv1.
My sum isn't working I would like to sum all of the services up by school but I'll take what anyone is willing to help me with. Any help is appreciated
SELECT SPE_Student_Profile_VW.school_code,
SPE_Student_Profile_VW.organization_name AS [[[School Name]]]]]]],
SPE_Student_Profile_VW.sis_number,
SPE_Student_Profile_VW.primary_disability_code,
SPE_Student_Profile_VW.secondary_disability_code,
SPE_Student_Profile_VW.tertiary_disability_code,
SPE_Student_Profile_VW.serv1,
SUM (SPE_Student_Profile_VW.mins1),
SPE_Student_Profile_VW.serv2,
SPE_Student_Profile_VW.mins2,
SPE_Student_Profile_VW.serv3,
SPE_Student_Profile_VW.mins3,
SPE_Student_Profile_VW.serv4,
SPE_Student_Profile_VW.mins4,
SPE_Student_Profile_VW.serv5,
SPE_Student_Profile_VW.mins5,
SPE_Student_Profile_VW.serv6,
SPE_Student_Profile_VW.mins6,
SPE_Student_Profile_VW.serv7,
SPE_Student_Profile_VW.mins7,
SPE_Student_Profile_VW.serv8,
SPE_Student_Profile_VW.mins8
FROM DBO.SPE_Student_Profile_VW SPE_Student_Profile_VW
WHERE ( SPE_Student_Profile_VW.serv1 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47')
GROUP BY SPE_Student_Profile_VW.school_code,
SPE_Student_Profile_VW.organization_name,
SPE_Student_Profile_VW.sis_number,
SPE_Student_Profile_VW.primary_disability_code,
SPE_Student_Profile_VW.secondary_disability_code,
SPE_Student_Profile_VW.tertiary_disability_code,
SPE_Student_Profile_VW.serv1,
SPE_Student_Profile_VW.mins1,
SPE_Student_Profile_VW.serv2,
SPE_Student_Profile_VW.mins2,
SPE_Student_Profile_VW.serv3,
SPE_Student_Profile_VW.mins3,
SPE_Student_Profile_VW.serv4,
SPE_Student_Profile_VW.mins4,
SPE_Student_Profile_VW.serv5,
SPE_Student_Profile_VW.mins5,
SPE_Student_Profile_VW.serv6,
SPE_Student_Profile_VW.mins6,
SPE_Student_Profile_VW.serv7,
SPE_Student_Profile_VW.mins7,
SPE_Student_Profile_VW.serv8,
SPE_Student_Profile_VW.mins8
the column you are trying to sum should not be in the group by clause, all the others should.
What I mean is the SELECT clause has this:
SUM (SPE_Student_Profile_VW.mins1),
and the GROUP BY has this part which should not be there:
SPE_Student_Profile_VW.mins1,
because it is the column he is trying to sum and you can't group by a column that you are aggregating with a function and that is why SUM isn't working
not sure of what is required but from what I get, you can probably get what you need if you add another
OR
in your
WHERE
Something like
WHERE (SPE_Student_Profile_VW.serv1 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47') OR
(SPE_Student_Profile_VW.serv2 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47') OR
SPE_Student_Profile_VW.serv3 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47'
and so on.
Thanks.
In Group by clause use all columns which you are used in select statement.Except the column you are calculating sum in select statement.
Thanks.
I have a problem that I hope you can help me.
I have the next query on Access SQL.
SELECT
ID_PLAN_ACCION, ID_SEGUIMIENTO,
Max(FECHA_SEGUIMIENTO) AS MAX_FECHA
FROM
SEGUIMIENTOS
WHERE
(((ID_PLAN_ACCION) = [CODPA]))
GROUP BY
ID_PLAN_ACCION, ID_SEGUIMIENTO;
And it returns this information:
ID_PLAN_ACCION ID_SEGUIMIENTO MAX_FECHA
-----------------------------------------------
A1-01 1 16/01/2014
A1-01 2 30/01/2014
But I really need that it throws off this:
ID_PLAN_ACCION ID_SEGUIMIENTO MAX_FECHA
----------------------------------------------
A1-01 2 30/01/2014
As you can see I only need the record that has the most recently date, not all the records
The GROUP BY doesn't work.
Please can you tell me what can I do? I am new on all this.
Thank you so much!!
PD: Sorry for my english, I'm learning
This will produce the results you want:
SELECT ID_PLAN_ACCION, max(ID_SEGUIMIENTO) as ID_SEGUIMIENTO, Max(FECHA_SEGUIMIENTO) AS MAX_FECHA
FROM SEGUIMIENTOS
WHERE ID_PLAN_ACCION = [CODPA]
GROUP BY ID_PLAN_ACCION;
I removed id_sequiimiento from the group by and added an aggregation function to get the max value. If the ids increase along with the date, this will work.
Another way to approach this query, though, is to use top and order by:
SELECT top 1 ID_PLAN_ACCION, ID_SEGUIMIENTO, FECHA_SEGUIMIENTO
FROM SEGUIMIENTOS
WHERE ID_PLAN_ACCION = [CODPA]
ORDER BY FECHA_SEGUIMIENTO desc;
This works because you are only returning one row.
EDIT:
If you have more codes, that you are looking at, you need a more complicated query. Here is an approach using where/not exists:
SELECT ID_PLAN_ACCION, ID_SEGUIMIENTO, FECHA_SEGUIMIENTO
FROM SEGUIMIENTOS s
WHERE not exists (select 1
from SEGUIMIENTOS s2
where s.ID_PLAN_ACCION = s2.ID_PLAN_ACCION and
s2.FECHA_SEGUIMIENTO > s.FECHA_SEGUIMIENTO
)
ORDER BY FECHA_SEGUIMIENTO desc;
You can read this as: "Get me all rows from SEGUIMIENTOS where there is no other row with the same ID_PLAN_ACCION that has a larger date". Is is another way of saying that the original row has the maximum date.