SQL Grouping to show sum - sql-server-2008

I Have below data returned from my code :
Part QTYPerUnit Multiplier
PN1 8 1
PN1 72 2
PN1 40 3
PN1 16 4
PN1 24 6
I need the results in new table to be grouped in 1 row and show the total:
Part QTYPerUnit Multiplier
PN1 160 16

Simply putting following query for the given data will give you desired output
SELECT SUM([QTYPerUnit]) QTYPerUnit, SUM([Multiplier]) Multiplier FROM [TABLE_NAME]
If you have multiple parts, you can use GROUP BY [Part] like following.
SELECT [Part], SUM([QTYPerUnit]) QTYPerUnit, SUM([Multiplier]) Multiplier
FROM [TABLE_NAME]
GROUP BY [Part]
To insert into a new table, you can do it in following way.
INSERT INTO [NEW_TABLE]([Part],[QTYPerUnit],[Multiplier])
SELECT [Part],SUM([QTYPerUnit]), SUM([Multiplier])
FROM [TABLE_NAME]
GROUP BY [Part]
Or simpally
SELECT [Part],SUM([QTYPerUnit]) QTYPerUnit, SUM([Multiplier]) Multiplier
INTO [NEW_TABLE]
FROM [TABLE_NAME]
GROUP BY [Part]
Above statement will crate a new table and will insert the values. It will throw error if the table already exists.
DEMO

Related

Insert unique records into table based on calculation of other table column MYSQL

I have two tables(ProductionDetails, Production), I need to populate Production based on calculated values of the ProductionDetails. ProductionDetails has the number of buckets gathered, Production needs to be populated with BINS. For example if I have 100 buckets, and I divide those by 30 that equals 3.33 bins. Now I need to create 3 BINS.
select Round( sum(ep.Buckets)/30),ep.lot,ep.crewid
from On_EmpProdDetails ep
group by ep.buckets, ep.lot,ep.crewid
Results in
Bins
Lot
Crew
3
556790186SOCC1
SOCC1
Now I want to insert 3 unique rows into the Production Table
BinID is auto increment
BinID
LOT
1
556790186SOCC1
2
556790186SOCC1
3
556790186SOCC1
You can use a recursive CTE to generate the rows:
insert into production (lot)
with recursive cte as (
select Round( sum(ep.Buckets)/30) as num_bins, ep.lot, ep.crewid, 1 as bin
from On_EmpProdDetails ep
group by ep.buckets, ep.lot,ep.crewid
union all
select num_bins, lot, crewid, bin + 1
from cte
where bin < num_bins
)
select lot
from cte;
Here is a db<>fiddle.

Reorganizing mysql aggregate row into single piece rows

Consider the following mysql table:
ID WeightS AmountS WeightM AmountM WeightL AmountL Someothercolumnshere
1 6 3 10 2 18 2 ...
I need to reorganize this data into a pivot-friendly table, where each piece in the amount columns should be one result row. E.g. from the first two columns, WeightS and AmountS, the SELECT should produce 3 result rows, each having a weight of 2 kgs (=6 kgs total). So the full result table should be like this:
Weight Someothercolumnshere
2 ...
2 ...
2 ...
5 ...
5 ...
9 ...
9 ...
I don't even know if there's a SQL syntax which is able to do this kind of operation? I've never had a request like this before. Worst case scenario, I have to do it in php instead, but I think MYSQL is a lot more fun :p
I've built the schema on sqlfiddle, but I'm afraid that's all I've got.
You need a Tally table for the task like this. Create as much rows as needed in it.
Create table Tally(`N` int);
insert into Tally( `N`) values(1),(2),(3),(4),(5);
Then
(select `ID`, `WeightS`/`AmountS`, `Someothercolumnshere`
from Catches
join Tally on Catches.`AmountS` >= Tally.`N`
)
UNION ALL
(select `ID`, `WeightL`/`AmountL`, `Someothercolumnshere`
from Catches
join Tally on Catches.`AmountL` >= Tally.`N`
)
UNION ALL
(select `ID`, `WeightM`/`AmountM`, `Someothercolumnshere`
from Catches
join Tally on Catches.`AmountM` >= Tally.`N`
)

Selecting unique lines based on two columns in mysql

I've been trying to figure out a way to select lines that are unique based on the values of two columns. For example, here is a sample/example of my file:
s r
10 12
10 13
14 10
10 12
14 10
12 10
And this is what I want my output to look like:
s r
10 12
10 13
14 10
I've tried to use this code
SELECT * FROM `message` WHERE (`s_id`=$b_id or 'r_id'=$b_id) GROUP BY
r_id
However, it only select 10,12 10,11 and i also needed 14,10 i mean the third row
in this case $b_id is obtained from session and it may be s or r
You can try to use DISTINCT
SELECT DISTINCT s_id,r_id FROMmessageWHERE (s_id=$b_id or 'r_id'=$b_id);
always try to specify which column needed instead of * (all) it improve the performance
Do it like this
select distinctrow s_id,r_id from table where s_id = 10 or r_id = 10
or else use "having" clause and group by both columns
After two days struggling i am succeeded in finding out solution to my problem
SELECT * FROM `message` WHERE s=$b_id or r=$b_id AND (r and
s)<>(s_ and r) GROUP BY s,r

generating count data using group by in mysql

I have data like
column
1
1
57
57
57
1
1
57
57
I need to generate count data like
count
2
3
2
2
using mysql query. I have tried group by but it groups all the values also i couldn't group by any other fields. Is there any easy way to achieve this?
find the screenshot in this link
Update:
My end goal is to get the time spend by the user on each course . like the user who has ID 1 have spend time from 1177924991 to 1177925038 ( here 1177925038 is one second minus the next course visit time) on course id 1
Use this query:
SELECT COUNT(*)
FROM `tablename`
GROUP BY `info`
try using
SELECT COUNT(columnname)
FROM `tablename`
GROUP BY columnname
This will do the needful.
UPDATE
I am getting following result set on running this query :
Use This Query:
Select `course`, count(*) from `tablename`
group by `course`

Unable to apply WHERE/AND on MySQL table with 2 columns on MAMP

I thought I had a very simple query to perform, but I can't seem to make it work.
I have this table with 2 columns:
version_id trim_id
1 15
1 25
1 28
1 30
1 35
2 12
2 25
2 33
2 48
3 11
3 25
3 30
3 32
I am trying to get any version-id's that have say a sub-set of trim_id's. Let's say all version_id's that have trim_id's 25 and 30. My obvious attempt was :
SELECT * FROM table WHERE trim_id=25 AND trim_id=30
I was expecting to have version_id 1 and 3 as a result, but instead I get nothing.
I am working with the latest version of MAMP, which has some odd behavior, like in this case it just tells me its 'LOADING' and never gives me an error message or something. But that's normally the case when there is no data to return.
This is InnoDB, if that helps.
Thanks for your input.
Your query does not work because you are using AND and the trim_id cannot have two different values at the same time, so you need to apply Relational Division to get the result.
You will need to use something similar to the following:
SELECT version_id
FROM yourtable
WHERE trim_id in (25, 30)
group by version_id
having count(distinct trim_id) = 2
See SQL Fiddle with Demo.
This will return the version_id values that have both 25 and 30. Then if you wanted to include additional columns in the final result, you can expand the query to:
select t1.version_id, t1.trim_id
from yourtable t1
where exists (SELECT t2.version_id
FROM yourtable t2
WHERE t2.trim_id in (25, 30)
and t1.version_id = t2.version_id
group by t2.version_id
having count(distinct t2.trim_id) = 2);
See SQL Fiddle with Demo
SELECT *
FROM table
WHERE trim_id IN(25,30)