time difference grouped by 2 different parameters - mysql

I am wondering if anyone could help me with a current problem i am having.
I have a database table which has the following columns
enter image description here
I have various different Areas which can have the same product and what i am looking to do is to sum the difference for all the different products in their respective areas. for example in the table above i would get the below results ( product 1 in area 1 total is 3 minutes etc)
Area Product Time
Retail Product 1 00:03:00'
Packing Product 2 00:02:00'
The start and stop columns have the DATETIME datatype
I have tried sum(timediff(stop,start)) and group it by product but this does not appear to work.
Consequently I also want to sum the difference based on the area bit no matter what I put in i always get 181
I would appreciate any help on this

All you need to do is group by your area and product, and sum the times for each. The answer here should help. For your query, you'll just need something like this:
select
area
,product
,SEC_TO_TIME(SUM(TIME_TO_SEC(`stop`) - TIME_TO_SEC(`start`))) as timediff
from products_in_area
group by area, product
If you're not tied to showing the end result as a TIME value (e.g. you can do the formatting in your calling application), your query would look a bit cleaner:
select
area
,product
,SUM(TIMESTAMPDIFF(MINUTE, `start`, `stop`)) Minutes
from products_in_area
group by area, product

Related

GROUP BY one variable but display multiple SQL

I'm trying to GROUP BY one categorical variable but also show another corresponding variable in my output in SQL. Here is what the table looks like:Original Data Table
There are three main variables I'm working with here: Game, Platform and Week. Week variable states the position of the game under the global charts, so 1 would mean its the number 1 game. I want to find the game with the most weeks inside the top 10 BY platform, so I'm trying to get my table to look like this:
Platform | Game | Most_weeks_top10
Right now, I tried the following steps:
SELECT platform, game, COUNT(*) AS total
FROM global_weekly_charts_2013_2014
WHERE week <= 10
GROUP BY game, platform;
Which returns this:
Table Grouped By Platform AND Game
However, I only want the game, platform and total weeks of the game with max number of weeks in top 10. I tried
SELECT game, platform, Max(total) OVER (PARTITION BY platform)
from the derived table but did not get the desired output. I feel like the solution is right there and not that difficult but I can't seem to get the answer.
please add “ORDER BY total DESC LIMIT 10”.try again

SSRS Show a percentage of a a total value

I have a regular Table in SSRS. With 3 Groups...
(Parent) STORE - CLERK - PRODUCT (Child)
I have some regular aggregations. How many PRODUCTS Sold by a CLERK , How Many CLERKS Per STORE and Eventually How many PRODUCTS Per STORE
On top of the Regular Sums And Avgs, I need To Find Out The Percentage of PRODUCT (Type) Meaning a Particular value of that Group.
Example STORE 001 Has Sold 10 RADIOS (a PRODUCT) and There has Been 100 RADIOS sold by all Stores
So Basically What I Need is to show STORE 001 is Responsible for 10% of all RADIO Sales.
(A note: Ideally , I would Like to show this To adjust to the Data - So if I add new products It will group those as products (Naturally) but still give me those percentages)
= fields!product.value / sum(fields!product.value)
in its most basic form you would want to use something like this.
The first will give you the total of the current row of data and the second will give you the total of all rows of that product.
Thus you would have 10 / 100 (per your example).
This is assuming that you have your data structured correctly. Depending on the structure of you report you may need to add a scope to your total summation to make sure that you are not totaling any other datasets that may reference the same product or field.
sum(fields!product.value, "--your dataset here--")

how to use group by on this table

here is a screen shot of my table
I am trying to remove all those rows whose sum of PostAmt comes to be 0 when grouped by the sales_contract_nbr and the name.
for example :
the sales_contract_nbr 51101008103 will be removed when grouped by name and sales_contract_nbr as -96.83 and 96.83 when summed up amounts to 0.
Quite simple right?
but what I want apart from this is that I want to remove the contracts in group. I mean if the contract 51101008195 is grouped it amounts to be 533.87 which won't be removed (highlighted)
But I want to remove it in groups
for example
two rows of contract number 51101008195 should be summed first (see the image below) I mean the amount -533.87 and 533.87 should be summed to get the total of 0. Only one record for the contract should be left.
Update
More Description :
what i want to do is first group the row number 1 and 2 (matching amounts one positive and the other negative) and then group the others. If there were 4 rows of the same contract number then the row 1 and row 2 should have been grouped then the row 3 and row 4 should be grouped if there absolute amounts are same if not the row number 3 and 4 doesn't get deleted.
I want to use group by to eliminate the rows whose total ends up to be 0 and which have the same name or the contract number.
I hope I have made the question clear. If not please ask.
how can it be done?
what i am doing till now is :
SELECT sales_contract_nbr
,name
,SUM(PostAmt) PostAmt
FROM tblMasData
GROUP BY sales_contract_nbr, name
thanks.
Here is what I come up with for now :
SELECT location
,sales_contract_nbr
,name
,SUM(absPostAmt * nbPostAmt) / ABS(SUM(nbPostAmt)) PostAmt
,ABS(SUM(nbPostAmt)) nbPostAmt
,SUM(absPostAmt * nbPostAmt) PostAmtTotal
FROM (
SELECT location
,sales_contract_nbr
,name
,PostAmt
,ABS(PostAmt) absPostAmt
,SUM(CASE WHEN PostAmt >= 0 THEN 1 ELSE -1 END) nbPostAmt
FROM tblMasData
GROUP BY location
,sales_contract_nbr
,name
,PostAmt
,ABS(PostAmt)
) t
GROUP BY location
,sales_contract_nbr
,name
,absPostAmt
HAVING SUM(absPostAmt * nbPostAmt) != 0
See SQLFiddle.
This doesn't totally answer your question, as if you have 100 + 100 - 200 for instance, it won't hide all three rows. But it can be pretty messy to find combinations which equal to 0 among a bunch of rows.
More, if some rows have the same amount, they will be grouped. That's why I added a column counting those rows being equal, and a column summing them up at the end.
This should at least allow you to deal with the data programmatically.
Let me know if this fills your needs, or if you need some improvement (which could involve some not so pretty SQL).

count 0.5 for some IDs

So this is for my dissertation and it is coming allong pretty well. almost finished it now xD
Anyway, I'm making a pub Epos system in access and its all OK, except now I have reached the stock control.
To get the query (Stock = Stock - Sales) I need to do a count query, which is easy enough, though the problem with pubs is they often serve half pints...
Is there any way to get the count SQL function to count certain ProductID's as 0.5?
This is a part of the table, and Product ID 2,4,6,8 and 10 are all relating to half pints and so count needs to recognise them as 0.5 instead of 1.
[URL=http://imageshack.us/photo/my-images/688/2121212e.png/][IMG]http://img688.imageshack.us/img688/64/2121212e.png[/IMG][/URL]
Thanks
Sam
It seems that you need to separate products purchased from products sold, lets say you called products sold "servings".
product (productId, supplierId, orderQuantity, reorderTrigger)
serving (servingId, productId, servingDesc, volumeOfServe)
this way you can have two servings of the one product (eq guiness)
servingDesc - "Guiness 1/2 pint" volumeOfServe - 0.5
servingDesc - "Guiness pint" volumeOfServe - 1

How to get MYSQL to show Zero results when left joining to self? (eg: Monthly Stats)

I have a table with typical statistics on page view and emails. An email creates a row and page view creates a row...
I want to chart the page views and emails on a daily basis for the past 30 days (for example) so I need to show the day and the value... even if it ZERO. I cannot have missing days from my results or the chart breaks.
PageViews Table:
UniqueID | PageID | DateTime
Emails Table:
UniqueID | SenderEmail | RecipientID | RecipientEmail | DateTime
I don't need this to be one query but the problem remains the same for either query... If there are days (or hours, or months.. depending on the group by range) where a specific page was not viewed it is simply omitted from the results array.
Similarly, if there there are days where there were page views but now emails, then the email count query has holes in it...
Essentially, I am trying to create a Google Analytics type chart and need the lines to show ZERO for the days where there are no data in my stats tables...
I think some kind of left self join might work but cannot seem to make it work. I even messed around with an integer table with only one column (values) and rows for 1-31
Can any of you MYSQL gurus help?
The standard solution for this problem is to create a helper table and populate it with all of the dates relevant to your anticipated queries.
Here's an applicable SO Q&A:
How to fill date gaps in MySQL?