Reset a running total if field value is hit - sql-server-2008

I am able to keep a running total with the below query, and it does that just fine. What I really want, is when the gap field is greater than or equal to the date_diff field, the running total should reset back to the current hrly_qty. I'm sure I could achieve my results with a cursor, but I wanted to know of possible other ways. Ideas?
Example:
WITH CTE AS
(
SELECT a.*, SUM(b.hrly_qty) AS running_total, c.gap
FROM #tmpTrxhist2 a
INNER JOIN #tmpTrxhist2 b ON a.people_id = b.people_id
AND b.sequence_id <= a.sequence_id
INNER JOIN incent_level c ON a.owner_division_id = c.owner_division_id
GROUP BY a.date_diff, a.owner_division_id, a.people_id, a.sequence_id,
a.hrly_qty, c.gap
)
SELECT * FROM CTE
ORDER BY people_id, sequence_id

You seldom need a cursor! A simple case statement should suffice. Of the top of my head something like:
WITH CTE AS
(
SELECT a.*, SUM(b.hrly_qty) AS running_total, c.gap
FROM #tmpTrxhist2 a
INNER JOIN #tmpTrxhist2 b ON a.people_id = b.people_id
AND b.sequence_id <= a.sequence_id
INNER JOIN incent_level c ON a.owner_division_id = c.owner_division_id
GROUP BY a.date_diff, a.owner_division_id, a.people_id, a.sequence_id,
a.hrly_qty, c.gap
)
SELECT *,
CASE
WHEN gap >= date_diff then hrly_qty else gap
END as comp_gap
FROM CTE
ORDER BY people_id, sequence_id
Run the execution plans between the two but SQL is almost always much happier trying to optimise non-cursored code. In my field at least, you usually find cursors are over used and abused by 'proper' C/C++ programmers because they get in their comfort zone when see something that looks a bit like a while-loop rather than think about sets of data. There is a place for cursors, but this isn't it!

Related

MySQL DISTINCT returning not so distinct results

Good day,
I have a small issue with MySQL Distinct.
Trying the following query in my system :
SELECT DISTINCT `booking_id`, `booking_ticket`, `booking_price`, `bookingcomment_id`, `bookingcomment_message` FROM `mysystem_booking`
LEFT JOIN `mysystem_bookingcomment` ON `mysystem_booking`.`booking_id` = `mysystem_bookingcomment`.`bookingcomment_link`
WHERE `booking_id` = 29791
The point is that there are bookings like 29791 that have many comments added.
Let's say 10. Then when running the above query I see 10 results instead of one.
And that's not the way DISTINCT supposes to work.
I simply want to know if there are any comments. If the comment ID is not 0 then there is a comment. Of course I can add COUNT(blabla) as comment_number but that's a whole different story. For me now I'd like just to have this syntax right.
You may try aggregating here, to find which bookings have at least a single comment associated with them:
SELECT
b.booking_id,
b.booking_ticket,
b.booking_price
FROM mysystem_booking b
LEFT JOIN mysystem_bookingcomment bc
ON b.booking_id = bc.bookingcomment_link
WHERE
b.booking_id = 29791
GROUP BY
b.booking_id
HAVING
COUNT(bc.bookingcomment_link) > 0;
Note that depending on your MySQL server mode, you might have to also add the booking_ticket and booking_price columns to the GROUP BY clause to get the above query to run.
You can try below - using a case when expression
SELECT DISTINCT `booking_id`, `booking_ticket`, `booking_price`, `bookingcomment_id`,
case when `bookingcomment_message`<>'0' then 'No' else 'Yes' end as comments
FROM `mysystem_booking`
LEFT JOIN `mysystem_bookingcomment` ON `mysystem_booking`.`booking_id` = `mysystem_bookingcomment`.`bookingcomment_link`
WHERE `booking_id` = 29791

How to increase performance of complicated MySQL select query?

I am working on project management tool and while generating reports through admin end the loading time for the data from the database is very much > 5 minutes. There are few points which I know would help me to increase the performance but right now I need to have the help in SELECT query
SELECT timesheet_client.organisation as client_name, timesheet_project.title as project_name,
timesheet_task.name as task, CONCAT(timesheet_user.first_name, ' ', timesheet_user.last_name) as resource_name,
timesheet_user.bill_factor, timesheet_client.client_type, sum(spent) as spent, sum(delivered_hours) as delivered_hours,
sum(billable_hours) as billable_hours, comments, color, lock_color, updated_by, updated_by_date, timesheet_user_psdb.grp_id,
timesheet_user_psdb.client_id, timesheet_user_psdb.proj_id, timesheet_user_psdb.task_id, timesheet_user_psdb.uid,
timesheet_user_grp.grp_name
FROM timesheet_user_psdb,timesheet_user, timesheet_client,
timesheet_project,timesheet_task,timesheet_user_grp
WHERE timesheet_user.username=timesheet_user_psdb.uid and
timesheet_client.client_id=timesheet_user_psdb.client_id and timesheet_project.proj_id=timesheet_user_psdb.proj_id and
timesheet_task.task_id = timesheet_user_psdb.task_id and timesheet_user_grp.grp_id=timesheet_user_psdb.grp_id and month =3
AND year = 2017 and month!='' and timesheet_user_psdb.client_id=326
GROUP BY timesheet_user_psdb.task_id,timesheet_user_psdb.uid
ORDER BY timesheet_client.client_type desc,timesheet_client.organisation,timesheet_user_psdb.proj_id,
timesheet_user_psdb.task_id,timesheet_user.uid,timesheet_user_psdb.task_id;
I have already used an index on all the primary keys.
EXPLAIN Output:
Help for this would be highly appreciable.
Give this a try:
SELECT
TC.ORGANISATION AS CLIENT_NAME,
TP.TITLE AS PROJECT_NAME,
TT.NAME AS TASK,
CONCAT(TU.FIRST_NAME, ' ', TU.LAST_NAME) AS RESOURCE_NAME,
TU.BILL_FACTOR,
TC.CLIENT_TYPE, SUM(SPENT) AS SPENT, -- You should specify which table this comes from
SUM(DELIVERED_HOURS) AS DELIVERED_HOURS, -- You should specify which table this comes from
SUM(BILLABLE_HOURS) AS BILLABLE_HOURS, -- You should specify which table this comes from
COMMENTS, -- You should specify which table this comes from
COLOR, -- You should specify which table this comes from
LOCK_COLOR, -- You should specify which table this comes from
UPDATED_BY, -- You should specify which table this comes from
UPDATED_BY_DATE, -- You should specify which table this comes from
TUP.GRP_ID,
TUP.CLIENT_ID,
TUP.PROJ_ID,
TUP.TASK_ID,
TUP.UID,
TUG.GRP_NAME
FROM
TIMESHEET_USER AS TU
LEFT OUTER JOIN
TIMESHEET_USER_PSDB AS TUP
ON TU.USERNAME = TUP.UID
LEFT OUTER JOIN
TIMESHEET_USER_GRP AS TUG
ON TUP.GRP_ID = TUG.GRP_ID
LEFT OUTER JOIN
TIMESHEET_CLIENT AS TC
ON TUP.CLIENT_ID = TC.CLIENT_ID
LEFT OUTER JOIN
TIMESHEET_PROJECT AS TP
ON TUP.PROJ_ID = TP.PROJ_ID
LEFT OUTER JOIN
TIMESHEET_TASK TT
ON TUP.TASK_ID = TT.TASK_ID
WHERE
MONTH = 3 AND -- You should specify which table this comes from
YEAR = 2017 AND -- You should specify which table this comes from
MONTH != '' AND -- You should specify which table this comes from
TUP.CLIENT_ID = 326
GROUP BY
TUP.TASK_ID,
TUP.UID
ORDER BY
TC.CLIENT_TYPE DESC,
TC.ORGANISATION,
TUP.PROJ_ID,
TUP.TASK_ID,
TU.UID,
TUP.TASK_ID;
Doing the EXPLAIN should shed some light on it.
You might see a performance gain by doing the table joins explicitly in the FROM clause instead of inside the WHERE (https://dev.mysql.com/doc/refman/5.7/en/join.html). By doing explicit joins (e.g. LEFT OUTER, etc...) you will not only improve the readability of the query, but may be able to use less expensive joins where needed. This also affects how the query is executed as each clause is executed in a specific order (MySQL query / clause execution order).

Calculate between two statements

I got two statements and I want to calculate their values. Both values have to be calculated 5 - 5 = ( I want to see the answer 0 )
SELECT COUNT(*) AS 'Aantal stoelen geboekt'
FROM Boekingsregel, Vlucht
WHERE Boekingsregel.Vlucht_Vlucht_Id = Vlucht.Vlucht_Id
AND Vlucht_Datum = '2017-04-10';
SELECT min(Vliegtuig_Aantal_Stoelen) AS 'Max aantal stoelen'
FROM Vliegtuig;
While I do not see any relation of the two queries you also should not use an alias with spaces. Make your alias as short as possible but can define what value it holds, but since this is your query you know better than I do.
As for your problem you can combine the two queries into one something like this:
SELECT BR.`Aantal stoelen geboekt` - VT.`Max aantal stoelen` AS TheResult
FROM (SELECT COUNT(*) AS `Aantal stoelen geboekt`
FROM Boekingsregel, Vlucht
WHERE Boekingsregel.Vlucht_Vlucht_Id = Vlucht.Vlucht_Id
AND Vlucht_Datum = '2017-04-10') BR,
(SELECT min(Vliegtuig_Aantal_Stoelen) AS `Max aantal stoelen` FROM Vliegtuig) VT;
NOTE: Not tested I just type it here.
If this is not what you are looking for then maybe you should explain your requirements in greater detail so dhat everyone can understand and will be able to help you.
First, learn to use proper JOIN syntax.
Second, combine these in the FROM clause.
SELECT COUNT(*) AS AantalStoelenGeboekt, vt.MaxAantalStoelen
FROM Boekingsregel b JOIN
Vlucht v
ON b.Vlucht_Vlucht_Id = v.Vlucht_Id CROSS JOIN
(SELECT min(Vliegtuig_Aantal_Stoelen) AS MaxAantalStoelen
FROM Vliegtuig
) vt
WHERE v.Vlucht_Datum = '2017-04-10';
Note: MySQL allows this syntax. Perhaps a cleaner way is to use an aggregation function on MaxAantalStoelen:
SELECT COUNT(*) AS AantalStoelenGeboekt,
MAX(vt.MaxAantalStoelen) as MaxAantalStoelen
FROM Boekingsregel b JOIN
Vlucht v
ON b.Vlucht_Vlucht_Id = v.Vlucht_Id CROSS JOIN
(SELECT min(Vliegtuig_Aantal_Stoelen) AS MaxAantalStoelen
FROM Vliegtuig
) vt
WHERE v.Vlucht_Datum = '2017-04-10';

MySQL Select within another select

I have a query as follows
select
Sum(If(departments.vat, If(weeklytransactions.weekendingdate Between
'2011-01-04' And '2099-12-31', weeklytransactions.takings / 1.2,
If(weeklytransactions.weekendingdate Between '2008-11-30' And '2010-01-01',
weeklytransactions.takings / 1.15, weeklytransactions.takings / 1.175)),
weeklytransactions.takings)) As Total,
weeklytransactions.weekendingdate,......
and another that returns a vat rate as follows
select format(Max(Distinct vat_rates.Vat_Rate),3) From vat_rates Where
vat_rates.Vat_From <= '2011-01-03'
I want to replace the hard coded if statement with the lower query, replacing the date in the lower query with weeklytransactions.weekendingdate.
After Kevin's comments, here is the full query I'm trying to get to work;
Select Max(vat_rates.vat_rate) As r,
If(departments.vat, weeklytransactions.takings / r, weeklytransactions.takings) As Total,
weeklytransactions.weekendingdate,
Week(weeklytransactions.weekendingdate),
round(datediff(weekendingdate, (if(month(weekendingdate)>5,concat(year(weekendingdate),'-06-01'),concat(year(weekendingdate)-1,'-06-01'))))/7,0)+1 as fyweek,
cast((Case When Month(weeklytransactions.weekendingdate) >5 Then Concat(Year(weeklytransactions.weekendingdate), '-',Year(weeklytransactions.weekendingdate) + 1) Else Concat(Year(weeklytransactions.weekendingdate) - 1, '-',Year(weeklytransactions.weekendingdate)) End) as char) As fy,
business_units.business_unit
From departments Inner Join (business_units Inner Join weeklytransactions On business_units.buID = weeklytransactions.businessUnit) On departments.deptid = weeklytransactions.departmentId
Where (vat_rates.vat_from <= weeklytransactions.weekendingdate and business_units.Active = true and business_units.sales=1)
Group By weeklytransactions.weekendingdate, business_units.business_unit Order By fy desc, business_unit, fyweek
Regards
Pete
Assuming I read your question correctly, your problem is about having the result of another SELECT used to be returned by the result of your main query (plus depending on how acquainted you are with SQL, maybe you haven't had the occasion to learn about JOINs?).
You can have subqueries you extract data from within a SELECT, provided you define it within the FROMclause. The following query will work, for example:
SELECT A.a, B.b
FROM A
JOIN (SELECT aggregate(c) FROM C) AS B
Notice that there is no reference to table A within the subquery. Thing is, you cannot just add it like that to the query, as the subquery doesn't know it is a subquery. So the following won't work:
SELECT A.a, B.b
FROM A
JOIN (SELECT aggregate(c) FROM C WHERE C.someValue = A.someValue) AS B
Back to basics. What you want to do here visibly, is to aggregate some data associated to each of the records of another table. For that, you will need merge your SELECT queries and use GROUP BY:
SELECT A.a, aggregate(C.c)
FROM A, C
WHERE C.someValue = A.someValue
GROUP BY A.a
Back to your tables, the following should work:
SELECT w.weekendingdate, FORMAT(MAX(v.Vat_Rate, 3)
FROM weeklytransactions AS w, vat_rates AS v
WHERE v.Vat_From <= w.weekendingdate
GROUP BY w.weekendingdate
Feel free to add and remove fields and conditions as you see fit (I wouldn't be surprised that you'd also want to use a lower bound when filtering the records from vat_rates, since the way I have written it above, for a given weekendingdate, you get records from that week + the weeks before!).
So it looks like my first try did not address the actual problem. With the additional information provided in the comments, as well as the new complete query, let's see how this goes.
We are still missing error messages, but normally the query as written should result in MySQL having the following complaint:
ERROR 1109 (42S02): Unknown table 'vat_rates' in field list
Why? Because the vat_rates table does not appear in the FROM clause, whereas it should. Let's make that more obvious by simplifying the query, removing all references to the business_units table as well as the fields, calculations and order that do not add or remove anything to the problem, leaving us with the following:
SELECT MAX(vat_rates.vat_rate) AS r,
IF(d.vat, w.takings / r, w.takings) AS Total
FROM departments AS d
INNER JOIN weeklytransactions AS w ON w.departmentId = d.deptid
WHERE vat_rates.vat_from <= w.weekendingdate
GROUP BY w.weekendingdate
That cannot work, and will produce the error mentioned above. It looks like there is no FOREIGN ID between the weeklytransactions and vat_rates tables, so we have no choice but to do a CROSS JOIN for the moment, hoping that the condition in the WHERE clause and the aggregate function used to get r are enough to fit the business logic at hand here. The following query should return the expected data instead of an error message (I also remove r since that seems to be an intermediate value judging by the comments that were written):
SELECT IF(d.vat, w.takings / MAX(v.vat_rate), w.takings) AS Total
FROM vat_rates AS v, departments AS d
INNER JOIN weeklytransactions AS w ON w.departmentId = d.deptid
WHERE v.vat_from <= w.weekendingdate
GROUP BY w.weekendingdate
From there, assuming it works, you will only need to put back all the parts I removed to get your final query. I am a tad doubtful about the way the VAT rate is gotten here, but I have no idea what your requirements are in that regard so I leave it up to you to make sure that works as expected.

MS Access query multiple criteria

I am trying to build an access query with multiple criteria. The table to be queried is "tblVendor" which has information about vendor shipment data as shown below:
The second table is "tblSchedule" which has the schedule for each Vendor cutoff date. This table has cutoff dates for data analysis.
For each vendor, I need to select records which have the ShipDate >= CutoffDate. Although not shown in the data here, it may be possible that multiple vendors have same CutoffDate.
For small number of records in "tblCutoffdate", I can write a query which looks like:
SELECT tblVendors.ShipmentId, tblVendors.VendorNumber, tblVendors.VendorName,
tblVendors.Units, tblVendors.ShipDate
FROM tblVendors INNER JOIN tblCutoffDate ON tblVendors.VendorNumber =
tblCutoffDate.VendorNumber
WHERE (((tblVendors.VendorNumber) In (SELECT VendorNumber FROM [tblCutoffDate] WHERE
[tblCutoffDate].[CutoffDate] = #2/1/2014#)) AND ((tblVendors.ShipDate)>=#2/1/2014#)) OR
(((tblVendors.VendorNumber) In (SELECT VendorNumber FROM [tblCutoffDate] WHERE
[tblCutoffDate].[CutoffDate] = #4/1/2014#)) AND ((tblVendors.ShipDate)>=#4/1/2014#));
As desired, the query gives me a result which looks like:
What concerns me now is that I have a lot of records being added to the "tblCutoffDate" which makes it difficult for me to hardcode the dates in the query. Is there a better way to write the above SQL statement without any hardcoding?
You might try something like -- this should handle vendors having no past cutoff,
or those having no future cutoff
"today" needs a suitable conversion to just date w/o time
comparison "=" may go on both, or one, or none Max/Min
"null" may be replaced by 1/1/1900 and 12/31/3999 in Max/Min
SELECT tblvendors.shipmentid,
tblvendors.vendornumber,
tblvendors.vendorname,
tblvendors.units,
tblvendors.shipdate
FROM tblvendors
LEFT JOIN
( SELECT vendornum,
Max( iif cutoffdate < today, cutoffdate, null) as PriorCutoff,
Min( iif cutoffdate >= today, cutoffdate, null) as NextCutoff
FROM tblcutoffdate
GROUP BY vendornum
) as VDates
ON vendornumber = vendornum
WHERE tblvendors.shipdate BETWEEN PriorCutoff and NextCutoff
ORDER BY vendornumber, shipdate, shipmentid
A simpler WHERE clause should give you what you want.
SELECT
v.ShipmentId,
v.VendorNumber,
v.VendorName,
v.Units,
v.ShipDate
FROM
tblVendors AS v
INNER JOIN tblCutoffDate AS cd
ON v.VendorNumber = cd.VendorNumber
WHERE v.ShipDate >= cd.CutoffDate;