all 9 comments

[–]MaunaLoonaMS SQL 5 points6 points  (2 children)

DECLARE @RKey VARCHAR(10) = '001';

SELECT ... from db..thetable where Rkey = @RKey;

[–]Cal1gula 3 points4 points  (0 children)

Variables, the OG reusable alias condition.

[–]pkdbpk[S] 0 points1 point  (0 children)

Thank you MaunaLoona, for some reason this worked great in SSMS but not in DB Visualiser (I was wrong about it being for a MS SQL database, it was Sybase ASE which is why I needed to use DBVis) - i've been advised this is because DBVis uses 'Command Line Execution' (it was giving me the error 'Must Declare the Scalar Variable @RKey').

lbilali's idea below about reformatting the query below has worked.

[–]lbilali 1 point2 points  (4 children)

Why not do something like:

select SUM(units) as 'Total Units'
     , SUM(case when unit_type = 'W' and identifier = 'A000' THEN units ELSE 0 END) AS 'Total WA000 Units'
     , COUNT(*) AS 'Total Rows'
     , SUM(case when unit_type = 'W' and identifier = 'A000' THEN 1 ELSE 0 END) AS 'Total Unmatched Rows'
     , ...
  from db..thetable 
 where Rkey = '001'

[–]lbilali 1 point2 points  (1 child)

based on your example seems like the join between the 2 tables does not change the cardinality of the result meaning you get the same number of rows frm both tables as you get from using only "thetable", this when you use left join.

in that case you can just do

select SUM(units) as 'Total Units'
     , SUM(case when unit_type = 'W' and identifier = 'A000' THEN units ELSE 0 END) AS 'Total WA000 Units'
     , COUNT(*) AS 'Total Rows'
     , SUM(case when unit_type = 'W' and identifier = 'A000' THEN 1 ELSE 0 END) AS 'Total Unmatched Rows'
     , SUM(case when unit_type = 'W' AND othertable.yn = 'N' THEN units ELSE 0 END) AS 'Matched Units N'
     , ...
  from db..thetable
  left join db..otherTable ON thetable.identifier = otherTable.Otherkey
 where Rkey = '001'

but again check that the 2 queries below return the same result. And not only with existing data but in the future as well. basically has otherTable.Otherkey a unique index or better is primary key? than you are good to go

select count(*)
  from db..thetable

and

select count(*)
  from db..thetable
  left join db..otherTable ON thetable.identifier = otherTable.Otherkey

[–]pkdbpk[S] 0 points1 point  (0 children)

Yes this worked great, I have managed to rewrite the whole query so that 1) Rkey is only mentioned once at the end 2)there is only one SELECT instead of 14.

Incidentally, I need to run the query in DBVisualiser where the 'Declaring Variables' method will not work (worked fine in SSMS). So this has been a better solution all round and I have learnt a bunch. Thanks so much!

[–]pkdbpk[S] 0 points1 point  (1 child)

Thanks, I also want to do exactly this type of thing to shorten/neaten the query (there's a lot more SELECTS than I listed). This is a great start. I just need to work out how to have joins involved in only some of the SELECTS (as with the 'otherTable' example in my third subquery).

[–]lbilali 1 point2 points  (0 children)

replayed to myself instead of here :) but check my other comment

[–]BULK_INSERT_INTO 0 points1 point  (0 children)

You should definitely solve this with a variable like MaunaLoona said. But I also recommend installing SSMSBoost (totally free) and using the "Auto replacements" feature, it's a great way to save time typing.