(1) Create a relational table TOTAPPS(anum,totap) that contains information
about the numbers of applicants (anum) and the total number
of applications (totap) submitted by each applicant. If an applicant submitted
no application then a value ofattribute
totap should be equal to zero. Load data to a relational table TOTAPPS.
So i just want to understand that the relational table isn't using following query
CREATE TABLE test_table ( anum INTEGER, totap INTEGER );
and how do i point the anum to count the number of applicants using the relational table
example my applicants table like this:
//Applicant
A#
---------
1
2
3
4
5
//Application
A# POSITION
--------------------
1 CEO
1 GM
2 DIRECTOR
3 MANAGER
so when i SELECT * FROM TOTAPPS, the following result should be like this:
//TOTAPPS
ANUM TOTAP
-------------------
1 2
2 1
3 1
4 0
5 0
i must insert manually or when i create the relational table i can straight count it?
Related
For my project, I have 2 tables. Initially I inner joined both tables (table 1 and 2) via an inner join. However, I wanted an outcome as seen in table 4 where the repeated value from table 1 is left blank instead.
For table 2, the number of rows will always vary. There will always only be 1 department ID attached to numerous function IDs. Is there a query then where regardless of the number of function IDs, the department ID will only appear as the first row as seen in table 4?
(I think to many, this might seem weird and frankly not clean data, but for mail merges within word, it is easier to field code when the data is presented this way to refrain sections from 'reprinting itself'.)
Current Code:
SELECT Table1.*, Table2.* FROM
INNER JOIN Table 2 ON Table1.DepartmentID = Table2.DepartmentID
Table 1:
Department ID
Department
1
XYZ
Table 2:
Department ID
Function ID
Function
1
1
ABC
1
2
DEF
Table 3 (inner joined):
Department ID
Department
FunctionID
Function
1
XYZ
1
ABC
1
XYZ
2
DEF
Table 4 (desired):
Department ID
Department
FunctionID
Function
1
XYZ
1
ABC
2
DEF
Unsure how to create a pivot report query in postgres (newbie to postgres) based on the following tables/report layout.
Please note that I am not able to change the structure of these tables as out of my control.
STOCK_REF ( sr_id, stock_name )
STOCK_INVENTORY ( si_id, sr_id, stock_count ) * where sr_id here is a foreign key constraint
Sample data for each table may include the following:
STOCK_REF
1 GUITAR
2 BASS
3 DRUMS
4 KEYBOARDS
STOCK_INVENTORY
1 1 10
2 2 5
3 3 2
4 4 15
Using the above two tables, I need to produce a report that looks like:
STOCK NAME COUNT
--------------------------- --------
GUITAR 10
BASS 5
DRUMS 2
KEYBOARDS 15
which is like a pivot table.
The thing is, I can write the query that will produce the stock name as columns with counts but I actually need to have the stock name and counts as rows, like above.
Any help with this postgres query would be ideal
Try the query below. It's simple SQL. I think POSTGRES don't determine too much here...
SELECT stock_name AS "STOCK NAME", stock_count AS "COUNT"
FROM STOCK_REF INNER JOIN STOCK_INVENTORY
ON (STOCK_REF.sr_id = STOCK_INVENTORY.sr_id)
Scenario is like this (I am using SQLite):
Main table books:
CREATE TABLE "books" (
"bookId" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"bookName" nvarchar NOT NULL COLLATE NOCASE
);
Foreign key table booksLang:
CREATE TABLE "booksLang" (
"bookLangId" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"BookId" INTEGER NOT NULL,
"LanguageID" INTEGER NOT NULL,
"BookName" TEXT
);
Data in books:
bookId BookName
1 Genesis
2 Exodus
3 Leviticus
4 Numbers
5 Deuteronomy
Data in booksLang:
bookLangId BookId LanguageID BookName_Localization
1 1 12 Genèse
2 2 12 Exode
3 1 10 Schöpfung
4 4 10 Zahlen
Question:
How does the query has to look like when the given languageID is 12 and the following result has to come out (the tables books and booksLang are "somehow" joined - but tell me how):
bookId BookName BookName_Localization
1 Genesis Genèse
2 Exodus Exode
3 Leviticus ----- (none because there is no localization)
4 Numbers ----- (none because there is no localization)
5 Deuteronomy ----- (none because there is no localization)
If the languageID is 10 the result has to look like this:
bookId BookName BookName_Localization
1 Genesis Schöpfung
2 Exodus ----- (none because there is no localization)
3 Leviticus ----- (none because there is no localization)
4 Numbers Zahlen
5 Deuteronomy ----- (none because there is no localization)
As I am no SQL expert, I do not get it how always to retrieve all records from books table and retrieve BookName_Localization from the joined but retrieve "null" or something similar in it.
Also there could be localizations in many different languages but the resultset must always only contain exactly 1 record for each main record (not less or more) - enriched with the BookName_Localization field from the joined booksLang table for a given LanguageID. The value of this additional field either contains the localized text for the given language if it exists - or, if it does not exist - must be empty.
Any help is greatly appreciated.
*Edited because of bad formatting
I think you are describing a LEFT JOIN:
select b.*, bl.bookname
from books b left join
bookslang bl
on bl.BookId = b.BookId and bl.languageId = 12
I've been working on a database which consists of two schemas names as front and backup. Where in one table name:
front.Details
studID SemID GPA
100 1 4
200 2 3
Another table name is:
backup.DetailsV
studID DEPT SemID GPA
The output in Table backup.DetailsV should look like below:
studID DEPT SemID GPA
100 1 1 4
200 1 2 3
100 2 1 4
200 2 2 3
How can I create trigger on table Details to insert in to table DetailsV twice with dept id 1 and 2?
To continue Damien's thought, if the only reason to have the DetailsV table is to generate that output, you can easily do that with a view. If it is just reading data, the stored procedure doesn't know or care if the source is a table or a view.
Select studID, 1 as Dept, SemID, GPA
From front.Details
UNION ALL
Select studID, 2 as Dept, SemID, GPA
From front.Details
You would only keep a backup table if you needed to keep a history of the data flowing through the front.Details table, or if you needed to manipulate that data before reporting it out. If that is really what you want, the trigger query is very similar, but instead of addressing the table, you use the special [inserted] table to get the new values.
Select studID, 1 as Dept, SemID, GPA
From inserted
UNION ALL
Select studID, 2 as Dept, SemID, GPA
From inserted
I have two tables, specifically, they contain standard and specific parameters respectively.
Table1:
PKParameter Name Unit
1 Temperature K
2 Length mm
3 Pressure bar
Table2:
PKSpecParam Name Unit
1 Weight kg
2 Area m2
PKParameter ans PKSpecParameter are primary keys
I would like to combine these two tables into a third table which will keep track of the primary keys so I can reference any of the parameters, regardless of the table they are from.
For example:
PKCombined PKParameter PKSpecParameter
1 1 NULL
2 2 NULL
3 3 NULL
4 NULL 1
5 NULL 2
Now I would like to use PKCombined primary key to reference parameter
Maybe there is a better way to do this, but I've just started meddling with databases.
Select a.PKParameter , a.name,a.unit,b.PKSpecParam , b.name,b.unit
from table1 a outer join table2 b on a.pkparameter=b.pkspecparam
However, this will give out null values if number of entries in pkparameter and pkspecparam dont match