I am using mySQL. I have a table that is updated everyday based on our sku. The sku has duplicate values because there is a pricing tier associated with it. I need to create a view that will show all the rows but only 1 sku and the associated sku rows beneath it so i can export everyday for an update in magento. Magento does not allow duplicate sku's during an import. Here is an example:
sku _tier_price_qty _tier_price_price
013964223286 10 1
50 1
100 1
9332153001025 5 1
25 1
50 1
9332153001032 5 1
25 1
50 1
9332153001063 5 1
25 1
50 1
9332153001049 5 1
25 1
50 1
640420002569 5 1
25 1
50 1
640420002538 5 1
25 1
50 1
640420002521 5 1
25 1
50 1
Although this type of formatting might be better to handle in a reporting application this query should do what you want:
select
case when x.tier_qty is not null then t.sku else null end tier_qty,
t.tier_qty,
t.tier_price
from t
left join (
select sku, min(tier_qty) tier_qty
from t
group by sku
) x on t.sku = x.sku and t.tier_qty = x.tier_qty
order by t.sku, t.tier_qty;
http://www.sqlfiddle.com/#!9/3314bd/1
Can you try this?
select case when (select MIN(tier_qty) from products where sku = o.sku) = tier_qty
then sku
else null,
tier_qty,
tier_price
from products o
order by sku, tier_qty
Related
database name = db_structure
id | param_id | occupation_id | amount | active
1. 1 1 20000 1
2. 2 1 20000 1
3. 3 1 20000 1
4. 1 1 20000 1
5. 2 2 20000 1
6. 3 2 20000 1
7. 4 2 20000 1
what query will I write to loop through the table to fetch the records on each row but only show occupation_id once on a table even if it appears more than once?
SELECT tbl_salarystructure.occupationid, SUM(amount), tbl_occupation.*
FROM `tbl_salarystructure`
INNER JOIN tbl_occupation ON
tbl_salarystructure.occupationid = tbl_occupation.id
WHERE tbl_salarystructure.active = 1 ORDER BY occupationid
the result of the query is this when I use php to fetch the rows on the table
Report table
Department | Amount
1 20000
1 20000
1 20000
1 20000
2 20000
2 20000
2 20000
I want the department to show once with the total sum of the amount column using php where their occupation id is thesame
Thanks guys, I got the solution to my problem... I changed the ORDER BY in my sql to GROUP BY the column I want and it came out perfect
I think you just want GROUP BY:
SELECT o.*, SUM(amount)
FROM tbl_occupation o JOIN
tbl_salarystructure ss
ON ss.occupationid = o.id
WHERE ss.active = 1
GROUP BY o.occupationid;
In general, you don't want to use SELECT * with GROUP BY. The one exception is when the primary key for the table (or any unique key actually) is one of the GROUP BY keys.
How to get those entries which have more than 1 records?
If it doesn't make sense... let me explain:
From the below table I want to access the sum of the commission of all rows where type is joining and "they have more than 1 entry with same downmem_id".
I have this query but it doesn't consider more entries scenario...
$search = "SELECT sum(commission) as income FROM `$database`.`$memcom` where type='joining'";
Here's the table:
id mem_id commission downmem_id type time
2 1 3250 2 joining 2019-09-22 13:24:40
3 45 500 2 egbvegr new time
4 32 20 2 vnsjkdv other time
5 23 2222 2 vfdvfvf some other time
6 43 42 3 joining time
7 32 353 5 joining time
8 54 35 5 vsdvsdd time
Here's the expected result: it should be the sum of the id no 2, 7 only
ie. 3250+353=whatever.
It shouldn't include id no 6 because it has only 1 row with the same downmem_id.
Please help me to make this query.
Another approach is two levels of aggregation:
select sum(t.commission) income
from (select sum(case when type = 'joining' then commission end) as commission
from t
group by downmem_id
having count(*) > 1
) t;
The main advantage to this approach is that this more readily supports more complex conditions on the other members of each group -- such as at most one "joining" record or both "joining" records and no more than two "vnsjkdv" records.
Use EXISTS:
select sum(t.commission) income
from tablename t
where t.type = 'joining'
and exists (
select 1 from tablename
where id <> t.id and downmem_id = t.downmem_id
)
See the demo.
Results:
| income |
| ----- |
| 3603 |
You can use subquery that will find all downmem_id having more than one occurrence in the table.
SELECT Sum(commission) AS income
FROM tablename
WHERE type = 'joining'
AND downmem_id IN (SELECT downmem_id
FROM tablename t
GROUP BY downmem_id
HAVING Count(id) > 1);
DEMO
I have two table
ORDER
prod_id qty gst gst_rate
1 25 yes 18
1 25 no 0
2 10 yes 12
3 5 no 0
RETURN
prod_id add less gst_rate
1 5 0 0
1 10 0 18
3 0 2 0
About the table ORDER
Product 1 have order 25 with gst 18% and order 25 without gst. Product
2 have order 10 with gst 12%. Product 3 have order 5 without gst.
About the table Return
Product 1 without gst have extra qty 5. Product 1 with gst have extra
qty 10. Product 3 (no gst) have less qty 2. Product 2 have no extra or
less qty.
So I have to create a VIEW for each entry in ORDER table
Result should look like this
prod_id qty gst_rate add less
1 25 18 10 0
1 25 0 5 0
2 10 12 0 0
3 5 0 0 2
What I tried is:
SELECT ord.prod_id, ord.qty, ord.gst_rate, ret.add, ret.less FROM order ord LEFT JOIN (SELECT case when ord.gst='no' then (select sum(add) from return,order where order.prod_id=return.prod_id and return.gst_rate=0) else (select sum(add) from return,order where order.prod_id=return.prod_id and return.gst_rate!=0) end as add FROM return) as ret ON ret.prod_id=ord.prod_id
But it is not working..
you query could be refactored avoiding subquery ..
SELECT ord.prod_id, ord.qty, ord.gst_rate,
case when rd.gst='no' then t1.sum_add else t2.sum_add end as add
FROM order ord
LEFT JOIN (
select prod_id, sum(add) as sum_add
from return
INNER JOIN order ON order.prod_id=return.prod_id and return.gst_rate=0
GROUP BY prod_id
) t1 on t1.prod_id = ord.prod_id
LEFT JOIN (
select prod_id, sum(add) as sum_add
from return
INNER JOIN order ON order.prod_id=return.prod_id and return.gst_rate!=0
GROUP BY prod_id
) t2 on t2.prod_id = ord.prod_id
(the less was not in you code so i have omitted by select )
Considering the following query:
SELECT t.recording_id, m.release_id
FROM track t
JOIN medium m ON t.medium_id = m.medium_id
i get a result set similar to this one
recording id release id
----------------------------------
1 25
1 25
1 37
1 76
1 300
1 336
2 37
... ...
i need to output the following
recording id count
---------------------------------------------------
1 5
2 1
In other words, i need to group by the recording_id but not count the release_id duplicates for that recording_id
After researching this board i've tried the following, with no success :
SELECT t.recording_id, count(t.recording_id)
FROM track t
JOIN medium m ON t.medium_id = m.medium_id
group by t.recording_id, m.release_id
but, im getting
recording id release id
--------------------------
1 2
1 1
1 1
1 1
1 1
2 1
What's wrong?
Try this, you can use distinct in your count function to return distinct release ids for a recording_id
SELECT t.recording_id, count(distinct m.release_id) cnt
FROM track t
JOIN medium m ON t.medium_id = m.medium_id
group by t.recording_id
I am trying to compare 2 rows and display the same ones.I did browse but was not able to find the right solution.
Table A
Count status Division
20 A 1
30 B 2
10 c 1
12 z 1
From the above table I want to display whose division is same.
Count status Division
20 A 1
10 c 1
12 z 1
Try this
Select * from TableA
Group By Division
Having Count(*) > 1
Select * from TableA
Group By Division
having Count(*) = 1
Here i used case statement , it worked for me
select CompanyCode ,'Commission Pec', Year
,sum(case when CommissionType='Commission Recevied' then JAN else 0 end)/sum(case when CommissionType='Net Payments from WM' and isnull(JAN,0)<>0 then JAN else 1 end)
from Commission_Consolidate
group by CompanyCode,Year
end