Is it possible to get 3 select query results by executing only one stored procedure? - tsql

I have to display data in the following format
-----------------------------------------------------------
| Group Name | Description | Assigned Users | Super Groups|
-----------------------------------------------------------
|Group1 | Blah Blah | User1 | SPG1 |
| | | User2 | SPG3 |
| | | User3 | |
-----------------------------------------------------------
| Group2 | More Blah | User1 | SPG5 |
| | | User13 | |
-----------------------------------------------------------
Assigned users and Super groups data are coming from unrelated tables. Now I wonder whether is it possible to get 3 select query results in one shot (i.e. the same procedures returns 3 results). Otherwise I'm going to query the groups and users first, get the group IDs then query super groups.
So again, Is it possible to get 3 select query results by executing only one stored procedure?

Yes, just include 3 select statements.
If you're consuming these in .net and storing them in a DataSet you'll have 3 tables in the DataSet.
Example:
create procedure test
as
select 1 as res1;
select 2 as res2;
select 3 as res3
exec test

Yes. You'll have to include the three statements in your stored procedure. Take a look at this post.

Related

Postgres DB Schema - multiple columns vs one json column

I have a db that contains username with 3 different phone numbers and 3 different ids. also we will have 3 different type of notes for each username.
I am using postgres and data are planned to increase to millions of rows. querying and inserting new data process are really important to be fastest way.
which schema would be better for that:
username | no1(string) | no2(string) | no3(string) | id1(string) | id2(string) | | id3(string) | note1(string) | note2(string) | note3(string)
OR
username | no(JSON) | id(JSON) | note(JSON)

Know which table are affected by a connection

I want to know if there is a way to retrieve which table are affected by request made from a connection in PostgreSQL 9.5 or higher.
The purpose is to have the information in such a way that will allow me to know which table where affected, in which order and in what way.
More precisely, something like this will suffice me :
id | datetime | id_conn | id_query | table | action
---+----------+---------+----------+---------+-------
1 | ... | 2256 | 125 | user | select
2 | ... | 2256 | 125 | order | select
3 | ... | 2256 | 125 | product | select
(this will be the result of a select query from user join order join product).
I know I can retrieve id_conn througth "pg_stat_activity", and I can see if there is a running query, but I can't find an "history" of the query.
The final purpose is to debug the database when incoherent data are inserted into the table (due to a lack of constraint). Knowing which connection do the insert will lead me to find the faulty script (as I have already the script name and the id connection linked).

What would be the most maintainable solution to define tables that have many columns in common?

I have the following schema (which is one approach):
CONTACTS
--------
|id |
|name |--------------------------
-------- \ \
| \ \
| \ \
^ ^ ^
PHONE_NUMBERS ADDRESSES EMAILS
-------------- -------------- --------------
|id | |id | |id |
|FK(contacts)| |FK(contacts)| |FK(contacts)|
|preferred | |preferred | |preferred |
|type | |type | |type |
|inserted_at | |inserted_at | |inserted_at |
| ---------- | | ---------- | | ---------- |
|phone_no | |address | |email |
-------------- |city | --------------
|(...) |
The other two solution I came up with was (1) inheritence and (2) dumping all of them in one table which is probably the ugliest. (Or maybe I am doing something fundamentally wrong.)
This is the right approach when designing OLTP systems. Please remember, that having multiple tables in your database don't make reading from them slower and is called database normalisation.
It's better to have a dictionary table contacts and manage them within one table and add references to it using foreign keys in different tables implementing 1:N relationships between entities.
You could dig deeper into normalisation and also create a table for lookup on your type column for each table to avoid data redundancy.
You could think of table inheritance but try to model it the way your business realities are represented. Avoid second approach which is "the ugliest" as you've already noticed.
Inheritance might not be what you're looking for - inspect FROM ONLY clause. You still have different types per entity.

Is it possible to use different forms and create one row of information in a table?

I have been searching for a way to combine two or more rows of one table in a database into one row.
I am currently creating multiple web-based forms that connect to one table in my database. Is there any way to write some mysql and php code that will take separate form submissions and put them into one row of the database instead of multiple rows?
Here is an example of what is going into the database:
This is all in one table with three rows.
Form_ID represents the three different forms that I used to insert the data into the table.
Form_ID | Lot_ID| F_Name | L_Name | Date | Age
------------------------------------------------------------
1 | 1 | John | Evans | *NULL* | *NULL*
-------------------------------------------------------------
2 |*NULL* | *NULL* | *NULL* | 2017-07-06 | *NULL*
-------------------------------------------------------------
3 |*NULL* | *NULL* | *NULL* | *NULL* | 22
This is an example of three separate forms going into one table. Every time the submit button is hit the data just inserts down to the next row of information.
I need some sort of join or update once the submit button is hit to replace the preceding NULL values.
Here is what I want to do after the submit button is hit:
I want it to be combined all into one row but still in one table
Form_ID is still the three separate forms but only in one row now.
Form_ID |Lot_ID | F_Name | L_Name | Date | Age
----------------------------------------------------------
1 | 1 | John | Evans | 2017-07-06 | 22
My goal is once a one form has been submitted I want the next, different form submission to replace the NULL values in the row above it and so on to create a single row of information.
I found a way to solve this issue. I used UPDATE tablename SET columname = newColumnName WHERE Form_ID = newID
So this way when I want to update rows that have blanks spaces I have it finding the matching ID's

Select distinct rows from MongoDB

How do you select distinct records in MongoDB? This is a pretty basic db functionality I believe but I can't seem to find this anywhere else.
Suppose I have a table as follows
--------------------------
| Name | Age |
--------------------------
|John | 12 |
|Ben | 14 |
|Robert | 14 |
|Ron | 12 |
--------------------------
I would like to run something like SELECT DISTINCT age FROM names WHERE 1;
db.names.distinct('age')
Looks like there is a SQL mapping chart that I overlooked earlier.
Now is a good time to say that using a distinct selection isn't the best way to go around querying things. Either cache the list in another collection or keep your data set small.