i want to sum the item sorted by master item and it should be sum different for each item, all i can do is like this, could you give me some help to fix my query?
this is the query
WITH Tb0 as
( SELECT masuk.no_masuk as no_bukti,masuk.tanggal,masuk_detail.kode_bahan,masuk_detail.qty as qty_masuk,0 'qty_keluar' from masuk,masuk_detail WHERE masuk.no_masuk=masuk_detail.no_masuk
UNION
SELECT keluar.no_keluar as no_bukti,keluar.tanggal,keluar_detail.kode_bahan,0 'qty_masuk',keluar_detail.qty as qty_keluar from keluar,keluar_detail WHERE keluar.no_keluar=keluar_detail.no_keluar
)
, Tb1 as
(
SELECT tanggal,kode_bahan,qty_masuk,qty_keluar,Row_Number() over (order by tanggal asc) as OrderId
FROM
Tb0
)
SELECT T1.kode_bahan,T1.tanggal,T1.qty_masuk,T1.qty_keluar,(Sum(T2.qty_masuk) - Sum(T2.qty_keluar)) as Balance FROM Tb1 as T1
INNER JOIN
Tb1 as T2
ON T1.OrderId >= T2.OrderId
Group By T1.kode_bahan,T1.tanggal,T1.qty_masuk,T1.qty_keluar
Order BY tanggal
and this is the result
result query
all i want is sorted by kode_bahan(master item) and tangggal(date) like this
result i want
Related
First query:
SELECT COUNT(sales.ucid) AS totalOutcomes
FROM sales
group by date(sales.saleDate)
Second query:
SELECT COUNT(*) AS joinedOutcomes
FROM sales
JOIN calls
ON sales.ucid = calls.call_id
group by date(sales.saleDate)
I now want to use the output from the second query and divide that by the output from the first query.
Can someone please help with this? Thanks!
Join the two queries.
SELECT t1.date, joinedOutcomes/totalOutcomes
FROM (
SELECT date(sales.saleDate) AS date, COUNT(sales.ucid) AS totalOutcomes
FROM sales
GROUP BY date
) AS t1
JOIN (
SELECT date(sales.saleDate) AS date, COUNT(*) AS joinedOutcomes
FROM sales
JOIN calls ON sales.ucid = calls.call_id
group by date
) AS t2 ON t1.date = t2.date
I want to make a query where I want to get all the values from a table1 prior to the max date from the date column.
I have table1 with column names as id,ord_date.
Further, I want to join it to another table2 with column as id, name
SELECT s.title,s.title_id,t.ord_date
FROM titles s
INNER JOIN (
SELECT title_id
,max(ord_date) AS ord_date
FROM sales
where ord_date < max(ord_date)
GROUP BY title_id
) t ON s.title_id = t.title_id
I want solution query showing title from table2 and dates from table1
Query.
SELECT s.title,s.title_id,t.ord_date
FROM titles s
INNER JOIN (
SELECT x.title_id, max(x.ord_date) AS ord_date
FROM sales x
where x.ord_date < (
select max(m.ord_date) from sales m where m.title_id = x.title_id
)
GROUP BY title_id
) t ON s.title_id = t.title_id
I have a table in MySQL and I want to do some group by operation. So here is my table
The expected result should be the sum of prices group by bag_no but the product_id should be the mainproduct='Y'. So the result should be like this
I can do the following
SELECT bag_no, sum(price) AS total_price
FROM myTable
GROUP BY bag_no
But this will not allow me to add product_id in the table which I also want. How can we do that?
One canonical way to do this involves joining to a subquery which finds the sums:
SELECT
t1.bag_no,
t1.product_id,
t2.total_price
FROM myTable t1
INNER JOIN
(
SELECT bag_no, SUM(price) AS total_price
FROM myTable
GROUP BY bag_no
) t2
ON t1.bag_no = t2.bag_no
WHERE
t1.mainproduct = 'Y';
With MySQL 8+, which supports analytic functions, we can slightly simplify the above query:
WITH cte AS (
SELECT
t1.bag_no,
t1.product_id,
SUM(price) OVER (PARTITION BY t1.bag_no) total_price,
t1.mainproduct
FROM myTable t1
)
SELECT bag_no, product_id, total_price
FROM cte
WHERE mainproduct = 'Y';
Currently I have the below query to count the number of rows with a distinct (customer username and store id) of the purchase of a certain item. How can I also count the total(all) rows where item_id = 3 in the same query?
SELECT count(*)
FROM (
SELECT DISTINCT customer_username, store_id
FROM transactions
WHERE item_id = 3)
You might try something like this:
SELECT COUNT(*), SUM(customer_cnt) FROM (
SELECT customer_username, store_id, COUNT(*) AS customer_cnt
FROM transactions
WHERE item_id = 3
GROUP BY customer_username, store_id
) t;
Try removing DISTINCT in your query:
SELECT count(*)
FROM (
SELECT customer_username, store_id
FROM transactions
WHERE item_id = 3)
Another way is a cross join of 2 inline views:
select x.distinct_rows, y.total_rows
from (select count(distinct concat(user_name, store_id)) as distinct_rows
from transactions
where item_id = 3) x
cross join (select count(*) as total_rows
from transactions
where item_id = 3) y
I have table with id, item_id, value (int), run (datetime) and i need select value diff betwen last two run per *item_id*.
SELECT item_id, ABS(value1 - value2) AS diff
FROM ( SELECT h.item_id, h.value AS value1, h2.value AS value2
FROM ( SELECT id, item_id, value
FROM table_name
GROUP BY item_id
ORDER BY run DESC) AS h
INNER JOIN ( SELECT id, item_id, value
FROM table_name
ORDER BY run DESC) AS h2
ON h.item_id = h2.item_id AND h.id != h2.id
GROUP BY item_id) AS h3
I believe this should do the trick for you. Just replace table_name to correct name.
Explanation:
Basicly I join the table with itself in a run DESC order, JOIN them based on item_id but also on id. Then I GROUP BY them again to remove potential 3rd and so on cases. Lastly I calculate the difference between them through ABS(value1 - value2).
SELECT t2.id, t2.item_id, (t2.value- t1.value) valueDiff, t2.run
FROM ( table_name AS t1
INNER JOIN
table_name AS t2
ON t1.run = (SELECT MAX(run) FROM table_name where run < t2.run)
and t1.item_id = t2.item_id)
This is assuming you want the diff between a record and the record with the previous run