SUM with specific condition in crystal report - crystal-reports

Basicalyy i want to sum data by certain condition.These are example of my data :
Group 1 - Value:
10,
20,
30,
Group 2-Value:
10,
20,
30,
Group 3-Value:
10,
20,
30,
I want to sum group 1 and group 3 only.How can do it,like in sql we can write select sum(group) from table1 where group <>'Group 2'.How can we do that using formula.Or there's other way to do it.

Related

How to get the second highest number from an array?

I have two arrays:
var arrPrice = [500, 400, 300, 300, 200, 100, 100]
var arrId = [1,2,3,4,5,6,7]
I need the output for id 1 price should be 400, for id 2 price should be 300, for id 3 price should be 200, for id 4 price should be 200, for id 5 price should be 200, for id 6 price should be 100 and last id and price should be same like for id 7 price should be 100. How do I get the values like these.
Try a Sort
Please add some code what you've tried and I can edit this post with further help. Although this seems like this is a CS100 assignment early in the semester. :P

How to Order By a column but array come first

(Sorry for my bad English)
I have an array of users ids as below:
[5, 9, 3, 22, 16]
Obviously the values are dynamic.
Now, I need to SELECT all users but users with above ids come first.
What I've tried so far?
This query gives me exact answer :
SELECT * FROM users WHERE id IN (5, 9, 3, 22, 16)
UNION ALL
SELECT * FROM users WHERE id NOT IN (5, 9, 3, 22, 16);
But is there any better way?
P.S:
I'm using PostgreSQL 10.
Try this:
select *
from users
order by id not in (5, 9, 3, 22, 16), id
As stated in the documentation, an expression used in the ORDER BY clause
can be an arbitrary expression formed from input-column values.
In particular, you can use a Boolean expression, as values of this type are sortable. Note that false < true.
You can use CASE statements on your ORDER BY, I'll give you an example:
select *
from users
order by
CASE WHEN id=5 THEN 1
WHEN id=9 THEN 2
WHEN id=3 THEN 3
WHEN id=22 THEN 4
WHEN id=16 THEN 5
END, id
With CASE you can tell postgres the "priority" or value you want for each id if you know them beforehand. After that I added "id" so the rest of the rows gets ordered properly.

Selecting/Inner Joining multiple values from one table into a single row

I've been searching google all afternoon I'm not even sure how I would word the question I have so that google or bing might understand it. So I have 2 tables(the tables and the database setup were not my doing) one is the details of a product
name, value, weight, weight unit of measure id, length, width, height, dimensions unit of measure id, description
Then another table with the units of measure and their ID values
eg.
id, unit name
1, cm
2, inches
3, lbs
4, kg
5, feet
I need to select from the products table, but replace the unit of measure ID's with the actual unit of measure text from the other table.
the data looks like
sm widgets, $10, 20, 3, 10, 10, 10, 1, small widgets
I want the results to come out like
sm widget, $10, 20, lbs, 10, 10, 10, cm, small widgets
lg widget, $15, 50, kg, 10, 10, 10, inches, large widgets
thanks for any insight you guys can give me
I think you just need to join the tables and return the description:
select
p.name,
p.value,
p.weight,
c.[unitname],
p.length,
p.width,
p.height,
c2.[unitname] as DimensionUnitName,
p.[description]
from products p
inner join unitcodetable c
on c.id = p.[weight unit of measure id]
inner join unitcodetable c2
on c2.id = p.[dimensions unit of measure id]

postgresql postgis : defining a new subzone index consistent with the old one

Here is my problem: I had a polygon layer with an index which looks like this:
id, population
100, 26
200, 12
300, 45
...
I edited the polygon layer and divided some of the polygons into smaller polygons (approximately 3-7 subpolygons). I already took care of having my data splitted between subzones (according to population density). So now I have this:
id, population
100, 22
100, 1
100, 3
200, 6
200, 6
I would like to create a new index that reflects the old one. For instance:
oldId, newId, population
100, 100, 22
100, 101, 1
100, 102, 3
200, 200, 6
200, 201, 6
Things I tried:
Defining a sequence:
DROP SEQUENCE IF EXISTS increment_id;
CREATE TEMP SEQUENCE increment_id INCREMENT BY 1 MINVALUE 0;
SELECT
id,
id+nextval('increment_id') AS new_id
FROM polygon_mapping WHERE id = 100;
THis works well for a single id to rename (the WHERE clause), but I don't know how to restart the sequence for every id.
I made some thinking around using the 'lag' function to compare current id with previous id. But I don't manage make it work.
Any suggestions?
Thank you
ps: I went through
Reset auto increment counter in postgres
where they reset the SEQUENCE but I don't manage to make it work in a SELECT clause.
Maybe using generate_series()?
SELECT id, generate_series(id,id + count(*)) AS newid
FROM polygon_mapping GROUP BY id;
If you want to select additional attributes, use a subquery and group the attributes using array_agg, than select the values from the array in the primary query:
SELECT id,
generate - 1 + id AS newid,
population_array[generate]
FROM (
SELECT id,
generate_series(1,count(*)) AS generate,
array_agg(population) AS population_array
FROM polygon_mapping GROUP BY id
) AS foo ORDER BY newid,id;

How to prevent MySQLi from automatic sorting in a query?

I have a table master where there song details are stored against their ids.
I have another table playlist that is actually a user playlist and only ids from the first table are inserted selectively here.
So the first table holds data like:
1,2,3,4,5,6,7,8,9,10..........112,113........500 etc along with other fields.
The second table only have selective ids.
45, 7, 98, 83, 6, 65, 8, 234, 51, 78.
Now I am running the following query
SELECT * FROM master WHERE id IN (SELECT * FROM playlist)
but the result set returned comes sorted as
6, 7, 8, 45, 51, 65,78, 83, 98, 234 with their corresponding details.
How do I prevent this automatic sorting?
Added a column serialno to the table playlist.
Then used the following query.
select * from audiomaster a inner join playlist_top10 b on b.audioid = a.id order by b.serialno
The rows now are always sorted in the order they are meant to be.