How to sum the value for each - sql-server-2008

I have table following :
ClientNUM PIECES DID
NEWAGENC 10 5
NEWAGENC 25 5
WAY 30 4
CHCAH 20 2
AVC 21 2
i want the Result that sum the value for each client as below
CleintNUM Pieces DID
NEWAGENC 35 5
WAY 30 4
CHCAH 20 2
AVC 21 2
My query
SELECT
CLIENTNUM,
DID,
PIECES,
GETDATE() AS CURRENTDATE,
SUM(PIECES)
FROM Mytable
GROUP BY CLIENTNUM, DISPID, PIECES
So how can i do the sum for each CLIENTNUM in my query Means DISTINCT For each client Pieces like NEWAGENC has value 10 and in second row 25 so the pieces will be 10+ 25 = 35

Don't group by PIECES if you want to aggregate it
SELECT CLIENTNUM,
DID,
PIECES,
GETDATE() AS CURRENTDATE,
SUM(PIECES)
FROM Mytable
GROUP BY CLIENTNUM, DISPID

Related

Is there a way to have a conditional statement for comparing of tables using MySQL only?

I am having a dilemma from comparing my tables.
My problem is, I want to get each sum, which depends on the pricing. Here is the table.
Main table
main_id main_price main_date_created
25 8.5 2019-08-16
26 11.5 2019-08-01
Total table
id main_id total_price date_generated
1 25 10 2019-08-16
2 25 10 2019-08-17
3 25 10 2019-08-18
4 25 10 2019-08-19
5 25 10 2019-08-20
6 25 10 2019-08-21
7 26 20 2019-08-01
8 26 5 2019-08-02
9 26 5 2019-08-03
10 26 10 2019-08-04
Price History table
id main_id changed_main_price price_date_changed
1 25 15 2019-08-18
2 26 20 2019-08-03
I don't know if there is a way to do this just by using MySQL, what I am trying to achieve is, the Total table will be sum by MONTH and YEAR and will be multiplied by their designated price that depends on the date whether if the price has changed or not . the SUM will from each month will be generated by multiplying with the main price in the Main table but if the price had changed from its original price which it is on the Price history table
The output should be like this if the conditional is possible:
id main_id total_price price_generated (which is the prices) date
1 25 170 (10+10*8.5) 8.5
2 25 610 (10+10+10+10*15) 15
3 26 287.5 (20+5*11.5) 11.5
4 26 300 (5+10*20) 20
Here is my existing query,
SELECT m.main_id
, m.main_price
, SUM(t.total_price) total_generated
, t.date_generated
FROM main m
INNER JOIN total t
ON m.main_id = t.main_id
GROUP
BY MONTH(t.date_generated);
I know that my query is not enough, and I still don't know if my idea is really possible :(.
I racked my brain. hahaha Is this you're after?
SELECT pricelist.id, pricelist.price, SUM(t.total_price), SUM(t.total_price) * pricelist.price,
year( pricelist.latestdate), month(pricelist.latestdate),
year( t.total_date_generated), month(t.total_date_generated)
from totaltable as t
LEFT JOIN (
(
select main_id as id, main_price as price, main_date_created as latestdate
from maintable
)
UNION ALL
(
select total_main_id, changed_main_price , price_date_changed
from pricehistorytable
)
) as pricelist on pricelist.id = t.total_main_id
GROUP BY pricelist.id , t.total_price, pricelist.price,
year( pricelist.latestdate), month(pricelist.latestdate),
year( t.total_date_generated), month(t.total_date_generated) ;

MySQL - Average ignoring Null and based on weekday

I´m trying to do some analysis in the following data
WeekDay Date Count
5 06/09/2018 20
6 07/09/2018 Null
7 08/09/2018 19
1 09/09/2018 16
2 10/09/2018 17
3 11/09/2018 24
4 12/09/2018 25
5 13/09/2018 24
6 14/09/2018 23
7 15/09/2018 23
1 16/09/2018 9
2 17/09/2018 23
3 18/09/2018 33
4 19/09/2018 22
5 20/09/2018 31
6 21/09/2018 17
7 22/09/2018 10
1 23/09/2018 12
2 24/09/2018 26
3 25/09/2018 29
4 26/09/2018 27
5 27/09/2018 24
6 28/09/2018 29
7 29/09/2018 27
1 30/09/2018 19
2 01/10/2018 26
3 02/10/2018 39
4 03/10/2018 32
5 04/10/2018 37
6 05/10/2018 Null
7 06/10/2018 26
1 07/10/2018 11
2 08/10/2018 32
3 09/10/2018 41
4 10/10/2018 37
5 11/10/2018 25
6 12/10/2018 20
The problem that I want to solve is: I want to create a table with the average of the 3 last same weekdays related to the day. But, when there is a NULL in the weekday, I want to ignore and do the average only with the remain numbers, not count NULL as an 0. I will give you an example here:
The date in this table is day/month/year :)
Ex: On day 12/10/2018, I need the average from
the days 05/10/2018; 28/09/2018; 21/09/2018. These are the last 3 same weekday(six) as 12/10/2018.
. Their values are Null; 29; 17. Then the result of this average must be 23, because I need to ignore the NULL, and not be 15,333.
How can I do this?
The count() function ignores nulls (i.e. does NOT increment if it encounters null) so I suggest you simply count the values then may contain the nulls you wish to ignore.
dow datecol value
6 21/09/2018 17
6 28/09/2018 29
6 05/10/2018 Null
e.g. sum(value) above = 46, and the count(value) = 2 so the average is 23.0 (and avg(value) will also return 23.0 as it also ignores nulls)
select
weekday
, `date`
, `count`
, (select (sum(`count`) * 1.0) / (count(`count`) * 1.0)
from atable as t2
where t2.weekday = t1.weekday
and t2.`date` < t1.`date
order by t2.`date` DESC
limit 3
) as average
from atable as t1
You could just use avg(count) in the query above, and get the same result.
ps. I do hope you do NOT use count as a column name! I also would suggest you do NOT use date as a column name either. i.e. Avoid using SQL terms as names.
SELECT WeekDay, AVG(Count)
FROM myTable
WHERE Count IS NOT NULL
GROUP BY WeekDay
Use IsNULL(Count,0) in your Select
SELECT WeekDay, AVG(IsNULL(Count,0))
FROM myTable
GROUP BY WeekDay
First off, you need to get the number of instances of that weekday in the data since you just need the last 3 same week days
create table table2
as
select
row_number() over(partition by weekday order by date desc) as rn
,weekday
,date
,count
from table
From here, you can get what you want. With you explanation, you don't need to filter out the NULL values for count. Just doing the avg() aggregation will simply ignore it.
select
weekday
,avg(count)
from table2
where rn in (1,2,3)
group by weekday

How to group data per week in MySQL?

id modid userid timemodified FROM_UNIXTIME(timemodified,'%d-%m-%Y')
410 32 46 1438971403 03-08-2015
411 32 46 1438971403 03-08-2015
412 66 977 1438971403 07-08-2015
412 66 977 1438971403 07-08-2015
413 67 34 1438971423 07-08-2015
414 68 16 1438971424 07-08-2015
415 132 23 1438972154 07-08-2015
416 134 2 1438972465 08-08-2015
417 115 2 1438996430 08-08-2015
418 130 977 1438996869 08-08-2015
I got this query from framing the last 4weeks ago by calculating from today's date. Now, I want to show the users for 4 weeks individually like week1, week2, week3 & week4, either it could be column wise or row wise, which would be the best.
In detailed, from the above query, I need to separate data from week1 to week4,like
Week4 : No user
Week3 : 2 users (2,977)
Week2 : 4 users (16, 23, 34, 977)
Week1 : 1 user (46)
SET #unix_four_weeks_ago = UNIX_TIMESTAMP(curdate()) - 2419200;
SELECT *,FROM_UNIXTIME(timemodified,'%d-%m-%Y') FROM mod_users WHERE timemodified >= #unix_four_weeks_ago
My guess is that you want to split the user count per week according to the timemodified column. I would use the WEEK() function to do that.
The following SQL would add a weeknumber column to identify the week number:
SELECT WEEK(timemodified) weeknumber, dates.*
FROM dates
Then, if you want to get the distinct user count, you can simply use the following SQL:
SELECT WEEK(timemodified) weeknumber, COUNT(DISTINCT(userid)) users_count
FROM dates
GROUP BY weeknumber
You can also add a WHERE clause to get only certain weeks as you wish. So, to get the last 4 weeks from the 23-08-2015, I would do:
SELECT WEEK(timemodified) weeknumber, COUNT(DISTINCT(userid)) users_count
FROM dates
WHERE WEEK(timemodified) <= WEEK('2015-08-23')
AND WEEK(timemodified) > (WEEK('2015-08-23') - 4)
GROUP BY weeknumber
Let's hope I assumed correctly. :-)

retrieve all rows from the table grouped by condition

I have the following mysql table:
products
with a fileds:
id, product_group_id, internal_product_id, version, platform_id, name
1 12 12 1 30 Megacalculator
2 12 12 2 30 Megacalculator
3 16 17 1 30 Calculator
4 16 17 2 30 Calculator
5 16 18 0.1 40 Calculator Linux
6 20 19 2.1 30 Converter Windows
7 20 20 2.1 40 Converter Linux
8 30 24 0.1 30 Editor
I need to retrieve all rows from this table grouped by 'product_group_id' but with a different 'internal_product_id' inside of the group. Also, the rows count in each group must be equal to some special number(the value must be supplied into the query as an external parameter)
For example:
external parameter = 2, result:
product_group_id
16
20
external parameter = 1, result:
product_group_id
12
30
Please help me with this sql query.
Try that:
for parameter = 2
select `product_group_id` from products
group by `product_group_id`
having count(distinct `internal_product_id`) = 2
for parameter = 1
select `product_group_id` from products
group by `product_group_id`
having count(distinct `internal_product_id`) = 1
DEMO HERE
You can do this:
select product_group_id from products
group by product_group_id;
having count(internal_product_id) = 2;
Or you can get the information in a table:
select product_group_id, count(internal_product_id) from products
group by product_group_id;

MySQL Select Last n Rows For List of ID'S

Fixture Table
uid home_uid away_uid winner date season_division_uid
1 26 6 6 2013-07-30 18
2 8 21 8 2013-06-30 18
3 6 8 8 2013-06-29 18
4 21 26 21 2013-05-20 18
5 6 26 6 2013-04-19 18
This table contains hundreds of rows.
Currently I have a query to select all the teams in a division, i.e.
SELECT team_uid
FROM Season_Division_Team
WHERE season_division_uid='18'
which lists the rows of team uid's i.e. [6,26,8,21,26].
Now for each of the unique team ids, I would like to return the last 3 winner values, ordered by the date column, that they were involved in (they could be an away_uid or home_uid).
So the returned value example would be:
team_id winner date
6 6 2013-07-30
6 8 2013-06-29
6 26 2013-04-19
26 6 2013-07-30
26 21 2013-05-20
26 6 2013-04-19
Any ideas? Thank you
Im not sure how to get it direct, a query like
select * from Season_division_Team where
`date >= (select min(`date`) from
(select `date` from season_division_team order by date desc limit 3))
and (home_uid = 6 or away_uid = 6)
Thats not going to be a good query. But only way i can think of currently
Its hard to get the 3rd largest value from SQL Example
the sub query is trying to get the date where the last win occured, and then getting all dates after that where the team played.
EDIT:
SELECT * FROM Season_Division_Team WHERE winner = 6 ORDER BY `date` DESC LIMIT 3
that sounds more like your latter comment