MySQL: Get total each row then get total each 'group by' - mysql

Sorry about the title, I don't know what's the proper term to use.
I have this data:
I want to produce a table something like this:
I want to get the total of each 'group by' and I'm doing this code but I'm getting wrong total.
SELECT code, (a * b)
AS total_each_code
FROM table1
GROUP BY code
UPDATE: updated photos of sample data, sorry for typos.

you can use sub query for this
select code, sum(a.total_each_code)
from (
SELECT code, (a * b)
AS total_each_code
FROM table1 order by code
)a
group by a.total_each_code
or just simple as
select code,sum(a*b) as total from table1 group by code.

Use sum() aggregation
SELECT code, sum(a * b)
AS total_each_code
FROM table1
GROUP BY code

Related

SQL COUNT if a specific value is found and output in the same line

I need some help with the query below.
I have for example the following data set:
and I need to get the following output:
I tried with a query similar to this one:
SELECT
id, ValA, count(1)
FROM dual
GROUP BY id, ValA;
but it is not working as expected. It's basically duplicating the values in the output:
Would you be able to help me?
count(*) counts all rows. count(ValA) counts non-null values. That means count(*) - count(ValA) counts null's.
SELECT
id, count(*) - count(ValA), count(ValA)
FROM dual
GROUP BY id;

Count the number of occurrences of each article number in MYSQL

I want to count the number of occurrences of each article number in my table. My table has the following structure:
|customerNumber|OrderNumber|ArticleNumber|
|1|1|1|
|2|2|2|
|3|3|4|
|1|4|3|
|3|3|2|
|4|5|2|
|5|6|4|
Expected Outcome:
|ArticleNumber|NumberOfOrders|
|1|1|
|2|3|
|3|1|
|4|2|
How can I do it? ( I got no idea atm)
You can use the count(fieldName) from tablename syntax to achieve your requirement.
So now, the code can be,
select ArticleNumber,count(NumberOfOrders) as NumberOfOrders from
tableName group by ArticleNumber
you need to count ArticleNumber then use below query
SELECT ArticleNumber,COUNT(*) AS NumberOfOrders FROM your_table group by ArticleNumber

Concat 2 columns in a string, then get a count for each concatenation

I am trying to concatenate 2 columns, then count the number of rows i.e. the total number of times the merged column string exists, but I don't know if it is possible. e.g:
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(merged_columns)
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
Note: the colon I've inserted as a part of the string, so my result is something like 12:3. The 'count' then should tell me the number of rows that exist where column_1 =12 and column_2 = 3.
Obviously, it tells me 'merged_columns' isn't a column as it's just an alias for my CONCAT. But is this possible and if so, how?
Old question I know, but the following should work without a temp table (unless I am missing something):
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(CONCAT(column_1,':',column_2 ))
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
You can try creating a temp table from your concatenation select and then query that:
SELECT CONCAT(column_1,':',column_2 ) AS mergedColumns
INTO #temp
FROM table
SELECT COUNT(1) AS NumberOfRows,
mergedColumns
FROM #temp
GROUP BY mergedColumns
Hope this answer is what your are looking for.
Try this
SELECT
CONCAT(column_1,column_2 ) as merged_columns,
COUNT(*)
FROM
table
GROUP BY merged_columns
ORDER BY merged_columns DESC

How to fetch all data and also the number of records in table

I am writing following query to fetch all details of table bill_details.
select * from bill_details;
Along with data, I also want to fetch number of records in this table.
SELECT *, (SELECT COUNT(*) FROM bill_details) AS cnt
FROM bill_details
Every row of the results will have an additional column with the row count.
mysql_num_rows(mysql_query("select * from bill_details"))
You'll have to use two queries for that, or use mysqli_num_rows if you're using MySQLi, as suggested by Rakesh. If you're using PDO, you can use rowCount.
SELECT * FROM bill_details;
Followed by:
SELECT COUNT(*) FROM bill_details;
You can try the query below, but the first column will repeat the count information:
SELECT *
FROM (SELECT * FROM bill_details) A, (SELECT COUNT(*) FROM bill_details) C

Is it possible to sum a expression column in access?

I have a column in access I need to get the total of. When I try the below method in my query I get the error "You tried to execute a query that does not include the specified expression 'ID' as part of an aggregate function."
Expr1: Sum([Column1])
Edit my query:
SELECT tblTest.ID,Field1,Sum([Field1]) AS Expr1 From tblTest;
Your question needs more details but I think you want this:
SELECT Sum(yourColumn)
FROM yourTable
Then if you need to you will have to add a GROUP BY
SELECT Sum(yourColumn)
FROM yourTable
GROUP BY yourTable.Id
Based on your comment your query would be:
SELECT ID
,Field1
,Sum([Field1]) AS Expr1
From tblTest
GROUP BY ID, Field1;