Creating a stored procedure that uses (IF ELSE) to decide what goes in a new column - sql-server-2008

I'm trying to create a stored procedure to show specific information that I need, however I don't have a column that says Yes/No when taking my dates into account.
Example...
I have a table with this info
| ID | Name | DOB |
| 1 | John | 1/1/1991 |
What I want to see is
| ID | Name | DOB | Under 21 |
| 1 | John | 1/1/1991 | No |
I'm thinking that I need to me a temp table or something in order to incorporate that extra column, then use an if/else statement to decide on whether the DOB is greater than said date.
Does anyone have any pointers that they could help me with?
Much appreciated

you can try using a case like this
SELECT *, CASE WHEN DATEDIFF(year,DOB,GETDATE()) < 21 THEN 'No'
ELSE 'Yes'
END AS 'Under 21'
FROM Table_Name

Related

Is there a way in SQL to create a new column that looks at the values in a column that shares multiple rows to determine the value in the new column?

My data looks similar to what I've attached.
I'm wanting to create a new column that looks at the appt# and any row associated with that appt to see if that appt had the procedure D0330 and marks it yes or no.
So for example B Bradley's appt 8210 did include the procedure D0330. So the new column would be marked yes for both lines 1 & 2 despite line 2 not having that specific procedure in it's row.
What would be the best way to go about doing this?
Data
You can't define a column of a table that auto-generates a value based on multiple rows, or a subquery, or anything like that. Generated columns can only be based on simple functions that reference columns within the same row.
But you can reference a window of rows in a query, and produce a column of that query result, even though the column is not persisted in the table.
SELECT *, MAX(CASE `procedure` WHEN 'D0330' THEN 'Yes' ELSE 'No' END)
OVER (PARTITION BY employee) AS `newcolumn`
FROM mytable;
+------+-----------+-----------+------+-----------+
| line | employee | procedure | apt | newcolumn |
+------+-----------+-----------+------+-----------+
| 1 | B Bradley | D0330 | 8210 | Yes |
| 2 | B Bradley | D1226 | 8210 | Yes |
| 3 | C Connor | D1457 | 1130 | No |
| 4 | D David | D0330 | 543 | Yes |
+------+-----------+-----------+------+-----------+
Window functions require MySQL 8.0.

MySQL : Use Variable/Function Returned Value in Select

There is a table named as customer like this:
| Name | Age | Balance |
------------------------------
| Kevin | 25 | 150000 |
| Bob | 33 | 350000 |
| Anna | 27 | 200000 |
Simply, to select "Name" column we can use:
SELECT Name FROM customer
Now, I want to do that by using a variable like this:
SET #temp = 'Name'
SELECT #temp FROM customer
The result I get:
| #temp |
-----------
| Name |
The result I want is same like the normal select:
| Name |
----------
| Kevin |
| Bob |
| Anna |
I am expecting this will run the same like "SELECT Name From Customer", so it basically run the SELECT from a variable value.
I also use a function returned value to do the same thing, but I get the similar result. For example, there is function called CustName(Value):
SELECT CustName(A) // Return : 'Name'
FROM customer;
This will give me result:
| CustName(A) |
-----------------
| Name |
Is there any way that MySQL will run "Name" normally like when I basically write "Select Name from customer" ?
What you're saying you're looking for is dynamic sql.. it's generally not a fabulous idea as you're trying to vary a part of a query that the database wants to be fixed, for performance reasons. You'll also struggle to make use of your sql in a client app if it's expecting a string of a username, but then the user supplied 'birthday' as the thing to select and your client gets a date instead
If you're hell bent on doing it, this SO post gives more detail: How To have Dynamic SQL in MySQL Stored Procedure
I must ask you to consider though, that this is a broken solution you've devised, to some other problem. It might be better to post the other problem as solving it may prove more productive

Dynamic value to display numbers of entries in second table

I've got multiple entries in table A and would like to display the number of entries in a coloumn of table B. Is there a way to create a dynamic cell-content displaying the number of entries in a table?
I'm a beginner in MySQL and did not find a way to do it so far.
Example table A:
+----+------+------------+
| id | name | birthday |
+----+------+------------+
| 1 | john | 1976-11-18 |
| 2 | bill | 1983-12-21 |
| 3 | abby | 1991-03-11 |
| 4 | lynn | 1969-08-02 |
| 5 | jake | 1989-07-29 |
+----+------+------------+
What I'd like in table B:
+----+------+----------+
| id | name | numusers |
| 1 | tblA | 5 |
+----+------+----------+
In my actual database there is no incrementing ID so just taking the last value would not work - if this would've been a solution.
If MySQL can't handle this the option would be to create some kind of cronjob on my server reading the number of rows and writing them into that cell. I know how to do this - just checking if there's another way.
I'm not looking for a command to run on the mysql-console. What I'm trying to figure out is if there's some option which dynamically changes the cell's value to what I've described above.
You can create a view that will give you this information. The SQL for this view is inspired by an answer to a similar question:
CREATE VIEW table_counts AS
SELECT table_name, table_rows
FROM information_schema.tables
WHERE table_schema = '{your_db}';
The view will have the cells you speak of. As you can see, it is just a filter on an already existing table, so you might consider that this table information_schema.tables is the answer to your question.
You can do that directly with COUNT() for example SELECT COUNT(*) FROM TblA The you get all rows from that table. If you IDXs are ok then its very fast. If you write it to another table you have to make an request too to get the result of the second table. So i think your can do it directly.
If you have some performance problems there are some other possibilities like Triggers or Stored Procedures to calculate that result and save them in a memory table to get a better performance.

Select userids from mysql table if user has at least 3 fields filled

I have a mysql user table that holds user data like that:
userid | title | content
----------------------------------
1 | about | I am from ...
1 | location | Norway
1 | name | Mark
1 | website |
2 | about |
2 | location |
2 | name |
2 | website |
3 | ...
As you see the content is empty for userid 2, and also for many more users in the table.
My goal is to select only the userids that have at least 3 fields filled. All others should be ignored.
As my mysql knowledge is still weak I could not find a solution for this. I only found the opposite and just with count: Find the count of EMPTY or NULL columns in a MySQL table
What is the magic mysql query? Any help appreciated, thank you.
You would use aggregation and a having clause for this:
select u.userId
from users u
where content > '' and content is not null
group by u.userId
having count(*) >= 3;
I added the non-blank check as well as the null check. The null check is redundant, but it makes the intention clearer.

MySQL - COUNT before INSERT in one query

Hey all, I am looking for a way to query my database table only once in order to add an item and also to check what last item count was so that i can use the next number.
strSQL = "SELECT * FROM productr"
After that code above, i add a few product values to a record like so:
ID | Product | Price | Description | Qty | DateSold | gcCode
--------------------------------------------------------------------------
5 | The Name 1 | 5.22 | Description 1 | 2 | 09/15/10 | na
6 | The Name 2 | 15.55 | Description 2 | 1 | 09/15/10 | 05648755
7 | The Name 3 | 1.10 | Description 3 | 1 | 09/15/10 | na
8 | The Name 4 | 0.24 | Description 4 | 21 | 09/15/10 | 658140
i need to count how many times it sees gcCode <> 'na' so that i can add a 1 so it will be unique. Currently i do not know how to do this without opening another database inside this one and doing something like this:
strSQL2 = "SELECT COUNT(gcCode) as gcCount FROM productr WHERE gcCode <> 'na'
But like i said above, i do not want to have to open another database query just to get a count.
Any help would be great! Thanks! :o)
There's no need to do everything in one query. If you're using InnoDB as a storage engine, you could wrap your COUNT query and your INSERT command in a single transaction to guarantee atomicity.
In addition, you should probably use NULL instead of na for fields with unknown or missing values.
They're two queries; one is a subset of the other which means getting what you want in a single query will be a hack I don't recommend:
SELECT p.*,
(SELECT COUNT(*)
FROM PRODUCTR
WHERE gccode != 'na') AS gcCount
FROM PRODUCTR p
This will return all the rows, as it did previously. But it will include an additional column, repeating the gcCount value for every row returned. It works, but it's redundant data...