Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I have the code below (obviously with some changed names). I'm trying to find a simpler or more-effective way to create the same results. Any suggestions appreciated. THIS IS FOR FUN. MINE ALREADY WORKS
DECLARE #Scenes TABLE(IncNo numeric(12,0), SceneSquad int, ScDate datetime)
INSERT INTO #Scenes
SELECT A.IncNo, SceneSquad, ScDate
FROM DATABASE as A
LEFT JOIN DATABASE as B
On A.IncNo = B.IncNo
WHERE ScDate BETWEEN '1/1/2021' AND '10/31/2021'
AND A.DivID = ''
DECLARE #Assigned TABLE(IncNo numeric(12,0), Followupsquad int, AssignmentDate
datetime)
INSERT INTO #Assigned
SELECT IncNo, Followupsquad, ASSIGNMENTDATE
FROM DATABASE AS C
LEFT JOIN DATABASE AS D
ON D.IncNo = C.CIncNo
WHERE SOMETHING = 'Y' AND DateRuled BETWEEN '1/1/2021' AND '10/31/2021'
AND D.AssignD = ''
DECLARE #Squads TABLE(Squad int, RN int)
INSERT INTO #Squads
SELECT Squad, ROW_NUMBER () OVER (ORDER BY Squad) AS E
FROM DATABASE
WHERE UnitType = '' AND DivId = '' AND SquadStatus = 'A'
ORDER BY Squad
DECLARE #MSS TABLE (SqdNo int, SOMETHING int, SOMETHINGELSE int)
DECLARE #DATE datetime, #X int, #SQ int, #M int, #S int
SET #DATE = 1/1/2021 - 10/31/2021
SET #X = 1
WHILE #X <= (SELECT MAX(RN) FROM #Squads)
BEGIN
SET #SQ = (SELECT Squad FROM #Squads WHERE RN = #X)
SET #S = (SELECT COUNT(*) FROM #Scenes WHERE SceneSquad = #SQ)
SET #M = (SELECT COUNT(*) FROM #Assigned WHERE Followupsquad = #SQ)
INSERT INTO #MSS VALUES (#SQ, #M, #S)
SET #X = #X + 1
END
SELECT SqdNo , SOMETHING , SOMETHINGELSE
FROM #MSS
You can use Common Table Expressions (CTEs) in place of table variables, and this can help reduce and often speed up the code, such that we can get everything down to one (large) query with no loop:
WITH Scenes As (
SELECT A.IncNo, SceneSquad, ScDate
FROM DATABASE as A
LEFT JOIN DATABASE as B
On A.IncNo = B.IncNo
WHERE ScDate BETWEEN '20210101' AND '20211031'
AND A.DivID = ''
)
, Assigned As (
SELECT IncNo, Followupsquad, ASSIGNMENTDATE
FROM DATABASE AS C
LEFT JOIN DATABASE AS D
ON D.IncNo = C.CIncNo
WHERE SOMETHING = 'Y' AND DateRuled BETWEEN '20210101' AND '20211031'
AND D.AssignD = ''
)
, Squads As (
-- Removed row_number(), since it only seemed to be used for an index later
SELECT Squad
FROM DATABASE
WHERE UnitType = '' AND DivId = '' AND SquadStatus = 'A'
-- also moved the ORDER BY to the final SELECT
)
SELECT sq.Squad As SqdNo
, (SELECT COUNT(*) FROM Scenes WHERE SceneSquad = sq.Squad) As Something
, (SELECT COUNT(*) FROM Assigned WHERE FollowupSquad = sq.Squad) As SomethingElse
FROM Squads sq
ORDER BY SqdNo
Each INSERT/SELECT for a table variable in the original question is instead now a CTE, and we then roll up the loop into correlated sub-queries (nested SELECTS).
The weakness here is it re-runs the Scenes and Assigned CTEs for each row, where the table variables effectively let the server cache those query results. The strength is the original table indexes are still available.
But that's just step one. We can improve this further to get the best of both worlds, using GROUP BY and JOINs. The trick is if we tried to use GROUP BY to get counts from both additional queries this in the same step, the row counts would be multiplied. However, we can use another CTE to force them to happen sequentially and get the right answers. If I skip past the prior CTEs to save space, it looks like this:
, SquadPlusSceneCounts As (
SELECT sq.Squad, COUNT(*) As Something
FROM Squads sq
LEFT JOIN Scenes sc ON sc.SceneSquad = sq.Squad
GROUP BY sq.Squad
)
SELECT sq.Squad As SqdNo, sq.Something, COUNT(*) As SomethingElse
FROM SquadPlusSceneCounts sq
LEFT JOIN Assigned a ON a.FollowupSquaud = sq.Squad
GROUP BY sq.Squad, sq.Something
ORDER BY sq.Squad
This will likely perform much better. The code is also simpler because we no longer need table variables or INSERT statements and I was able to remove the row_number() and loop. Finally, we preserved the intermediate logic and naming, so it will still be maintainable in a similar way (you can run most of the CTEs independently to verify them).
All of this was a straight-up translation of the code from the question. Better understanding of the data might also allow for logical improvements with further performance benefits and simpler code.
However, there's still the matter of the #Date variable; it's not at all clear what you were doing there. SET #DATE = 1/1/2021 - 10/31/2021 makes no sense, and it wasn't used later. You may need an additional CTE that generates dates based on an initial value on the fly, that you can then also JOIN to. There are a number of ways to do this, and since I don't know it's even needed I'll leave it as an exercise.
One more thing. In many cases where CTEs are used, it's possible to wrap them up into nested SELECT queries instead. I tend to avoid this, because it's harder to maintain (you lose a level of naming or mnemonics and you can't run the CTE by itself as easily for testing/maintenance), but you can sometimes get a small performance benefit this way.
So for completeness, view the monstrosity below:
SELECT sq.Squad As SqdNo, sq.Something, COUNT(*) As SomethingElse
FROM (
SELECT sq0.Squad, COUNT(*) As Something
FROM (
SELECT Squad
FROM DATABASE
WHERE UnitType = '' AND DivId = '' AND SquadStatus = 'A'
) sq0
LEFT JOIN (
SELECT A.IncNo, SceneSquad, ScDate
FROM DATABASE as A
LEFT JOIN DATABASE as B On A.IncNo = B.IncNo
WHERE ScDate BETWEEN '20210101' AND '20211031'
AND A.DivID = ''
) sc ON sc.SceneSquad = sq0.Squad
GROUP BY sq0.Squad
) sq
LEFT JOIN (
SELECT IncNo, Followupsquad, ASSIGNMENTDATE
FROM DATABASE AS C
LEFT JOIN DATABASE AS D
ON D.IncNo = C.CIncNo
WHERE SOMETHING = 'Y' AND DateRuled BETWEEN '20210101' AND '20211031'
AND D.AssignD = ''
) a ON a.FollowupSquaud = sq.Squad
GROUP BY sq.Squad, sq.Something
ORDER BY sq.Squad
But again, don't do this last part unless you have to.
As a complete aside, as bad as this is, it demonstrates why I'm not a fan or ORMs. As ugly as this code is, I don't want to think about trying to replicate it via ORM syntax. And even if you could, you'd still need to know how to write code like this in order be sure it's right... at which point you may just as well devote your time to learning good SQL.
Related
I have a datamodels which consists of 'Claims' which (to make things simple for stackoverflow) only has an OpenAmount field. There are two other tables, 'ClaimCoupling' and 'ClaimEntryReference'.
The ClaimCoupling table directly references back to the Claim table and the ClaimEntryReference is effectively the booking of a received amount that can be booked over multiple claims (See ClaimEntry_ID). See this diagram;
For simplicity I've removed all amounts as that's not what I am currently struggling with.
What I want is a query that will start # the Claim table, and fetches all a claim with an OpenAmount which is <> 0. However I want to be able to print out an accurate report of how this OpenAmount came to be, which means I'll need to also print out any Claims coupled to this claim. To make it even more interesting the same thing applies to the bookings, if a booking was made on claim X and claim Y and only X has an open amount I want to fetch both X and Y so I can then show the payment which was booked as a whole.
I've attempted to do this with a recursive CTE but this (rightfully) blows up on the circulair references. I figured I'd fix that with a simple where statement where I would say only recursively add records which are not yet part of CTE but this is not allowed....
WITH coupledClaims AS (
--Get all unique combinations
SELECT cc.SubstractedFromClaim_ID AS Claim_ID,
cc.AddedToClaim_ID AS Linked_Claim_ID FROM dbo.ClaimCoupling cc
UNION
SELECT cc.AddedToClaim_ID AS Claim_ID,
cc.SubstractedFromClaim_ID AS Linked_Claim_ID FROM dbo.ClaimCoupling cc
),
MyClaims as
(
SELECT * FROM Claim WHERE OpenAmount <> 0
UNION ALL
SELECT c.* FROM coupledClaims JOIN MyClaims mc ON coupledClaims.claim_id = mc.ID JOIN claim c ON c.ID = coupledClaims.linked_Claim_ID
WHERE c.ID NOT IN (SELECT ID FROM MyClaims)
)
SELECT * FROM MyClaims
After fiddling around with that for way too long I decided I'd do it with an actual loop... ##Rowcount and simply manually add them to a table variable but as I was writing this solution (which I'm sure I can get to work) I figured I'd ask here first because I don't like writing loops in TSQL as I always feel it's ugly and inefficient.
See the following sql Fiddle for the data models and some test data (I commented out the recursive part as otherwise I was not allowed to create a link);
http://sqlfiddle.com/#!6/129ad5/7/0
I'm hoping someone here will have a great way of handling this problem (likely I'm doing something wrong with the recursive CTE). For completion this is done on MS SQL 2016.
So here is what I've learned and done so far. Thanks to the comment of habo which refers to the following question; Infinite loop in CTE when parsing self-referencing table
Firstly I decided to at least 'solve' my problem and wrote some manual recursion, this solves my problem but is not as 'pretty' as the CTE solution which I was hoping/thinking would be easier to read as well as out perform the manual recursion solution.
Manual Recursion
/****************************/
/* CLAIMS AND PAYMENT LOGIC */
/****************************/
DECLARE #rows as INT = 0
DECLARE #relevantClaimIds as Table(
Debtor_ID INT,
Claim_ID int
)
SET NOCOUNT ON
--Get anchor condition
INSERT INTO #relevantClaimIds (Debtor_ID, Claim_ID)
select Debtor_ID, ID
from Claim c
WHERE OpenAmount <> 0
--Do recursion
WHILE #rows <> (SELECT COUNT(*) FROM #relevantClaimIds)
BEGIN
set #rows = (SELECT COUNT(*) FROM #relevantClaimIds)
--Subtracted
INSERT #relevantClaimIds (Debtor_ID, Claim_ID)
SELECT DISTINCT c.Debtor_ID, c.id
FROM claim c
inner join claimcoupling cc on cc.SubstractedFromClaim_ID = c.ID
JOIN #relevantClaimIds rci on rci.Claim_ID = cc.AddedToClaim_ID
--might be multiple paths to this recursion so eliminate duplicates
left join #relevantClaimIds dup on dup.Claim_ID = c.id
WHERE dup.Claim_ID is null
--Added
INSERT #relevantClaimIds (Debtor_ID, Claim_ID)
SELECT DISTINCT c.Debtor_ID, c.id
FROM claim c
inner join claimcoupling cc on cc.AddedToClaim_ID = c.ID
JOIN #relevantClaimIds rci on rci.Claim_ID = cc.SubstractedFromClaim_ID
--might be multiple paths to this recursion so eliminate duplicates
left join #relevantClaimIds dup on dup.Claim_ID = c.id
WHERE dup.Claim_ID is null
--Payments
INSERT #relevantClaimIds (Debtor_ID, Claim_ID)
SELECT DISTINCT c.Debtor_ID, c.id
FROM #relevantClaimIds f
join ClaimEntryReference cer on f.Claim_ID = cer.Claim_ID
JOIN ClaimEntryReference cer_linked on cer.ClaimEntry_ID = cer_linked.ClaimEntry_ID AND cer.ID <> cer_linked.ID
JOIN Claim c on c.ID = cer_linked.Claim_ID
--might be multiple paths to this recursion so eliminate duplicates
left join #relevantClaimIds dup on dup.Claim_ID = c.id
WHERE dup.Claim_ID is null
END
Then after I received and read the comment I decided to try the CTE solution which looks like this;
CTE Recursion
with Tree as
(
select Debtor_ID, ID AS Claim_ID, CAST(ID AS VARCHAR(MAX)) AS levels
from Claim c
WHERE OpenAmount <> 0
UNION ALL
SELECT c.Debtor_ID, c.id, t.levels + ',' + CAST(c.ID AS VARCHAR(MAX)) AS levels
FROM claim c
inner join claimcoupling cc on cc.SubstractedFromClaim_ID = c.ID
JOIN Tree t on t.Claim_ID = cc.AddedToClaim_ID
WHERE (','+T.levels+',' not like '%,'+cast(c.ID as varchar(max))+',%')
UNION ALL
SELECT c.Debtor_ID, c.id, t.levels + ',' + CAST(c.ID AS VARCHAR(MAX)) AS levels
FROM claim c
inner join claimcoupling cc on cc.AddedToClaim_ID = c.ID
JOIN Tree t on t.Claim_ID = cc.SubstractedFromClaim_ID
WHERE (','+T.levels+',' not like '%,'+cast(c.ID as varchar(max))+',%')
UNION ALL
SELECT c.Debtor_ID, c.id, t.levels + ',' + CAST(c.ID AS VARCHAR(MAX)) AS levels
FROM Tree t
join ClaimEntryReference cer on t.Claim_ID = cer.Claim_ID
JOIN ClaimEntryReference cer_linked on cer.ClaimEntry_ID = cer_linked.ClaimEntry_ID AND cer.ID <> cer_linked.ID
JOIN Claim c on c.ID = cer_linked.Claim_ID
WHERE (','+T.levels+',' not like '%,'+cast(c.ID as varchar(max))+',%')
)
select DISTINCT Tree.Debtor_ID, Tree.Claim_ID
from Tree
This solution is indeed a lot 'shorter' and easier on the eyes but does it actually perform better?
Performance differences
Manual; CPU 16, Reads 1793, Duration 13
CTE; CPU 47, Reads 4001, Duration 48
Conclusion
Not sure if it's due to the varchar cast that is required in the CTE solution or that it has to do one extra iteration before completing it's recursion but it actually requires more resources on all fronts than the manual recursion.
In the end it is possible with CTE however looks aren't everything (thank god ;-)) performance wise sticking with the manual recursion seems like a better route.
I have a simple SQL table containing some values, for example:
id | value (table 'values')
----------
0 | 4
1 | 7
2 | 9
I want to iterate over these values, and use them in a query like so:
SELECT value[0], x1
FROM (some subquery where value[0] is used)
UNION
SELECT value[1], x2
FROM (some subquery where value[1] is used)
...
etc
In order to get a result set like this:
4 | x1
7 | x2
9 | x3
It has to be in SQL as it will actually represent a database view. Of course the real query is a lot more complicated, but I tried to simplify the question while keeping the essence as much as possible.
I think I have to select from values and join the subquery, but as the value should be used in the subquery I'm lost on how to accomplish this.
Edit: I oversimplified my question; in reality I want to have 2 rows from the subquery and not only one.
Edit 2: As suggested I'm posting the real query. I simplified it a bit to make it clearer, but it's a working query and the problem is there. Note that I have hardcoded the value '2' in this query two times. I want to replace that with values from a different table, in the example table above I would want a result set of the combined results of this query with 4, 7 and 9 as values instead of the currently hardcoded 2.
SELECT x.fantasycoach_id, SUM(round_points)
FROM (
SELECT DISTINCT fc.id AS fantasycoach_id,
ffv.formation_id AS formation_id,
fpc.round_sequence AS round_sequence,
round_points,
fpc.fantasyplayer_id
FROM fantasyworld_FantasyCoach AS fc
LEFT JOIN fantasyworld_fantasyformation AS ff ON ff.id = (
SELECT MAX(fantasyworld_fantasyformationvalidity.formation_id)
FROM fantasyworld_fantasyformationvalidity
LEFT JOIN realworld_round AS _rr ON _rr.id = round_id
LEFT JOIN fantasyworld_fantasyformation AS _ff ON _ff.id = formation_id
WHERE is_valid = TRUE
AND _ff.coach_id = fc.id
AND _rr.sequence <= 2 /* HARDCODED USE OF VALUE */
)
LEFT JOIN fantasyworld_FantasyFormationPlayer AS ffp
ON ffp.formation_id = ff.id
LEFT JOIN dbcache_fantasyplayercache AS fpc
ON ffp.player_id = fpc.fantasyplayer_id
AND fpc.round_sequence = 2 /* HARDCODED USE OF VALUE */
LEFT JOIN fantasyworld_fantasyformationvalidity AS ffv
ON ffv.formation_id = ff.id
) x
GROUP BY fantasycoach_id
Edit 3: I'm using PostgreSQL.
SQL works with tables as a whole, which basically involves set operations. There is no explicit iteration, and generally no need for any. In particular, the most straightforward implementation of what you described would be this:
SELECT value, (some subquery where value is used) AS x
FROM values
Do note, however, that a correlated subquery such as that is very hard on query performance. Depending on the details of what you're trying to do, it may well be possible to structure it around a simple join, an uncorrelated subquery, or a similar, better-performing alternative.
Update:
In view of the update to the question indicating that the subquery is expected to yield multiple rows for each value in table values, contrary to the example results, it seems a better approach would be to just rewrite the subquery as the main query. If it does not already do so (and maybe even if it does) then it would join table values as another base table.
Update 2:
Given the real query now presented, this is how the values from table values could be incorporated into it:
SELECT x.fantasycoach_id, SUM(round_points) FROM
(
SELECT DISTINCT
fc.id AS fantasycoach_id,
ffv.formation_id AS formation_id,
fpc.round_sequence AS round_sequence,
round_points,
fpc.fantasyplayer_id
FROM fantasyworld_FantasyCoach AS fc
-- one row for each combination of coach and value:
CROSS JOIN values
LEFT JOIN fantasyworld_fantasyformation AS ff
ON ff.id = (
SELECT MAX(fantasyworld_fantasyformationvalidity.formation_id)
FROM fantasyworld_fantasyformationvalidity
LEFT JOIN realworld_round AS _rr
ON _rr.id = round_id
LEFT JOIN fantasyworld_fantasyformation AS _ff
ON _ff.id = formation_id
WHERE is_valid = TRUE
AND _ff.coach_id = fc.id
-- use the value obtained from values:
AND _rr.sequence <= values.value
)
LEFT JOIN fantasyworld_FantasyFormationPlayer AS ffp
ON ffp.formation_id = ff.id
LEFT JOIN dbcache_fantasyplayercache AS fpc
ON ffp.player_id = fpc.fantasyplayer_id
-- use the value obtained from values again:
AND fpc.round_sequence = values.value
LEFT JOIN fantasyworld_fantasyformationvalidity AS ffv
ON ffv.formation_id = ff.id
) x
GROUP BY fantasycoach_id
Note in particular the CROSS JOIN which forms the cross product of two tables; this is the same thing as an INNER JOIN without any join predicate, and it can be written that way if desired.
The overall query could be at least a bit simplified, but I do not do so because it is a working example rather than an actual production query, so it is unclear what other changes would translate to the actual application.
In the example I create two tables. See how outer table have an alias you use in the inner select?
SQL Fiddle Demo
SELECT T.[value], (SELECT [property] FROM Table2 P WHERE P.[value] = T.[value])
FROM Table1 T
This is a better way for performance
SELECT T.[value], P.[property]
FROM Table1 T
INNER JOIN Table2 p
on P.[value] = T.[value];
Table 2 can be a QUERY instead of a real table
Third Option
Using a cte to calculate your values and then join back to the main table. This way you have the subquery logic separated from your final query.
WITH cte AS (
SELECT
T.[value],
T.[value] * T.[value] as property
FROM Table1 T
)
SELECT T.[value], C.[property]
FROM Table1 T
INNER JOIN cte C
on T.[value] = C.[value];
It might be helpful to extract the computation to a function that is called in the SELECT clause and is executed for each row of the result set
Here's the documentation for CREATE FUNCTION for SQL Server. It's probably similar to whatever database system you're using, and if not you can easily Google for it.
Here's an example of creating a function and using it in a query:
CREATE FUNCTION DoComputation(#parameter1 int)
RETURNS int
AS
BEGIN
-- Do some calculations here and return the function result.
-- This example returns the value of #parameter1 squared.
-- You can add additional parameters to the function definition if needed
DECLARE #Result int
SET #Result = #parameter1 * #parameter1
RETURN #Result
END
Here is an example of using the example function above in a query.
SELECT v.value, DoComputation(v.value) as ComputedValue
FROM [Values] v
ORDER BY value
Suppose I have an sql query like the following (I realize this query could be written better, just bear with me):
SELECT aT.NAME
FROM anothertable aT,
( SELECT ts.slot_id,
tgm.trans_id,
tagm.agent_id
FROM slots ts,
transactions tgm,
agents tagm
WHERE ts.slot_id = (12345, 678910)
and ts.slot_id = tagm.slot_id
AND ts.slot_id = tgm.slot_id) INNER
WHERE INNER.trans_id = aT.trans_id
AND INNER.agent_id = aT.trans_id
Now suppose that I need to break up this query into two parts...in the first I'll execute the inner query, do some processing on the results in code, and then pass back a reduced set to the outer part of the query. The question is, is there an easy way to emulate an inner table in sql?
For instance, if the results of the inner query returned 5 rows but my program deems to only need two of those rows, how can I write sql that will do what I am trying to do below? Is there a way, in sql, to declare a table for in memory in query use?
SELECT
at.Name
FROM
anotherTable aT,
(SLOT_ID, TRANS_ID, AGENT_ID
-------------------------
230743, 3270893, 2307203
078490, 230897, 237021) inner
WHERE
inner.trans_id = at.trans_id
AND INNER.agent_id = aT.trans_id
Just use a subquery:
SELECT at.Name
FROM anotherTable aT JOIN
(select 230743 as SLOT_ID, 3270893 as TRANS_ID, 2307203 as AGENT_ID from dual
select 078490, 230897, 237021 from dual
) i
on i.trans_id = at.trans_id AND i.agent_id = aT.trans_id;
Most systems will let you define a TEMP TABLE or TABLE VARIABLE: https://www.simple-talk.com/sql/t-sql-programming/temporary-tables-in-sql-server/
CREATE TABLE #temp (
SLOT_ID INT,
TRANS_ID INT,
AGENT_ID INT
);
INSERT INTO #temp(SLOT_ID, TRANS_ID, AGENT_ID)
(--inner query goes here)
--do your main query, then:
DROP TABLE #temp
IN MS SQL Server (not sure about other systems), you could possibly use a Common Table Expression (CTE): https://technet.microsoft.com/en-us/library/ms190766%28v=sql.105%29.aspx
WITH inner AS (
--inner query goes here
)
--main select goes here
Personally, since I generally work with MSSQL Server, I use CTE's quite a bit, as they can be created "on the fly", and can be a big help in organizing more complex queries.
The subquery method worked. Since this is Oracle, the syntax turned out to be:
SELECT aT.Name
FROM anotherTable aT,
(select 1907945 as SLOT_ID, 2732985 as TRANS_ID, 40157 as AGENT_ID FROM DUAL
union
select 1907945, 2732985, 40187 FROM DUAL
) inner
WHERE
inner.trans_id = aT.trans_id AND INNER.agent_id = aT.trans_id;
I have a query which involves 2 tables 'Coupons' and 'CouponUsedLog' in SQL Server, the query below will obtain some information from these 2 tables for statistics study use. Somehow I feel that while my query works and returns me the desired results, I feel that I can be written in a more efficient way, can someone please advice if there's a better way to rewrite this? Am I using too many unnecessary variables and joins? Thanks.
DECLARE #CouponISSUED int=null
DECLARE #CouponUSED int=null
DECLARE #CouponAVAILABLE int=null
DECLARE #CouponEXPIRED int=null
DECLARE #CouponLastUsed Date=null
--Total CouponIssued
SET #CouponISSUED =
(
select count(*)
from Coupon C Left Join
couponusedlog CU on C.autoid = CU.Coupon_AutoID
where C.VoidedBy is null and
C.VoidedOn is null and
DeletedBy is null and
DeletedOn is null and
Card_AutoID in (Select AutoID
from Card
where MemberID = 'Mem001')
)
--Total CouponUsed
SET #CouponUSED =
(
select count(*)
from couponusedlog CU Left Join
Coupon C on CU.Coupon_AutoID = V.autoid
where CU.VoidedBy is null and
CU.VoidedOn is null and
C.Card_AutoID in (select AutoID
from Card
where MemberID = 'Mem001')
)
SET #CouponAVAILABLE = #CouponISSUED - #CouponUSED
--Expired Coupons
SET #CouponEXPIRED =
(
select Count(*)
from Coupon C Left Join
couponusedlog CU on C.autoid = CU.Coupon_AutoID
where C.VoidedBy is null and
C.VoidedOn is null and
deletedBy is null and
deletedOn is null and
Card_AutoID in (select AutoID
from Card
where MemberID = 'Mem002') and
CONVERT (date, getdate()) > C.expirydate
)
--Last Used On
SET #CouponLastUsed =
(
select CONVERT(varchar(10),
Max(VU.AddedOn), 103) AS [DD/MM/YYYY]
from couponusedlog CU Left Join
coupon C on CU.Coupon_AutoID = C.autoid
where CU.voidedBy is null and
CU.voidedOn is null and
C.Card_AutoID in (select AutoID
from Card
where MemberID = 'Mem002')
)
Select #CouponISSUED As Coupon_Issued,
#CouponUSED As Coupon_Used,
#CouponAVAILABLE As Coupon_Available,
#CouponEXPIRED As Coupon_Expired,
#CouponLastUsed As Last_Coupon_UsedOn
In general its better to do things in a single query if you you're just looking for counts of things particularly against nearly the same data set then in four separate queries.
This query combines what you need into a single query by converting your WHERE Clauses into SUMS of CASE statements. The MAX of the date is just a normal thing you can do when you're doing a count or a sum.
SELECT COUNT(*) couponissued,
SUM(CASE
WHEN deletedby IS NULL
AND deletedon IS NULL THEN 1
ELSE 0
END) AS couponused,
SUM(CASE
WHEN deletedby IS NULL
AND deletedon IS NULL
AND Getdate() > c.expirydate THEN 1
ELSE 0
END) AS couponex,
MAX(vu.addedon) CouponEXPIRED
FROM [couponusedlog] cu
LEFT JOIN [Coupon] c
ON ( cu.coupon_autoid = v.autoid )
WHERE cu.voidedby IS NULL
AND cu.voidedon IS NULL
AND ( c.card_autoid IN (SELECT [AutoID]
FROM [Card]
WHERE memberid = 'Mem001') )
You can then convert that into a Common Table Expression to do your subtraction and formatting
Are you asking this question out of a proactive desire to be as effecient as possible, or because of an actual performance issue you would like to correct? You can make this more effecient at the cost of having code that is harder to manage. If the performance is okay right now I would highly recommend you leave it because the next person to come along will be able to understand it just fine. If you make one huge effecient but garbled sql statement out of it then when you or anyone else wants to update something about it it's going to take you 3 times longer as you try to re-figure out what the heck you were thinking when you wrote it.
This is probably a stupid question to most of you but I was wondering whether you can rename a column using the 'AS' keyword and a select statement?
Here is my SQL:
Select Main.EmpId
, Associate_List.costCenter, Assignments.Area
, Main.Assignments_1 AS (
Select Assignment_Name
from Assignments
where Assignment_Number = 1
and Assignments.Area = '#Someparemeter'
)
from associate_list
, main
, APU_CC
, Assignments
where Main.Empid = Associate_List.Empid
and substring(Associate_List.CostCenter,1,4) = APU_CC.CostCentre
The only part of SQL I'm wondering about is:
Main.Assignments_1 AS (
Select Assignment_Name
from Assignments
where Assignment_Number = 1
and Assignments.Area = '#Someparemeter'
)
Is this possible or am I talking jibberish or is this just a stupid thing to do?
Many Thanks
The part after as is not a value but a variable name; the SQL database will use it to reference the value of the result set so you can compare/sort/filter them. Therefore this is not possible.
If you must do this, you must read the documentation of your database how to build dynamic queries. But I suggest against it because it will cause strange errors that will be very hard to debug.
I'm not quite sure what you are driving at.
If there is a column in the Main table called Assignments_1, you can rename it in your query (to give a different header at the top of the output or for some other reason) like this...
SELECT MAIN.ASSIGNMENT_1 AS MY_NEW_NAME
FROM etc.
If you want a derived table in your query you name it like this...
SELECT MAIN.ASSIGNMENT_1,
SELECT *
FROM (SELECT THIS, THAT, THE_OTHER
FROM SOME_TABLE) AS DERIVED_TABLE
FROM etc.
If you didn't want either of those things, please clarify and we'll try to help.
In SQL Server, you can assign an alias to a column with AS like so:
...
ColumnName AS ColumnAlias,
...
And you can do it for a "sub-select" like you have in your example. (I wouldn't write the query quite like that--I'd run the subquery first and then plop the result into the second query like so:
DECLARE #Assignment_Name varca(100) -- or however long
SELECT #Assignment_Name = Assignment_Name
from Assignments
where Assignment_Number = 1
and Assignments.Area = #Someparemeter
SELECT
...
#Assignment_Name = Assignment_Name,
...
But you can do this:
Select m.EmpId, l.costCenter,
(Select Area From Assignments a
Where Assignment_Number = 1
And Area = '#Someparemeter') As Area,
(Select Assignment_Name From Assignments a
Where Assignment_Number = 1
And Area = '#Someparemeter') As Assignments_1
From associate_list l
Join main m On m.Empid = l.Empid
Join APU_CC c On c.CostCentre = substring(l.CostCenter,1,4)
or this:
Select m.EmpId, l.costCenter, Asgn.Area,
Asgn.Assignment_Name as Assignments_1
From associate_list l
Join main m On m.Empid = l.Empid
Join APU_CC c On c.CostCentre = substring(l.CostCenter,1,4)
Cross Join (Select Assignment_Name From Assignments a
Where Assignment_Number = 1
And Area = '#Someparemeter') as Asgn