let´s assume we have the following table
_________________________________________________________________________
|FlightNo | DepartureDate | Origin | Destination | Flightduration |
_________________________________________________________________________
| IB233 | 2018-12-31 09:30:45 | Berlin | Barcelona | 200 |
| IB222 | 2019-01-01 12:20:34 | Barcelona| Rome | 100 |
| LE111 | 2019-01-01 11:11:11 | Rome | NY | 400 |
__________________________________________________________________________
Now I want to calculate the timedifference between the Destination of NY and start in Berlin. I want to check whether the whole flightduration is < 660 min.
I worked out this, but it doesn´t work ..
SELECT
TIMEDIFF(
(SELECT adddate(DepartureDate, INTERVAL FlightDuration
MINUTE)
FROM flightexecution
where ICAO_Code_Origin = 'Berlin'),
(SELECT DepartureDate
FROM flightexecution
where ICAO_Code_Destination = 'NY' AND ICAO_Origin NOT LIKE 'Berlin' AND)
) from flightexecution ;
MySQL is giving me just an 'OK' as an result, which means that somehting is wrong...
Could you guys help me out?
You may try a self join query like this
SELECT
A.Origin,
(TIME_TO_SEC(TIME_DIFF(A.DepartureDate, C.DepartureDate))/60 - A.FlightDuration - B.FlightDuration) AS `Transfer time (minutes)`,
A.DepartureDate,
(TIME_TO_SEC(TIME_DIFF(A.DepartureDate, C.DepartureDate))/60 + C.FlightDuration) AS `Overall Duration (minutes)`
FROM
flightexecution A INNER JOIN
flightexecution B
ON A.Destination = B.Origin INNER JOIN
flightexecution C
ON B.Destination = C.Origin
WHERE
A.Origin = 'Berlin'
AND C.Destination = 'NY'
Related
I have 2 tables config_location_workstation and asset_workstation where both have the columns floor and workstation_number.
I want a query to return something like this :
FLOOR| SUM PRODUCTION WORKSTATION | SUM HEAD COUNT
This code below came from another source which I was able to pull up the sum but the numbers are incorrect because what I want to get is the number of all Production workstation.
SELECT config_location_workstation1.floor,
(SELECT COUNT(config_location_workstation2.workstation_number)
FROM config_location_workstation AS config_location_workstation2
WHERE config_location_workstation2.floor = config_location_workstation1.floor) AS SUM_FLOOR,
(SELECT COUNT(asset_workstation2.workstation_number)
FROM asset_workstation AS asset_workstation2
WHERE asset_workstation2.floor = config_location_workstation1.floor) AS SUM_HEAD
FROM config_location_workstation AS config_location_workstation1
INNER JOIN asset_workstation AS asset_workstation1
ON (config_location_workstation1.workstation_number = asset_workstation1.workstation_number)
WHERE config_location_workstation1.workstation_name = 'NORTH PRODUCTION'
GROUP BY config_location_workstation1.floor
The problem is WHERE is not working on this code.
The Workstation Column sum is invalid. It is pulling up all the entries. I only need to query all PRODUCTION workstations
Here's the current output.
+-------+-------------+------+------+
| Floor | Head Count | Workstations|
+-------+-------------+------+------+
| 18TH | 696 | 576 |
| 19TH | 381 | 463 |
| 20TH | 380 | 760 |
+-------+-------------+------+------+
Expected output for all Production workstations
+-------+-------------+------+------+
| Floor | Head Count | Workstations|
+-------+-------------+------+------+
| 18TH | 696 | 497 |
| 19TH | 381 | 388 |
| 20TH | 380 | 659 |
+-------+-------------+------+------+
Probably you could try using a subquery
select * from (
SELECT config_location_workstation1.floor,config_location_workstation1.workstation_name as z,
(SELECT COUNT(config_location_workstation2.workstation_number)
FROM config_location_workstation AS config_location_workstation2
WHERE config_location_workstation2.floor = config_location_workstation1.floor) AS SUM_FLOOR,
(SELECT COUNT(asset_workstation2.workstation_number)
FROM asset_workstation AS asset_workstation2
WHERE asset_workstation2.floor = config_location_workstation1.floor) AS SUM_HEAD
FROM config_location_workstation AS config_location_workstation1
INNER JOIN asset_workstation AS asset_workstation1
ON (config_location_workstation1.workstation_number = asset_workstation1.workstation_number)
GROUP BY config_location_workstation1.floor
) x
WHERE z = 'NORTH PRODUCTION'
UPDATE:
select x.*,y.* from ( SELECT *, COUNT(workstation_name) as COUNT_FLOOR
FROM config_location_workstation WHERE workstation_name LIKE '%PRODUCTION%' GROUP BY floor)x
join
(SELECT *, COUNT(floor) as COUNT_USERS FROM asset_workstation GROUP BY floor) y on
x.workstation_number = y.workstation_number
I want to remove digit after decimal how to solve it?
My query is:
SELECT city_name,
Assignedto,
COUNT(Assignedto) AS TC,
CONCAT(count(CASE
WHEN STATUS = 'CLOSED' THEN 1
ELSE NULL
END) * 100 / count(1), '%') AS SC,
CONCAT(count(CASE
WHEN STATUS = 'PENDING' THEN 1
ELSE NULL
END) * 100 / count(1), '%') AS PC,
SUM(TIMESTAMPDIFF(MINUTE,Request_Date, Xetr))/60 AS WH,
(154440-sum(TIMESTAMPDIFF(MINUTE,Request_Date, Xetr))/60) AS VH,
CONCAT(COUNT(Feedback_Rate)/COUNT(Assignedto)*100,'%') AS Feed_Percent,
SUM(Feedback_Rate)/(count(Feedback_Rate)*5)*5 AS AVG_Feedback
FROM `it_service_ticket`
INNER JOIN `it_problem`ON `it_service_ticket`.`it_problem_id`=`it_problem`.`it_problem_id`
INNER JOIN `city_master` ON `it_service_ticket`.cityid=`city_master`.city_id
WHERE `it_service_ticket`.`xetr` BETWEEN '2016-04-01 12:00:00 AM' AND '2017-02-28 12:00:00 PM'
GROUP BY Assignedto
ORDER BY city_name ASC;
Output
+-------------------------+-------------------------+-------+------------+----------+------------+--------------+-----------+---------+
| City_Name | AssigneeTo | TC | SC | PC | WH | VH | Feedback | Average |
+-------------------------+-------------------------+-------+------------+----------+------------+--------------+-----------+---------+
| Ahmedabad | mahesh.patel#corp.in | 297 | 100.0000% | 0.0000% | 147.0667 | 154292.9333 | 43.4343% | 4.4031 |
| Ahmedabad | mahesh.patel#corp.in | 297 | 100.0000% | 0.0000% | 147.0667 | 154292.9333 | 43.4343% | 4.4031 |
If you want to round off decimal places, use ROUND(yourColumn,0) function.
So 13.78 will become 14
If you want to get rid of decimal places, user FLOOR(yourColumn)
So 13.78 will become 13
So for example
SUM(TIMESTAMPDIFF(MINUTE,Request_Date, Xetr))/60 AS WH
should be changed to
ROUND(SUM(TIMESTAMPDIFF(MINUTE,Request_Date, Xetr))/60,0) AS WH
Edit: This would take care of your %.
CONCAT(
ROUND(count(CASE
WHEN STATUS = 'PENDING' THEN 1
ELSE NULL
END) * 100 / count(1),0)
, '%') AS PC
Do the same for all the columns you need to remove decimal places.
You Should Use TRUNCATE() in Mysql for Example
SELECT TRUNCATE(525.668545, 3) -- 525.668
I have a table ValuationHistory with the following columns
Code | ValuationDate | NetAssetValue | PricePerShare | Subscriptions | Redemptions
ABC | 2014-06-30 | 12546.50 | 100.23 | 60 | 70
CEF | 2014-06-30 | 10025.20 | 120.50 | 30 | 20
ABC | 2014-07-31 | 12505.50 | 101.50 | 40 | 60
ABC | 2014-08-31 | 13051.41 | 102.50 | 35 | 70
Now the user will select a Valuation date and a code from an aspx page. I want to write an sql query that will give me a report with the following columns for all the valuation dates prior to the valuation date selected by the user
Code | BeginningEquity | Subscriptions | Redemptions | EndingEquity
Where
Code is the code selected by the user
BeginningEquity is the NetAssetValue for the previous valuation
Subscriptions is straight forward
Redemptions is straight forward
EndingEquity is the NetAssetValue on the valuationDate
I first created a table variable called #ValDates and got all the valuation dates into this temp table. Then I did a join of #ValDates with the ValuationHistory table.
But I am getting an error on the following query. The error message is:
subquery returned more than one value
Could somebody help me write this query in a better way
SELECT (SELECT NetAssetValue
FROM ValuationHistory
WHERE ValuationDate IN (SELECT Max(ValuationDate)
FROM ValuationHistory
WHERE ValuationDate < nd.ValuationDate)),
Subscriptions,
Redemptions,
EndingEquity
FROM ValuationHistory vh
INNER JOIN #ValDates vd
ON vh.ValuationDate = vd.ValuationDate
WHERE vh.Code = #Code
AND vh.ValuationDate < = #ValuationDate
I'm not very clear about your requirement. Based on the description, you may try to start your query as below
DECLARE #Code VARCHAR(10) = 'ABC'
,#ValuationDate date = '2014-07-31'
;WITH cte AS (
SELECT
rn = ROW_NUMBER() OVER (PARTITION BY Code ORDER BY ValuationDate), *
FROM ValuationHistory
)
SELECT
c1.Code,
c2.NetAssetValue AS BeginningEquity,
c2.ValuationDate AS BeginningDate,
c1.NetAssetValue AS EndingEquity,
c1.ValuationDate AS EndingDate,
c1.[Subscriptions],
c1.[Redemptions]
from cte c1
LEFT JOIN cte c2
ON c1.Code = c2.Code
AND c2.rn = c1.rn - 1
WHERE c1.Code = #Code
AND c1.ValuationDate < = #ValuationDate
SQL Fiddle Demo
I need a bit of help on a mathematical function,
Following query will result 2 lines per lot like this:
group | style | lot | section | q1 | q2 |q3 | q4 | ...
aaaaa | sssss | 123 | 111111 | 55 | 77 | 88 | 99 | ...
aaaaa | sssss | 123 | 222222 | 10 | 20 | 20 | 10 | ...
aaaaa | sssss | 321 | 111111 | 11 | 22 | 44 | 55 | ...
aaaaa | sssss | 321 | 222222 | 10 | 23 |33 | 10 | ...
each lot result 2 diff section code (2 lines)
Question is : how do I make a subtraction between the 2 section code for the colums q1 q2 q3 q4 q5 ... ?
expected results:
group | style | lot | q1 | q2 |q3 | q4 | ...
aaaaa | sssss | 123 | 45 | 57 |68 | 89 | ...
aaaaa | sssss | 321 | 1 | -1 |11 | 45 | ...
query so far :
SELECT DISTINCT gp_style_gr.code_groupe, po_lot.num_style, po_lot_sp.Num_lot,
po_lot_sp.num_secti, po_lot_se.code_secti, po_lot.terminer, po_lot.date_livraison,
po_lot_sp.qte_1, po_lot_sp.qte_2, po_lot_sp.qte_3, po_lot_sp.qte_4, po_lot_sp.qte_5,
po_lot_sp.qte_6, po_lot_sp.qte_7, po_lot_sp.qte_8, po_lot_sp.qte_9, po_lot_sp.qte_10,
po_lot_sp.qte_11, po_lot_sp.qte_12, po_lot_sp.qte_13, po_lot_sp.qte_14, po_lot_sp.qte_15,
po_lot_sp.qte_16, po_lot_sp.qte_17, po_lot_sp.qte_18, po_lot_sp.qte_19, po_lot_sp.qte_20,
po_lot_sp.qte_21, po_lot_sp.qte_22, po_lot_sp.qte_23, po_lot_sp.qte_24, po_lot_sp.qte_25,
po_lot_sp.qte_26, po_lot_sp.qte_27, po_lot_sp.qte_28, po_lot_sp.qte_29, po_lot_sp.qte_30
FROM po_lot_sp
LEFT OUTER JOIN po_lot_se ON po_lot_se.num_lot = po_lot_sp.num_lot
and po_lot_se.num_secti = po_lot_sp.num_secti
LEFT OUTER JOIN po_lot ON po_lot.num_lot = po_lot_sp.num_lot
LEFT OUTER JOIN gp_style_gr ON gp_style_gr.num_style = po_lot.num_style
WHERE
((gp_style_gr.code_groupe = 'INSTOCK') and (po_lot.terminer = '0')
and (po_lot_se.code_secti = '01')) or ((gp_style_gr.code_groupe = 'INSTOCK')
and (po_lot.terminer = '0') and (po_lot_se.code_secti = '09'))
ORDER BY gp_style_gr.code_groupe, po_lot.num_style, po_lot_sp.Num_lot,
po_lot_sp.num_secti, po_lot_se.code_secti, po_lot.terminer, po_lot.date_livraison,
Thanks !
If the section code follows some pattern as it does in your example, then you can simply join the table against itself.
I'll pretend your table is called po_lot_sp as it is in your example.
In the following query, I assume that the second row has a higher section number. That's the condition t1.section > t2.section. If not, change appropriately. If the section numbers follow no pattern, then ignore this completely.
SELECT t1.`group`, t1.style, t1.lot, t1.section,
t2.q1 - t1.q1 q1, t2.q2 - t1.q2 q2, t2.q3 - t1.q3 q3, t2.q4 - t1.q4 q4
FROM t t1
JOIN t t2 ON t1.`group` = t2.`group` AND t1.style = t2.style AND
t1.lot = t2.lot AND t1.section > t2.section
Fiddle here.
This is the fastest way I can think of. Of course, this is assuming that you want to decrement from section with value '111111':
SELECT `group`, style, lot,
sum(if(section = '111111', q1, -q1)),
sum(if(section = '111111', q2, -q2)),
sum(if(section = '111111', q3, -q3)),
sum(if(section = '111111', q4, -q4))
FROM t
GROUP BY `group`, style, lot
Fiddle here.
By the way, try not to use group as a column name. It is a reserved word.
If you don't know the section value you want to decrement from and you only want to decrement from the lowest section, then go for Andy's solution.
Try this:
select a.group,a.style,a.lot,
coalesce(a.q1 -
(select b.q1 from tablename b where b.ID = a.ID + 1), a.q1) as q1,
coalesce(a.q2-
(select b.q2 from tablename b where b.ID = a.ID + 1), a.q2) as q2,
coalesce(a.q3-
(select b.q3 from tablename b where b.ID = a.ID + 1), a.q3) as q3,
coalesce(a.q4-
(select b.q4 from tablename b where b.ID = a.ID + 1), a.q4) as q4
from tablename a group by a.lot
Note: Here ID refers to a primary key from your table and tablename refers to your original table name.So replace the field ID with your primary key field and table name vice-versa.
Demo
Update #1: query gives me syntax error on Left Join line (running the query within the left join independently works perfectly though)
SELECT b1.company_id, ((sum(b1.credit)-sum(b1.debit)) as 'Balance'
FROM MyTable b1
JOIN CustomerInfoTable c on c.id = b1.company_id
#Filter for Clients of particular brand, package and active status
where c.brand_id = 2 and c.status = 2 and c.package_id = 3
LEFT JOIN
(
SELECT b2.company_id, sum(b2.debit) as 'Current_Usage'
FROM MyTable b2
WHERE year(b2.timestamp) = '2012' and month(b2.timestamp) = '06'
GROUP BY b2.company_id
)
b3 on b3.company_id = b1.company_id
group by b1.company_id;
Original Post:
I keep track of debits and credits in the same table. The table has the following schema:
| company_id | timestamp | credit | debit |
| 10 | MAY-25 | 100 | 000 |
| 11 | MAY-25 | 000 | 054 |
| 10 | MAY-28 | 000 | 040 |
| 12 | JUN-01 | 100 | 000 |
| 10 | JUN-25 | 150 | 000 |
| 10 | JUN-25 | 000 | 025 |
As my result, I want to to see:
| Grouped by: company_id | Balance* | Current_Usage (in June) |
| 10 | 185 | 25 |
| 12 | 100 | 0 |
| 11 | -54 | 0 |
Balance: Calculated by (sum(credit) - sum(debits))* - timestamp does not matter
Current_Usage: Calculated by sum(debits) - but only for debits in JUN.
The problem: If I filter by JUN timestamp right away, it does not calculate the balance of all time but only the balance of any transactions in June.
How can I calculate the current usage by month but the balance on all transactions in the table. I have everything working, except that it filters only the JUN results into the current usage calculation in my code:
SELECT b.company_id, ((sum(b.credit)-sum(b.debit))/1024/1024/1024/1024) as 'BW_remaining', sum(b.debit/1024/1024/1024/1024/28*30) as 'Usage_per_month'
FROM mytable b
#How to filter this only for the current_usage calculation?
WHERE month(a.timestamp) = 'JUN' and a.credit = 0
#Group by company in order to sum all entries for balance
group by b.company_id
order by b.balance desc;
what you will need here is a join with sub query which will filter based on month.
SELECT T1.company_id,
((sum(T1.credit)-sum(T1.debit))/1024/1024/1024/1024) as 'BW_remaining',
MAX(T3.DEBIT_PER_MONTH)
FROM MYTABLE T1
LEFT JOIN
(
SELECT T2.company_id, SUM(T2.debit) T3.DEBIT_PER_MONTH
FROM MYTABLE T2
WHERE month(T2.timestamp) = 'JUN'
GROUP BY T2.company_id
)
T3 ON T1.company_id-T3.company_id
GROUP BY T1.company_id
I havn't tested the query. The point here i am trying to make is how you can join your existing query to get usage per month.
alright, thanks to #Kshitij I got it working. In case somebody else is running into the same issue, this is how I solved it:
SELECT b1.company_id, ((sum(b1.credit)-sum(b1.debit)) as 'Balance',
(
SELECT sum(b2.debit)
FROM MYTABLE b2
WHERE b2.company_id = b1.company_id and year(b2.timestamp) = '2012' and month(b2.timestamp) = '06'
GROUP BY b2.company_id
) AS 'Usage_June'
FROM MYTABLE b1
#Group by company in order to add sum of all zones the company is using
group by b1.company_id
order by Usage_June desc;