How to use case statement to declare a local variable for a subsequent subquery? - sql

Is it possible to declare a local variable depending on the case and then fire a common subquery?
Pseudo code:
SELECT
CASE
WHEN TABLE.TYPE = 'STUDENT' variable = "UNIVERSITY"
WHEN TABLE.TYPE = 'EMPLOYEE' variable = "EMPLOYER"
(Some big query here it has a common joins / groupings but the variable changes)
END AS NAME
FROM TABLE
Looking for a SQL Server solution.
Scenario is like this - I have a query that lists some member information . Now I need to get some addendum data to an existing query - I mean result set won't change . I can actually join but then I have to filter out many things - Does that make sense ? Imagine I was display all the members in the table and someone asks me to show the univeristy name or the employer name . And the biggest problem is a member can have more than one university he attended - same for employer . Isn't joins really bad on performance in this case ? Since there are many one to many stuff . Also please note I finally display only 10 records as part of pagination so I thought I should do a case statement .

DECLARE #variable varchar(10)
SELECT #variable =
CASE
WHEN t.TYPE = 'STUDENT' THEN 'UNIVERSITY'
WHEN t.TYPE = 'EMPLOYEE' THEN 'EMPLOYER'
ELSE 'Undefined'
END
FROM TableName t
MSDN - SELECT #local_variable
If the SELECT statement returns more than one value, the variable is
assigned the last value returned.

Related

SQL Query to get all the data from tables when there is no parameter

I am trying to get the whole data when there are no filters selected. I have made an array that contains the selections. In case there are no selections then there will be just '' , i.e. no characters but not null.
SELECT * FROM Skills WHERE person IN ('Technology', 'Drilling');
For example - In this query it will return all required - filtered data. So my array contains Technology and Drilling. In case there is nothing selected by the user as a filter then the query would look like:
SELECT * FROM Skills WHERE person IN ('');
In this case the table is returning nothing in SQL Server. I want it to return everything from the table without any filters.
I would really like to get some help here and maybe some resources that might help me achieve the required thing.
The array is being filled in javascript.
It seems really strange to have a column called person compared to values like "Drilling". But you would do something like:
SELECT *
FROM Skills
WHERE person IN (<whatever>) OR <whatever> = '';
Often NULL is used to mean everything, so that would be:
WHERE person IN (<whatever>) OR <whatever> IS NULL;
And "whatever" might be a delimited string, so this might look like:
WHERE person IN (SELECT s.value FROM string_split(#params) s) OR
#params IS NULL;

UPDATE or SET from a SELECT query?

I have a database of information that should only be used one time. To grab the information to be exported into a .CSV file for use. I would use the following query, we also have the column USED which should be set to YES after the data has been exported (so it not re-used).
SELECT TOP(40000)
ms.website AS Website,
ms.company AS COMPANY,
ms.address AS [ADDRESS],
ms.city AS CITY,
ms.state AS [STATE],
ms.zip AS ZIP,
ms.phone AS PHONE
FROM
[QUETABLE] as ms
WHERE
DEAD != 'YES' AND USED != 'YES';
And then I figured I would use this query to update the column "USED" so that if the same query was run again, only new information would be exported:
UPDATE TOP(40000) QUETABLE
SET USED = 'YES'
WHERE USED = 'NULL' AND DEAD != 'YES';
However, while a lot of the data was the same, for some reason, not all 40,0000 columns were matching, meaning un-used data would be marked as used (and vice versa), the USED column is set to NULL before its used (not IS NULL, but written "null').
How could I run the top query, but at the same time also set USED to "YES" so the information is identical? So, in broken SQL it would be like:
SELECT TOP(40000)
ms.website AS Website,
ms.company AS COMPANY,
ms.address AS [ADDRESS],
ms.city AS CITY,
ms.state AS [STATE],
ms.zip AS ZIP,
ms.phone AS PHONE
FROM [QUETABLE] as ms
WHERE DEAD != 'YES' AND USED != 'YES' THEN SET USED = 'YES';
But of course, that wouldn't work, I'm not sure how to accomplish this!
Thank you
You are using TOP with no ORDER BY. This returns an arbitrary set of rows. There is no reason to think that the rows returned on two different runs of the same query would return the same rows, much less an UPDATE and a SELECT.
I would suggest that you use the OUTPUT clause and do the work in the opposite order:
DECLARE #t TABLE ( . . . ); -- fill in the columns
UPDATE TOP(40000) QUETABLE
SET USED = 'YES'
OUTPUT inserted.* INTO #t;
WHERE USED = 'NULL' AND DEAD <> 'YES'
Now you can "export" the ones that were just set to DONE.

SQL use intermediate results

I have a column with numbers (float) that I would like to categorize and store a category as integer and as label (string). For now assume that the category is simply defined by the FLOOR(x).
This works:
SELECT salary,
FLOOR(salary) AS category_integer,
CASE WHEN FLOOR(salary) = 0
THEN 'foo'
ELSE 'bar'
END AS category_label
FROM test01
but I was wondering if I could use the intermediate variable 'category_integer' defined in the beginning of my query in a later part, something like this:
SELECT salary,
FLOOR(salary) AS category_integer,
CASE WHEN category_integer = 0
THEN 'foo'
ELSE 'bar'
END AS category_label
FROM test01
but this is apparently not how SQL works. I've looked into Common table Expressions but got lost there. Is there a way to reuse intermediate variables in an SQL expression?
SQL Fiddle
I must have missed this but I couldn't find related questions so far.
You may resort to common table expressions - basically a query that produces a labelled result set you can refer to in subsequent queries.
Adapted to your example:
with cte as (
select salary
, floor(salary) as category_integer
from test01
)
SELECT salary
, category_integer
, CASE WHEN category_integer = 0
THEN 'foo'
ELSE 'bar'
END AS category_label
FROM cte
;
Consult the reference for more details: CTE / WITH in pgSQL 9.6.
See it at work in SQL fiddle.
There are pre- and post-selection operations. For example order by and group by are post-selection instructions, distinct for example filters out duplicate results during the selection proces itself and as such duplicate results do not even enter the result set to be ordered or grouped.
When you use AS, you are telling PostgreSQL to take the result and put it in a column named category_integer in the output. You are not actually making a variable here that's available during query execution, as the result is only available after the query executes. As such, you can only do this with subselects where you have the result available as a virtual table in itself, where category_integer is a column in a table rather than a variable.
SELECT category_integer,
CASE WHEN category_integer = 0
THEN 'foo'
ELSE 'bar'
END AS category_label
FROM (SELECT FLOOR(0) AS category_integer FROM test01) AS test02
https://www.postgresql.org/docs/current/static/queries-select-lists.html
https://www.postgresql.org/docs/current/static/queries-table-expressions.html#QUERIES-TABLE-ALIASES

SQL Select statement - base results upon the value of an alias within statement

I am trying to write a Select statement (comprised of around 20 different joined aliases) that will only return results if the value of one of the aliases created within the same statement equals a certain value.
I'm very green with SQL at this point and therefore don't really know how to phrase this dilemma properly to find the answer elsewhere.
Current code for element being assigned an alias of "cmp_freq":
ISNULL((SELECT GroupValue FROM ClientGroup WHERE ClientKey = c.ClientKey AND GroupCode = 'CMP-FREQ'),'PLEASE UPDATE FIELD') AS cmp_freq
Essentially, I only want results returned for the entire statement where the value of cmp_freq is "30". Is there any way to reference this alias in the where clause of the statement as a whole in order to accomplish this?
There are several ways to accomplish your goal. One way would be to wrap your query in a SELECT and use a WHERE clause, like so:
SELECT i.cmp_freq
FROM (
/* Your existing query */
SELECT
ISNULL((SELECT GroupValue FROM ClientGroup WHERE ClientKey = c.ClientKey AND GroupCode = 'CMP-FREQ'),'PLEASE UPDATE FIELD') AS cmp_freq
FROM MyTable c
) i
WHERE i.cmp_freq = 30
It's difficult to offer other options as there is not enough information in your question.

T-SQL Skip cte Table and Union when Variable Is Null

I've got a query that captures all shipments and costs from our factory. Sample data and desired output on Google Drive here:
https://drive.google.com/open?id=0B4xdnV0LFZI1VndEaGgxNDVpU2M
The issue is we've got 2 different ways of selling things. One is 'Regluar' where we make it and the other one is a 'buy/sell' where we buy and sell it.
To capture the costs I've had to write two queries, one for each scenario. The end users of this query can enter in a date range and the query works well then, but I'm stuck when it comes to the variable #Job_No.
All work that goes through our factory (cteRegularJobs) has a Job Number associated with it and I've declared a variable so users can use it to search. The cteBuyandSell has a value of 'NULL' for Job_No declared in the SELECT statement so I can do a UNION of these two tables at the end. However, no buy/sell jobs have Job Numbers assigned to them, they are always NULL.
Initially #Job_No is declared as '' and when it's left '' I want the results from both cte tables returned. If there is an entry by the user, i.e. '001' then I want results for cteRegularJobs.
If it makes it easier I am open to declaring the cteBuyandSell.Job_No something else besides NULL, like "Buy and Sell".
The real query is complicated so here's a simplified example of the structure:
DELCARE #Job_No AS varchar(10) = '';
SET #Job_No = {User Input or leave as ''};
WITH
cteBuyandSell AS ( NULL AS 'Job_No',
...),
cteRegularJobs AS (tblJobs.Job_No AS 'Job_No',
...
WHERE tblJobs.Job_No LIKE #Job_No)
SELECT *
FROM
(cteBuyandSell
UNION
cteRegularJobs)
You can logically break this up with an IF statement to check the value of your variable. I'd suggest NULL over white space though. Here's an example procedure... with the limited code you provided.
CREATE PROCEDURE getData(#Job_No varchar(10) = NULL)
AS
IF #Job_No IS NULL
BEGIN
WITH
cteBuyandSell AS ( NULL AS 'Job_No',
...),
cteRegularJobs AS (tblJobs.Job_No AS 'Job_No',
...
WHERE tblJobs.Job_No LIKE #Job_No)
SELECT *
FROM
(cteBuyandSell
UNION
cteRegularJobs)
END
ELSE
BEGIN
WITH
cteRegularJobs AS (tblJobs.Job_No AS 'Job_No',
...
WHERE tblJobs.Job_No LIKE #Job_No)
SELECT * FROM cteRetularJobs
END