How to show ratio in the second axis with chart Devexpress - winforms

I have a database
CREATE TABLE table1 (
name nvarchar(50),
total int,
ratio float
);
insert into table1 (name, total, ratio) values ('dream', 1200, 0.15)
insert into table1 (name, total, ratio) values ('SH', 2200, 0.2)
insert into table1 (name, total, ratio) values ('Novo', 1000, 0.35)
I would like to show total in the first axis on the left and ration on second axis on the right as below
pls give me your advise

You have to add secondary axis.
Something like this:
var anotherY = new SecondaryAxisY("Tyle");
((XYDiagram)chartControl1.Diagram).SecondaryAxesY.Add(anotherY);
((LineSeriesView)seriesTyle.View).AxisY = anotherY;

Related

Reason why SQLite query returns the wrong value with sum

I've come across a weird behavior in SQLite when running a query and I'd like to understand why the behavior happens.
When I run the following query the discount is calculated incorrectly making the summed value incorrect.
SELECT sum (quantity * price) - (sum (quantity * price)*(discount/100)) as total
FROM [orderProducts]
JOIN [order] ON [order].id = orderProducts.order_id
When the discount is 0, the query behaves as expected i.e the order total is summed correctly. However when there is a set discount the total value isn't correct. The discount seems to get applied at random to the rows when the sum happens. I get the correct behavior when I do do some creative grouping
SELECT sum (total) FROM (SELECT sum (quantity * price) - (sum (quantity * price)*(discount/100)) as total
FROM [orderProducts]
JOIN [order] ON [order].id = orderProducts.order_id
GROUP BY [order].id)
So my question is why does this make such a big difference? What is happening in the first query that makes the discount apply seemingly at random?
To test the queries you can use this statement to make the tables
create table orderProducts
(
id int NOT NULL,
order_id NOT NULL,
quantity int(3),
price double NOT NULL,
primary key (id),
foreign key (order_id) references [order]
);
create table order
(
id int NOT NULL,
discount double NOT NULL,
primary key (id)
);
And to add data you can use
insert into order (id, discount) values (1, 10.0);
insert into order (id, discount) values (2, 0.0);
insert into orderProducts (id, order_id, quantity, price) values (1, 1, 1, 20);
insert into orderProducts (id, order_id, quantity, price) values (2, 2, 1, 50);
The expected output running the sum query is 68 but the actual output is 70
If you expect to get 68 from your sample data, this does the trick:
SELECT sum(quantity * price) - sum((quantity * price) * (discount / 100)) AS total
FROM orderProducts
JOIN "order" ON "order".id = orderProducts.order_id;
Your original query uses sum(quantity * price) * (discount / 100) - that is, it multiplies the sum of all rows totals by a percentage, instead of multiplying each row's total by that row's discount percentage and totalling those numbers up.
Also note my use of double quotes around the order table name instead of brackets; quotes are the normal, standard SQL way of escaping identifiers; brackets are a MS-SQL thing that Sqlite supports for some degree of compatibility.

sql calculation save result in temp column and pas it to another calculation

how do i make sql calculate and pass the result to a temp column and then use that result in another calculation with some values from another tabel and then pass that result to a temp column
as i getting
Invalid column name 'inch'.
SELECT 10 AS inch
SELECT inch *25.4 AS diameter
SELECT P.[merged],
(P.[profile]*2+diameter)*PI() AS circumference
FROM [dbo].[profilelist] P;
i can get it to work with this
SELECT 10 AS inch
select 10*25.4 as diameter
SELECT P.[merged],
(P.[profile] *2+254)*PI() as circumference
FROM [dbo].[profilelist] P;
See if this is what you are looking for.
Declare #inch int, #diameter decimal(5,2)
SELECT #inch=10
SELECT #diameter = #inch * 25.4
SELECT P.[merged],
(P.[profile]*2+#diameter)*PI() AS circumference
FROM [dbo].[profilelist] P;

Getting Largest Distance from group of GPS Coordinates

So I have a database with multiple rows of GPS coordinates. I know how to calculate the distance from a given lat/lng from any one of them in the database, but what I want to do basically is look at the coordinates of a set of rows and get the two rows that are farthest apart. I'd love it if I could do this in SQL, but if I have to do it in my application code that would work to. Here is what I am doing to calculate the distance between two points:
ROUND(( 3960 * acos( cos( radians( :lat ) ) *
cos( radians( p.latitude ) ) * cos( radians( p.longitude ) - radians( :lng ) ) +
sin( radians( :lat ) ) * sin( radians( p.latitude ) ) ) ),1) AS distance
What we are trying to do is, look at GPS data for a specific user and make sure they aren't moving wildly all over the country. All the coordinates for a user should be within a couple miles at most of each other. A flag that there is malicious activity in our system is if the coordinates are all over the country. So I'd like to be able to quickly run through the data for a spcicic user and know what is the max distance they have been.
I thought about just running a Max/Min on the lat and lng separately and set an internal threshold for what is acceptable. And maybe that is easier, but if what I asked in the first part is possible, that would be best.
If you have SQL Server 2008 or later then you can use GEOGRAPHY to calculate the distance, e.g.:
DECLARE #lat1 DECIMAL(19,6) = 44.968046;
DECLARE #lon1 DECIMAL(19,6) = -94.420307;
DECLARE #lat2 DECIMAL(19,6) = 44.33328;
DECLARE #lon2 DECIMAL(19,6) = -89.132008;
SELECT GEOGRAPHY::Point(#lat1, #lon1, 4326).STDistance(GEOGRAPHY::Point(#lat2, #lon2, 4326));
This makes the problem pretty trivial?
For a set of lats/ longs for a user you would need to calculate the distance between each set and then return the highest distance. Putting this all together, you could probably do something like this:
DECLARE #UserGPS TABLE (
UserId INT, --the user
GPSId INT, --the incrementing unique id associated with this GPS reading (could link to a table with more details, e.g. time, date)
Lat DECIMAL(19,6), --lattitude
Lon DECIMAL(19,6)); --longitude
INSERT INTO #UserGPS SELECT 1, 1, 44.968046, -94.420307; --User #1 goes on a very long journey
INSERT INTO #UserGPS SELECT 1, 2, 44.33328, -89.132008;
INSERT INTO #UserGPS SELECT 1, 3, 34.12345, -92.21369;
INSERT INTO #UserGPS SELECT 1, 4, 44.978046, -94.430307;
INSERT INTO #UserGPS SELECT 2, 1, 44.968046, -94.420307; --User #2 doesn't get far
INSERT INTO #UserGPS SELECT 2, 2, 44.978046, -94.430307;
--Make a working table to store the distances between each set of co-ordinates
--This isn't strictly necessary; we could change this into a common-table expression
DECLARE #WorkTable TABLE (
UserId INT, --the user
GPSIdFrom INT, --the id of the first set of co-ordinates
GPSIdTo INT, --the id of the second set of co-ordinates being compared
Distance NUMERIC(19,6)); --the distance
--Get the distance between each and every combination of co-ordinates for each user
INSERT INTO
#WorkTable
SELECT
c1.UserId,
c1.GPSId,
c2.GPSId,
GEOGRAPHY::Point(c1.Lat, c1.Lon, 4326).STDistance(GEOGRAPHY::Point(c2.Lat, c2.Lon, 4326))
FROM
#UserGPS c1
INNER JOIN #UserGPS c2 ON c2.UserId = c1.UserId AND c2.GPSId > c1.GPSId;
--Note this is a self-join, but single-tailed. So we compare each set of co-ordinates to each other set of co-ordinates for a user
--This is handled by the "c2.GPSID > c1.GPSId" in the JOIN clause
--As an example, say we have three sets of co-ordinates for a user
--We would compare set #1 to set #2
--We would compare set #1 to set #3
--We would compare set #2 to set #3
--We wouldn't compare set #3 to anything (as we already did this)
--Determine the maximum distance between all the GPS co-ordinates per user
WITH MaxDistance AS (
SELECT
UserId,
MAX(Distance) AS Distance
FROM
#WorkTable
GROUP BY
UserId)
--Report the results
SELECT
w.UserId,
g1.GPSId,
g1.Lat,
g1.Lon,
g2.GPSId,
g2.Lat,
g2.Lon,
md.Distance AS MaxDistance
FROM
MaxDistance md
INNER JOIN #WorkTable w ON w.UserId = md.UserId AND w.Distance = md.Distance
INNER JOIN #UserGPS g1 ON g1.UserId = md.UserId AND g1.GPSId = w.GPSIdFrom
INNER JOIN #UserGPS g2 ON g2.UserId = md.UserId AND g2.GPSId = w.GPSIdTo;
Results are:
UserId GPSId Lat Lon GPSId Lat Lon MaxDistance
1 3 34.123450 -92.213690 4 44.978046 -94.430307 1219979.460185
2 1 44.968046 -94.420307 2 44.978046 -94.430307 1362.820895
Now I made a LOT of assumptions about what data you are holding as there was no information about the detail of this in your question. You would probably need to adapt this to some degree?

SQL Newbie Needs Assistance w/ Query

Below is what I am trying to do in SQL Server 2012. I want to Update Table 2 with the % of total that each AMT value is to the total in Table 1. But the denominator to get the % should only be the total of the rows that have the same MasterDept. I can use this SELECT query to get the correct percentages when I load the table with only one MasterDept but do not know how to do it when there are multiple MasterDept. The first 2 columns in each table are identical both in structure and the data within the columns.
SELECT ABCID,
[AMT%] = ClientSpreadData.AMT/CONVERT(DECIMAL(16,4),(SELECT SUM(ClientSpreadData.AMT)
FROM ClientSpreadData))
FROM ClientSpreadData
Table data
TABLE 1 (MasterDept varchar(4), ABCID varchar(20), AMT INT)
Sample Data (4700, 1, 25),
(4300, 2, 30),
(4700, 3, 50),
(4300, 4, 15)
TABLE 2 (MasterDept varchar(4), ABCID varchar(20), [AMT%] INT)
Sample Data (4700, 1, AMT%)
AMT% should equal AMT / SUM(AMT). SUM(AMT) should only be summing the values where the MasterDept on Table 1 matches the MasterDept from the record on Table 2.
Does that make sense?
You can use a window to get a partitioned SUM():
SELECT MasterDept, ABCID, AMT, SUM(AMT) OVER(PARTITION BY MasterDept)
FROM #Table1
You can use that to get the percentage for each row to update your second table (this assumes 1 row per MasterDept/ABCID combination):
UPDATE A
SET A.[AMT%] = B.[AMT%]
FROM Table2 A
JOIN (SELECT MasterDept
, ABCID
, AMT
, CASE WHEN SUM(AMT) OVER(PARTITION BY MasterDept) = 0 THEN 0
ELSE AMT*1.0/SUM(AMT) OVER(PARTITION BY MasterDept)
END 'AMT%'
FROM #Table1
) B
ON A.MasterDept = B.MasterDept
AND A.ABCID = B.ABCID
As you can see in the subquery, a percent of total can be added to your Table1, so perhaps you don't even need Table2 as it's a bit redundant.
Update: You can use a CASE statement to handle a SUM() of 0.

Subtract data from the same column

I am having difficulties subtracting data from the same column. My data structure looks like this:
TimeStamp ID state Gallons
1/01/2012 1 NY 100
1/05/2012 1 NY 90
1/10/2012 1 NY 70
1/15/2012 1 NY 40
So I would like to get the result of (100-40) = 60 gallons used for that time period. I tried using the following query:
SELECT T1.Gallons, T1.Gallons -T1.Gallons AS 'Gallons used'
FROM Table 1 AS T1
where Date = '2012-07-01'
ORDER BY Gallons
It seems like a simple way of doing this. However, I can not find an efficient way of including the timestamp as a way of retrieving the right values (ex. gallon value on 1/1/2012 - gallon value on 1/30/2012).
ps. The value of "Gallons" will always decrease
select max(gallons) - min(gallons)
from table1
group by Date
having Date = '2012-07-01'
try
select max(gallons) - min(gallons)
from table1 t1
where date between '2012-01-01' and '2012-01-31'
SELECT MAX(gallons) - MIN(gallons) AS 'GallonsUsed'
FROM table;
would work in this instance, if you are doing a monthly report this could work. How does the value of gallons never increase? what happens when you run out?
Will you always be comparing the maximum and minimum amount? If not, maybe something like the following will suffice (generic sample)? The first time you'll run the script you'll get an error but you can disregard it.
drop table TableName
create table TableName (id int, value int)
insert into TableName values (3, 20)
insert into TableName values (4, 17)
insert into TableName values (5, 16)
insert into TableName values (6, 12)
insert into TableName values (7, 10)
select t1.value - t2.value
from TableName as t1 inner join TableName as t2
on t1.id = 5 and t2.id = 6
The thing you're asking for is the last line. After we've inner joined the table onto itself, we simply request to match the two lines with certain ids. Or, as in your case, the different dates.

Resources