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
Related
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 )
I have a table with the following entries:
id customer amount kind
1 123 15 g
2 123 30 op
3 234 20 g
4 345 25 g
5 456 12 g
6 456 15 op
What I want to do is to sum all amounts with the kind "g".
Now I want to add a condition:
"Only sum the amount to the sum if there is another entry of the customer with the kind 'op'"
Means my result should be 27 in this case and not 72.
What's a good way to add this condition?
Thanks!
To get the sum for each customer do
select customer, sum(case when kind = 'g' then amount else 0 end) as c_sum
from your_table
group by customer
having sum(kind = 'op') > 0
to get the total sum do
select sum(c_sum)
from
(
select customer, sum(case when kind = 'g' then amount else 0 end) as c_sum
from your_table
group by customer
having sum(kind = 'op') > 0
) tmp
I have this table in MYSQL:
Year Type Value ID
0 0 5 1
2010 1 6 1
2011 1 4 1
2012 1 5 1
2013 1 7 1
2014 1 8 1
2015 1 5 1
0 0 6 2
2009 1 7 2
2010 1 4 2
2011 1 2 2
2012 1 8 2
2013 1 8 2
2014 1 5 2
I want to select the minimum and maximum year for each person (IDs 1 and 2), but I also want to select the value associated with type 0 for each person as well. Ideally this is what the query result would look like:
ID MinYear MaxYear Type0Value
1 2010 2015 5
2 2009 2014 6
The query should look, I think, something like this...
select ID,
(min(year) where type = 1) as MinYear,
(max(year) where type = 1) as MaxYear,
(value where type = 0) as Type0Value
from table
group by ID
But this is obviously not correct SQL syntax. How do I do this?
strange table structure, but:
select
_type0.id,
_type0.value,
_type1._min,
_type1._max
from
tbl as _type0
inner join (
select
id,
min(year) as _min,
max(year) as _max
from
tbl
where
1 = type
group by
id
) as _type1 on
_type0.id = _type1.id
where
0 = _type0.type;
you should use inner join.
one half will handle the min and max, second half the type0value:
select a.minYear, a.maxYear, a.id, b.type0value from
(select min(year) as minYear, max(year) as maxYear, id from table where id = 1 group by id) as a
inner join table as b on a.id = b.id
where b.type = 0
Your pseudo-code is actually pretty close. You just need conditional aggregation:
select ID,
min(case when type = 1 then year end) as MinYear,
max(case when type = 1 then year end) as MaxYear,
max(case when type = 0 then value end) as Type0Value
from table
group by ID;
If there could be multiple rows with type = 0, you might want group_concat() instead.
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
Let's say I have the following data.
id name_id completed
1 10 1
2 10 0
3 15 1
4 10 0
5 20 1
6 15 0
7 20 1
8 15 0
I'm trying to find a count by the name id, which is pretty simple
SELECT name_id, COUNT(*) FROM db
GROUP BY name_id
Now, I have a second component which I want to include in the query.
For name_id 10, I want to count just those values where completed is 1. For the other name_id's, I want to select them regardless of whether they are 0 or 1.
So I should end up with:
name_id count(*)
10 1
15 3
20 2
Name_id 10 only has a count of 1 because it's just the 1 which is completed, while the other counts include both 0 and 1.
Can anyone help with this task.
Thanks!
You can use a CASE expression inside of your aggregate function.
SELECT name_id,
sum(case
when name_id = 10
then case when completed = 1 then 1 else 0 end
else 1 end) Total
FROM db
GROUP BY name_id;
See SQL Fiddle with Demo.
Exclude the rows where name_id = 10 and completed = 0:
SELECT name_id, COUNT(*) FROM db
WHERE NOT (completed = 0 AND name_id = 10)
GROUP BY name_id
SELECT name_id, COUNT(*) FROM db
WHERE name_id != 10 or completed = 1
GROUP BY name_id
Count when name_id is not 10. If it is 10, count when completed = 1:
SELECT
name_id,
COUNT(CASE WHEN name_id <> 10 or completed = 1 THEN 1 END)
FROM db
GROUP BY name_id