Is it possible to edit Sphinx needtable contents? - documentation

For example, if I had this table:
.. needtable:: names of people
:style: datatables
:types: english
:columns: names, age, birth
and it looks like this:
|Name |Age |Birth |
-------------------------
|John man |20 |England |
|Jen Smith |30 |America |
Is it possible to manipulate the data so that only first names are shown?
|Name |Age |Birth |
-------------------------
|John |20 |England |
|Jen |30 |America |
Thought about using regex or :filter-func: but it doesn't work.

Related

Noob needs advise on three tables in postgresql DB

I've very minor experience at working with databases( I only know the absolute basics). With that stated I guess my problem is rather easy to solve for more experienced minds.
My question is:
I need a way to be able to search for example "all types of Aluminium and all its sub_materials"?
I can do simple queries like
SELECT *
FROM sub_materials
WHERE category_id = 2;
So what I'm asking for is basically to be able to see the whole branch of Aluminium. I've looked at ltreeand Closure Tables But I'm to much of a Noob to figure it out by my self.
I believe I have to connect the tables somehow as "grandparent, parent, child" or something similar, but I've no idea if there is some other way or?
I'm not even sure if I'm doing this the right way.
Can someone advise me on this?
I've three tables in a database.(below are samples from the tables)
materials_category Holds all the categories for every material in the DB.
materials Holds the Category name and id, material_names and ids and the values for each material.
Some materials has sub_categories. not all but some.
3. sub_materials Holds the category info, material name and id, the sub_material name and id and the sub_material values.
1. `materials_category`
|category_id| category_name|
------------------------------
| 1 | Aggregate |
| 2 | Aluminium |
| 3 | Asphalt |
2. `materials`
|category_id |category_name |material_id |material_name|EE_1|EE_2|EC_1|EC_2
---------------------------------------------------------------------------
| 1 |Aggregate | 1 | General |3 |0 | 52 | 8
| 2 |Aluminium | 2 | General |55 |7 | 9 | 24
| 2 |Aluminium | 3 | Cast Pr |34 |30 | 22 | 28
| 2 |Aluminium | 4 | Extrud. |65 |16 | 8 | 74
| 2 |Aluminium | 5 | Rolled |15 | 0 | 9 | 61
| 3 |Asphalt | 6 |Asphalt, 4% |2 |22 | 6 | 54
| 3 |Asphalt | 7 |Asphalt, 5% |3 |91 | 1 | 4
3. `sub_materials`
|category_id|category_name|material_id|material_name|sub_mar_id|sub_mar_name|EE_1|EE_2|EC_1|EC_2|
|2 |Aluminium |2 |General |1 |Virgin |21 |5 |9 |60 |
|2 |Aluminium |2 |General |2 |Recycled |29 |3 |8 |9 |
Your tables are denormalised. This is usually not a good thing, unless this is a reporting only database.
Anyway, here's a start for what you're after. why don't you run it and give some feedback on whether it's what you want or not.
select *
from materials_category c
left outer join materials m
on c.category_id = m.category_id
left outer join sub_materials s
on s.material_id = m.material_id
where c.category_name = 'Aluminium'
Looking at your data, I'm guessing that 'recycled' could apply to any number of material categories, not just Aluminium? Would you have a whole bunch of 'Recycled' records in your sub_materials to allow for each material_category?

Select rows with same id but different result in another column

sql: I have a table like this:
+------+------+
|ID |Result|
+------+------+
|1 |A |
+------+------+
|2 |A |
+------+------+
|3 |A |
+------+------+
|1 |B |
+------+------+
|2 |B |
+------+------+
The output should be something like:
Output:
+------+-------+-------+
|ID |Result1|Result2|
+------+-------+-------+
|1 |A |B |
+------+-------+-------+
|2 |A |B |
+------+-------+-------+
|3 |A | |
+------+-------+-------+
How can I do this?
SELECT
Id,
MAX((CASE result WHEN 'A' THEN 'A' ELSE NULL END)) result1,
MAX((CASE result WHEN 'B' THEN 'B' ELSE NULL END)) result2,
FROM
table1
GROUP BY Id
results
+------+-------+-------+
|Id |Result1|Result2|
+------+-------+-------+
|1 |A |B |
|2 |A |B |
|3 |A |NULL |
+------+-------+-------+
run live demo on SQL fiddle: (http://sqlfiddle.com/#!9/e1081/2)
there are a few ways to do it.
None of tehm a are straight forward.
in theory, a simple way would be to create 2 temporary tables, where you separte the data, all the "A" resultas in one table and "B" in another table.
Then get the results with simple query. using JOIN.
if you are allowed to use some scrpting on the process then it is simpler, other wise you need a more complex logic on your query. And for you query to alwasy work, you need to have some rules like, A table always contains more ids than B table.
If you post your real example, it is easier to get better answers.
for this reason:
ID Name filename
1001 swapan 4566.jpg
1002 swapan 678.jpg
1003 karim 7688.jpg
1004 tarek 7889.jpg
1005 karim fdhak.jpg
output:
ID Name filename
1001 swapan 4566.jpg 678.jpg
1003 karim 7688.jpg fdhak.jpg
1004 tarek 7889.jpg ...
.. ... ... ...

How to get middle character from a string using SQL Server?

I have a table like this.
----------------
|Id | Name |
----------------
|1 |Apple |
|2 |banana |
|3 |Orange |
|4 |Grapes |
|5 |Mango |
----------------
I need middle character from each string.My result is to be like this
--------------------
|Id | Name |MidChar|
------------------------
|1 |Apple | p |
|2 |banana | n(or)a|
|3 |Orange | a(or)n|
|4 |Grape | a |
|5 |Mango | n |
-------------------
Please give some solutions or ideas.
Use LEN and Substring function
Try this
declare #Name varchar(10) = 'Apple'
select substring(#Name,LEN(#Name)/2+1,1)
I would suggest you try the below query. It should suit all your specified requirements.
SELECT [Id],[Name],CASE WHEN LEN(Name)%2 != 0
THEN SUBSTRING(Name,LEN(Name)/2+1,1)
ELSE SUBSTRING(Name,LEN(Name)/2,1)+' (or) '+ SUBSTRING(Name,LEN(Name)/2 + 1,1)
END AS [MidChar]
FROM [yourTable]
Kindly share your feedback.
Try the below script,
SELECT Id,Name,SUBSTRING(Name,CEILING(LEN(Name)/2.0),1) AS MidChar
FROM MyTable

How to Merge 2 Rows into one by comma separate?

I need to merge this individual rows to one column, I now how to merge column by comma separated,
+---------------+-------+-------+
|CID |Flag |Value |
+---------------+-------+-------+
|1 |F |10 |
|1 |N |20 |
|2 |F |12 |
|2 |N |23 |
|2 |F |14 |
|3 |N |21 |
|3 |N |22 |
+---------------+-------+-------+
Desired Result can be anything,
+-----------+----------------------------+ +--------------------------+
|Part Number| Value | | Value |
+-----------+----------------------------+ +--------------------------+
| 1 | 1|F|10 ; 1|N|20 | Or | 1|F|10 ; 1|N|20 |
| 2 | 2|F|12 ; 2|N|23 ; 2|F|14 | | 2|F|12 ; 2|N|23 ; 2|F|14 |
| 3 | 3|N|21 ; 3|N|22 | | 3|N|21 ; 3|N|22 |
+-----------+----------------------------+ +--------------------------+
Note:
Any hint in right direction with small example is more than enough
EDIT :
I have massive data in tables like thousands of records where parent's and child relationship is present. I have to dump this into text files by comma separated values In single line as record. Think as primary record has relationship with so many other table then all this record has to be printed as a big line.
And I am trying to achieve by creating query so load can be distributed on database and only thing i have to worry about in business is just dumping logic into text files or whatever form we need in future.
You can try to use LISTAGG and your query will look like this:
select a.cid, a.cid || listagg(a.flag || '|' || a.value, ',')
from foo.dat a
group by a.cid
You can use different separators and of course play with how the result will be formatted.

Adding an additional column to SQL UNION SELECT

Suppose I have the following mapped and normalized tables;
Group User Contact BelongsTo
+-------+------+------+ +-------+------+------+ +-------+------+------+ +-------+------+
| gID| name| col3| | uID| fname| sname| | cID| name| col3| | accID| gID|
+-------+------+------+ +-------+------+------+ +-------+------+------+ +-------+------+
|1 |ABC |? | |1 |JJ |BB | |4 |ABCD |? | |1 |2 |
+-------+------+------+ +-------+------+------+ +-------+------+------+ +-------+------+
|2 |BCD |? | |2 |CC |LL | |5 |BCDE |? | |3 |2 |
+-------+------+------+ +-------+------+------+ +-------+------+------+ +-------+------+
|3 |DEF |? | |3 |RR |NN | |6 |CDEF |? | |5 |3 |
+-------+------+------+ +-------+------+------+ +-------+------+------+ +-------+------+
Using EERM, User and Contact are subclasses of "Account" superclass. (not shown) An account can belong to many groups, thus "BelongsTo" table records the M:N relationship between the Accounts and Group membership.
I would like an SQL statement which will allow me to query all the users and contacts that have membership in a Group matching conditions as follows:
SELECT
tc."cID" AS "accID",
tc."name" AS "accName",
tbt."gID"
FROM "tblContact" tc
INNER JOIN "tblBelongsTo" tbt
ON tbt."accID" = tc."cID"
UNION SELECT
tu."uID" AS "accID",
CONCAT (tu."fname", ' ', tu."sname") AS "accName",
tbt."gID"
FROM "tblUser" tu
INNER JOIN "tblBelongsTo" tbt
ON tbt."accID" = tu."uID"
ORDER BY "accID" ASC;
The above works, I have combined UNION SELECT in the query as the number of columns match either side when I CONCAT the forename and surname together. Resulting in a global "account_name" & "account_id" column.
My question is this: How would I go about adding an extra column so that I can see what the group name is?
ie from this:
Result
+-------+-------+------+
| accID|accName| gID|
+-------+-------+------+
|1 |JJBB |2 |
+-------+-------+------+
|3 |RRNN |2 |
+-------+-------+------+
|5 |BCDE |3 |
+-------+-------+------+
to this:
Result (2)
+-------+-------+------+------+
| accID|accName| gID| name|
+-------+-------+------+------+
|1 |JJBB |2 | BCD|
+-------+-------+------+------+
|3 |RRNN |2 | BCD|
+-------+-------+------+------+
|5 |BCDE |3 | DEF|
+-------+-------+------+------+
It seems everything I have tried causes the UNION SELECT to break (because of unmatched column). Likewise, I had little luck in combining sub-queries. I am probably missing something very obvious...
Thanks in advance.
You can join it to the group table to get the name.
select x.*, g.name
from group g join
(
SELECT
tc."cID" AS "accID",
tc."name" AS "accName",
tbt."gID"
FROM "tblContact" tc
INNER JOIN "tblBelongsTo" tbt
ON tbt."accID" = tc."cID"
UNION
SELECT
tu."uID" AS "accID",
CONCAT (tu."fname", ' ', tu."sname") AS "accName",
tbt."gID"
FROM "tblUser" tu
INNER JOIN "tblBelongsTo" tbt
ON tbt."accID" = tu."uID"
) x on x.gid = g.gid
order by accid;