Lets say we have a query
SELECT recordType,SUM(amount) FROM records GROUP BY recordType
Output
1: 50
2: 100
3: 150
4: 200
We have 4 different record types which are 1,2,3 and 4. The above query will obviously return values grouped for those 4 types. How can I adjust the query to show grouping based on their paired grouping. Like return the result of recordType 1 and recordType 2 in one row, and for 3 and 4 in second row.
Expected Output
Group 1: 150
Group 2: 350
Like return the result of recordType 1 and recordType 2 in one row, and for 3 and 4 in second row.
You could do this with a case statement. It won't perform very well, but it'll work. Depending on your actual values, the final query could look something like this:
group by case
when type in(1, 2) then 1
when type in(3, 4) then 2
when ...
else ...
end
Part of the problem is doing the grouping correctly, the other part is getting the names of the groups.
If you really have the numbers in question, you can do:
select concat('Group ', cast((recordType - 1)/2 as int)), count(*)
from records r
group by cast((recordType - 1)/2 as int)
If the values are actually not so arithmetically amenable, then a variable is possibly the simplest method:
select concat('Group ', #rn := #rn + 1), count(*)
from records r cross join (select #rn := 0) const
group by (case when type in (1, 2) then 1
when type in (3, 4) then 2
end)
Related
I have really different problem about database query. There is a little bit different scenarios:
I have a table created with 3 columns. They have ID, ItemId, TypeId columns. I need a count query, it should count ItemId and TypeId together but except duplicate columns. For example;
Id ItemId TypeId
-- ------ ------
1 1 1 -> count +1
2 1 1 -> ignore
3 1 2 -> count -1
4 1 2 -> ignore
5 1 1 -> count +1
result count = 1
In the end, if distinct row repeated, count ignore that row. But TypeId data changed for one specific Item it should increase or decrease count. TypeId equals to 1 count +=1, equals to 2 count -=1.
In MySQL, you would seemingly use count(distinct):
select count(distinct itemId, typeId)
from t;
However, you really have a gaps-and-islands problem. You are looking at the ordering to see where things change.
If I trust that the id has no gaps, you can do:
select count(*)
from t left join
t tprev
on t.id = tprev.id + 1
where not ((t.itemId, t.typeid) <=> (tprev.itemId, t.prev.id))
Try the following query. This employs User-defined session variables. It will work in all the cases (including gaps in Id):
SELECT
SUM(dt.factor) AS total_count
FROM
( SELECT
#factor := IF(#item = ItemId AND
#type = TypeId,
0,
IF(TypeID = 2, -1, 1)
) AS factor,
#item := ItemId,
#type := TypeId
FROM your_table
CROSS JOIN (SELECT #item := 0,
#type := 0,
#factor := 0) AS user_init_vars
ORDER BY Id
) AS dt
DB Fiddle DEMO
my Ui return id in this format 1:6:3 so i want to sum the value corresponding to that id. example if id return is = 1:6:3
then output will= 100+50+140=290
How about SELECT SUM(value) FROM table WHERE ID IN (1,6,3) ?
Took some time to figure out what's the question. I think I got it now - it is about Apex and its "capability" to allow multiple selection from, for example, a select list or shuttle item. When you do that, those multiple values are represented as a colon-separated string.
If we suppose that it was a select list item, it requires two values: display and return. The resulting string contains those return values, which means that you selected several items whose return values were 1, 6 and 3 and - as described above - stored as 1:6:3.
In order to do something with them - in your example, sum values from the table - you'll have to first parse that string into rows and join the result with the "real" table (the one whose image you posted). Let's call it the TEST table; here it is:
SQL> create table test (id number, value number, name varchar2(2));
Table created.
SQL> insert into test
2 select 1, 100, 'a' from dual union
3 select 2, 110, 'b' from dual union
4 select 3, 140, 'c' from dual union
5 select 4, 203, 'd' from dual union
6 select 5, 230, 'e' from dual union
7 select 6, 50 , 'f' from dual;
6 rows created.
In Apex, you'd do it as follows: P1_ITEM is a Page 1 item that contains the '1:6:3' string:
select sum(t.value) result
from test t join (select regexp_substr(:P1_ITEM, '[^:]+', 1, level) id
from dual
connect by level <= regexp_count(:P1_ITEM, ':') + 1
) a on a.id = t.id;
Just to demonstrate it in SQL*Plus, this is the result:
SQL> select sum(t.value) result
2 from test t join (select regexp_substr('&&P1_ITEM', '[^:]+', 1, level) id
3 from dual
4 connect by level <= regexp_count('&&P1_ITEM', ':') + 1
5 ) a on a.id = t.id;
Enter value for p1_item: 1:6:3
RESULT
----------
290
SQL>
I am having trouble with my sql query (Select distinct didnt work).
My sql is :
select distinct
count(T2.Column1)
from Table1 t2
where T2.Column1='2017-05-210'
The actual Column 1 data only have 3 data,
But the output is 12
Nb :
- Column1 data is having 1 to Many situation with Column2,
Here are the actual data:
Column 1 Column 2
1 A
1 B
1 C
1 D
2 A
2 B
2 C
2 D
3 A
3 B
3 C
3 D
Can anyone help me?
Really appreciate for your attention.
Thanks!
You want to count distinct values, so do count(distinct ):
select count(distinct T2.Column1)
from Table1 t2
where T2.Column1='2017-05-210'
(However, your sample data and the query's data/columns do not match.)
Your sample data, result and query do not match.
However, what your query does is:
Find all records with a column1 = '2017-05-210'.
Count all of these records where column1 is not null (which is true for all these records, as column1 = '2017-05-210').
This results in one number (one row, one column). But you additionally say with DISTINCT that you want to remove any duplicates from your result rows. With one row only there can be no duplicates, so the function is superfluous here.
So think about what you want to count really. You count distinct values (i.e. count ignoring duplicates) with COUNT(DISTINCT column), but COUNT(DISTINCT column1) would return 1 of course, because you are only looking for one value which is '2017-05-210' (or zero in case there is no record matching this value).
Could you help me with simple table SUM and COUNT calculating?
I've simple table 'test'
id name value
1 a 4
2 a 5
3 b 3
4 b 7
5 b 1
I need calculate SUM and Count for "a" and "b". I try this sql request:
SELECT name, SUM( value ) AS val, COUNT( * ) AS count FROM `test`
result:
name val count
a 20 5
But should be
name val count
a 9 2
b 11 3
Could you help me with correct sql request?
Add GROUP BY. That will cause the query to return a count and sum per group you defined (in this case, per name).
Without GROUP BY you just get the totals and any of the names (in your case 'a', but if could just as well have been 'b').
SELECT name, SUM( value ) AS val, COUNT( * ) AS count
FROM `test`
GROUP BY name
You need group by
select
name,
sum(value) as value,
count(*) as `count`
from test group by name ;
Instead of executing 2 slightly different sql statements to get a total of 10 results can it be done with just one select?
e.g
select
a
, b
, c
from mytable limit 3
select
a
, b
, LEFT(100, c )
from mytable limit 3, 10
Check out UNION syntax
(SELECT a,b,c FROM mytable LIMIT 3)
UNION
(SELECT a,b,LEFT(100, c) FROM mytable LIMIT 3, 10);
Note the parentheses - these ensure the final LIMIT clause applies to the second query and not the whole result set.
Unless you've got a numeric key in the result which would let you use an IF to format the first n results differently, I don't think you're going to do this with a single select.
You can select all ten rows and then use a case statement to control what value is returned depending on a conditional statement you define.
set #line_total= 0;
select
a,
b,
#line_total := #line_total + 1,
(case when #line_total < 4
then c
else left(100, c) end)
from test_query limit 10;
http://dev.mysql.com/doc/refman/5.0/en/case-statement.html