Can I use the row record as header for a table? - sql

Let say I have 2 table, rowName and realRecord
rowName has 2 column
1.Index______________2.FieldName
1____________________Name
2____________________Surname
3____________________Age
4____________________Gender
And realRecord column header will depend on what I have for field name in the previous table. For example in this case, realRecord will have Name, Surname, Age and Gender as header
I wish to do it as I may have to add additional column and I want to add to rowName table as it will be neater

Assume your table rowName has following records:
Index | FieldName
-------------------
1 | Name
2 | SurName
3 | Age
4 | Gender
Your RealRecord has,
RecordID | EmployeeID | FieldIndex | Value
1 | E123 | 1 | Nishanthi
2 | E123 | 2 | Grashia
and so on... If you want a new field say Nationality, You can perhaps add additional records as below:
rowName :
Index | FieldName
-------------------
5 | Nationality
RealRecord
RecordID | EmployeeID | FieldIndex | Value
100 | E123 | 5 | Indian
You can achieve this scenario easily if you have realRecord structure as in my answer (Multiple Rows instead of Multiple Headers as per your existing spec)

Related

How to update Multiple rows in PostgreSQL with other fields of same table?

I have table which consist of 35,000 records in which half of the rows have name as null value
If the field has null value, i want to update the field with the value of username.
Can anyone help me with this ?
This is sample table
name | username | idnumber | type
----------------------------------------------
-- | jack | 1 | A
Mark | Mark | 2 | B
-- | dev | 3 | A
After update i want it to look like this
name | username | idnumber | type
----------------------------------------------
jack | jack | 1 | A
Mark | Mark | 2 | B
dev | dev | 3 | A
You seem to want:
update t
set name = username
where name is null;
Note that -- is not a typical representation of NULL values. You might consider <null> for instance.

How can I load rows by name and role and order them based on their name, but preference the name? (SQL)

What I mean is the following.
I have a database containing people. These people van a name and a role.
It looks like this:
-----------------------------
| (PK) id | name | role |
-----------------------------
| 1 | ben | fireman |
| 2 | ron | cook |
| 3 | chris | coach |
| 4 | remco | barber |
-----------------------------
Ive created a searchbar where you can search for people in the database. When you press search, it looks for name and roles, for example:
When I type in 'co', the result I get is:
-----------------------------
| (PK) id | name | role |
-----------------------------
| 3 | chris | coach |
| 4 | remco | barber |
| 2 | ron | cook |
-----------------------------
This is because its looking for matches in the name and role column.
The query I use is:
SELECT * FROM people WHERE name LIKE '$search' OR role LIKE '$search' ORDER BY name";
The only issue with this is that it just order by name.
I want it to first order every result from the name column by name and then order every remaining result from the role column by name, so it ends up looking like this:
-----------------------------
| (PK) id | name | role |
-----------------------------
| 4 | remco | barber | <- 'co' found in name column, ordered by name
| 3 | chris | coach | <- 'co' found in role column, ordered by name
| 2 | ron | cook | <- 'co' found in role column, ordered by name
-----------------------------
How can I do this?
Edit: $search is the output from the searchbar
Use a case expression to put the 'co' names first:
order by case when name LIKE '$search' then 0 else 1 end, name, role

Pivot SSRS Dataset

I have a dataset which looks like so
ID | PName | Node | Val |
1 | Tag | Name | XBA |
2 | Tag | Desc | Dec1 |
3 | Tag | unit | Int |
6 | Tag | tids | 100 |
7 | Tag | post | AAA |
1 | Tag | Name | XBB |
2 | Tag | Desc | Des9 |
3 | Tag | unit | Float |
7 | Tag | post | BBB |
6 | Tag | tids | 150 |
I would like the result in my report to be
Name | Desc | Unit | Tids | Post |
XBA | Dec1 | int | 100 | AAA |
XBB | Des9 | Float | 150 | BBB |
I have tried using a SSRS Matrix with
Row: PName
Data: Node
Value: Val
The results were simply one row with Name and next row with desc and next with unit etc. Its not all in the same rows and also the second row was missing. This is possibly because there is no grouping on the dataset.
What is a good way of achieving the expected results?
I would not recommend this for a production scenario but if you need to knock out a report quickly or something you can try this. I would just not feel comfortable that the order of the records you get will always be what you expect.
You COULD try to insert the results of the SP into a table (regular table, temp table, table variable...doesn't matter really as long as you can get an identity column added). Assuming that the rows always come out in the correct order (which is probably not a valid assumption 100% of the time) then add an identity column on the table to get a unique row number for each row. From there you should be able to write some math logic to "group" your values together and then pivot out what you want.
create table #temp (ID int, PName varchar(100), Node varhar(100), Val varchar(100))
insert #temp exec (your stored proc)
alter table #temp add UniqueID int identity
then use UniqueID (modulo on 5 perhaps?) to group records together and then pivot

Efficient Classification of records by common letters in impala

I have a table in impala (TBL1), that contains different names with different number of first common letters. The table contains about 3M records. I would like to add add an new attribute to the table, where each common first letters will have a class. It is the same way as DENSE_RANK work but with dynamic number of first letters. The number of same first letters should not be less than p=3 letters (p = parameter).
Here is an example for the table and the required results:
| ID | Attr1 | New_Attr1 | Some more attribute...
+-------+--------------+-------------+-----------------------
| 1 | ZXA-12 | 1 |
| 2 | YL3300 | 2 |
| 3 | ZXA-123 | 1 |
| 4 | YL3400 | 2 |
| 5 | YL3-aaa | 2 |
| 6 | TSA 789 | 3 |
...
Does this do what you want?
select t.*,
dense_rank() over (order by strleft(attr1, 3)) as newcol
from . . .;
The "3" is your parameter.
As a note: In your example, you seem to have assigned the new value in reverse alphabetic order. Hence, you would want desc for the order by.

Dynamic view with dynamic list of columns in postgres

I have a scenario as follows:
Table User
ID User viewname Description
1 JamesId employeeview This is a employee
2 FredId employeeview This is a employee
3 NickId managerview This is a manager
Table properties
ID key value user_ref
1 name james 1
2 phone 88888888888 1
3 email test#test.com 3
4 name fred 2
5 phone 555555555555 2
I need to create views as following:
EmployeeView
Id empId name phone Description
1 JamesId james 88888888888 This is a employee
2 FredId fred 555555555555 This is a employee
ManagerView
Id empId email Description
1 NickId test#test.com This is a manager
properties table is a extension to User table where everything is stored as key value pair. Views need to be created dynamically using the above two tables. Properties table is dynamic.
Is it possible to generate such view. Can someone give a example to do it.
I managed to do it with crosstab function in postgres. However i had to modify the properties table to accommodate another colume as a identified.
Modified property table:
+------+-------+---------------+-------------+-------------------+
| **ID | key | value | user_ref | identifier** |
+------+-------+---------------+-------------+-------------------+
| 1 | name | james | 1 | viewemployee |
| 2 | phone | 88888888888 | 1 | viewemployee |
| 3 | email | test#test.com | 3 | viewmanager |
| 4 | name | fred | 2 | viewemployee |
| 5 | phone | 555555555555 | 2 | viewemployee |
+------+-------+---------------+-------------+-------------------+
Sql query using crosstab for employeeview:
create MATERIALIZED VIEW EmployeeView as
select one.id, one.userid, two.name, two.phone, one.description from
user_table one,
(SELECT *
FROM crosstab(
'SELECT user_ref,key,value FROM user_properties where identifier=''viewemployee'' order by 1,2')
AS
ct_row (user_ref int, name varchar, phone varchar)) two
where
one.id=two.user_ref
Sql query using crosstab for managerview:
create MATERIALIZED VIEW ManagerView as
select one.id, one.userid, two.email, one.description from
user_table one,
(SELECT *
FROM crosstab(
'SELECT user_ref,key,value FROM user_properties where identifier=''viewmanager'' order by 1,2')
AS
ct_row (user_ref int, email varchar)) two
where
one.id=two.user_ref