I have datetime field names "salestime" in "sales" table
I need to get data as:
SalesMonthYear Total
2010-11 10
2010-10 15
2010-09 21
or
Nov-2010 10
Oct-2010 15
Sep-2010 21
Any one know how can I achieve this using query?
Thanks for the help
You need to use DATE_FORMAT and GROUP BY to achieve what you want. This query will do it:
SELECT DATE_FORMAT(salestime, '%Y-%m') as SalesMonthYear, count(*) as `Total` FROM `sales` GROUP BY SalesMonthYear ORDER BY `salestime`
And for the second version:
SELECT DATE_FORMAT(salestime, '%b-%Y') as SalesMonthYear, count(*) as `Total` FROM `sales` GROUP BY SalesMonthYear ORDER BY `salestime`
SELECT DATE_FORMAT(salesTime, '%Y-%m') AS salesMonthYear,
COUNT(*) AS salesCount
FROM sales
GROUP BY DATE_FORMAT(salesTime, '%Y-%m')
Related
I have a sqlite database with this info
id
pcs
dollars
year
10
25
150
2021
10
20
160
2021
10
22
120
2022
11
12
130
2021
11
10
100
2022
I want to get this
id
pcs2021
dollars2021
pcs2022
dollars2022
10
45
310
22
120
11
12
130
10
100
I got this:
SELECT id, SUM(pcs), SUM(dollars) FROM Table GROUP BY id
But I can't find the way to get the SUM of each year separately.
I tried something like:
SELECT id, (SELECT SUM(pcs) FROM Table WHERE id=id AND year=2021) AS pcs2021, (SELECT SUM(dollars) FROM Table WHERE id=id AND year=2021) AS dollars2021, (SELECT SUM(pcs) FROM Table WHERE id=id AND year=2022) AS pcs2022, (SELECT SUM(dollars) FROM Table WHERE id=id AND year=2022) AS dollars2022, FROM Table GROUP BY id
but it doesn't work.....
Thank you for your help.
Using :-
SELECT id, SUM(pcs) AS pcs2021, SUM(dollars) AS dollars2021 FROM `Table` WHERE year = 2021 GROUP BY id;
results in :-
note the above assumes year is an integer so you may have to enclose the literal in single quotes.
Additional (re comment)
My problem is when I want to put different columns with SUM of values for each year
An issue you are having is that id=id will always be true and thus you will sum all rows, irrespective of the id, for the year and get the result (when the syntax is correct):-
That is id is the id of the sub query when you want to compare against the id currently being processed by the main query.
To do this you can give the main query an alias using the AS clause and then refer to the id of the alias so (where the main query is given the alias of a):-
SELECT
id,
(SELECT sum(pcs) FROM `table` WHERE id=a.id AND year='2021') AS pcs2021,
(SELECT sum(dollars) FROM `table` WHERE id=a.id AND year='2021') AS dollars2021,
(SELECT sum(pcs) FROM `table` WHERE id=a.id AND year='2022') AS pcs2022,
(SELECT sum(dollars) FROM `table` WHERE id=a.id AND year='2022') AS dollars2022
FROM `table` AS a
GROUP BY id
;
The result being :-
Note the use of Table as a table name will result in a syntax error if it is not enclosed as Table is an SQLite keyword.
can some one help me...
I have to create,for each "Costumer", a iterator for a non-sequential ID to update the "version" column.
I need a cursor or something else?
Can i get some help?
Example:
ID COSTUMER VERSION
12 ANNA 1
24 ANNA 4
25 ANNA 5
60 ANNA 11
I want to correct the version to be sequential
You could use code something like this:
begin
for r in ( select id, row_number() over (partition by name order by version) as rn
from costumer
)
loop
update costumer
set version = r.rn
where id = r.id;
end loop;
end;
/
The partition by is there because I have assumed you want to have the sequence start from 1 for 'ANNA', then start from 1 again for customer 'JANE' etc. If not you can remove that part.
Here's the way to do it via a single MERGE statement:
MERGE INTO costumer tgt
USING (SELECT ID,
costumer,
VERSION,
ROWID row_id,
row_number() OVER (PARTITION BY costumer ORDER BY VERSION) new_version
FROM costumer) src
ON (tgt.rowid = src.rowid)
WHEN MATCHED THEN
UPDATE SET tgt.version = src.new_version;
I have 3 Columns: 'GAME' 'PLAYER_ID' 'MINUTES_PLAYED'
looks like this:
GAME ID MINUTES_PLAYED
12550788 229569 23
12500788 393438 4
12500788 458730 25
12500782 229569 10
I would like find total minutes played per game i.e.
ID TOTAL_MINUTES
229569 33
.
.
.
Thank you in advance!
select id, sum(minutes_played) from tableName group by id
SELECT ID, SUM(MINUTES_PLAYED)
FROM MYTABLE
GROUP BY ID
Simply add them up:
SELECT ID, SUM(MINUTES_PLAYED) AS TOTAL_MINUTES
FROM table GROUP BY ID
I have got a table which i have mentioned below, i need to get a total results avoiding duplicates , can any one provide any assistance or suggestions on how to get the results mentioned below, thankyu
ID name Total Used
24 John 5 2
24 John 10 6
27 Peter 20 0
27 Peter 20 5
Result should be something like this
ID name Total Used
24 John 15 8
27 Peter 40 5
Looks like you just need to use SUM() on the two columns. Also use a GROUP BY on the id and name
SELECT id, name, sum(total) All_total, sum(used) All_used
FROM yourtable
GROUP BY id, name
see SQL Fiddle with Demo
The GROUP BY field must include any other columns you are selecting which are not in an aggregate function, so for the example you would include both id and name in the GROUP BY.
Edit #1 your query would be:
SELECT [ID] , name, sum([Total]), sum([Used]), [GUID]
FROM [table].[dbo].[vw_data]
GROUP BY [ID], [name], [GUID]
select sum(Total), sum(Used), ID, name
from table
group by ID, name;
select ID, name, sum( Total,), sum( Used)
from table
group by Id,name;
Try this:
select id,name, sum(Total),sum(used)
from tab
group by id,name
SELECT id, name, sum(total) total, sum(used) used from table GROUP BY id, name
MySql syntax for this problem:
CREATE TABLE DUPLICATE (ID,NAME, TOTAL, USED);
INSERT INTO DUPLICATE VALUES(24,'John',5,2);
INSERT INTO DUPLICATE VALUES(24,'John',10,6);
INSERT INTO DUPLICATE VALUES(27,'Peter',20,0);
INSERT INTO DUPLICATE VALUES(27,'Peter',20,5);
SELECT ID, NAME, SUM(TOTAL), SUM(USED) FROM DUPLICATE GROUP BY ID, NAME;
I need some help to build SQL Query. I have table having data like:
ID Date Name
1 1/1/2009 a
2 1/2/2009 b
3 1/3/2009 c
I need to get result something like...
1 1/1/2009 a
2 1/2/2009 b
3 1/3/2009 c
4 1/4/2009 Null
5 1/5/2009 Null
6 1/6/2009 Null
7 1/7/2009 Null
8 1/8/2009 Null
............................
............................
............................
30 1/30/2009 Null
31 1/31/2009 Null
I want query something like..
Select * from tbl **where month(Date)=1 AND year(Date)=2010**
Above is not completed query.
I need to get all the record of particular month, even if some date missing..
I guess there must be equi Join in the query, I am trying to build this query using Equi join
Thanks
BIG EDIT
Now understand the OPs question.
Use a common table expression and a left join to get this effect.
DECLARE #FirstDay DATETIME;
-- Set start time
SELECT #FirstDay = '2009-01-01';
WITH Days AS
(
SELECT #FirstDay as CalendarDay
UNION ALL
SELECT DATEADD(d, 1, CalendarDay) as CalendarDay
FROM Days
WHERE DATEADD(d, 1, CalendarDay) < DATEADD(m, 1, #FirstDay)
)
SELECT DATEPART(d,d.CalendarDay), **t.date should be (d.CalendarDay)**, t.Name FROM Days d
LEFT JOIN tbl t
ON
d.CalendarDay = t.Date
ORDER BY
d.CalendarDay;
Left this original answer at bottom
You need DATEPART, sir.
SELECT * FROM tbl WHERE DATEPART(m,Date) = 1
If you want to choose month and year, then you can use DATEPART twice or go for a range.
SELECT * FROM tbl WHERE DATEPART(m,Date) = 1 AND DATEPART(yyyy,Date) = 2009
Range :-
SELECT * FROM tbl WHERE Date >= '2009-01-01' AND Date < '2009-02-01'
See this link for more info on DATEPART.
http://msdn.microsoft.com/en-us/library/ms174420.aspx
You can use less or equal to.
Like so:
select * from tbl where date > '2009-01-01' and date < '2009-02-01'
However, it is unclear if you want month 1 from all years?
You can check more examples and functions on "Date and Time Functions" from MSDN
Create a temporary table containing all days of that certain month,
Do left outer join between that table and your data table on tempTable.month = #month.
now you have a big table with all days of the desired month and all the records matching the proper dates + empty records for those dates who have no data.
i hope that's what you want.