I'm newbie to Couchbase and trying to understand whether it fits my domain.
I have following data:
Birth City Name
1980 A John
1981 B Rick
1982 A Ase
1983 C Max
1984 C Bob
1980 A Rick
1983 D John
1982 A Bob
1985 C Bob
And not getting on how to implement similar to following SQL query:
SELECT birth FROM tbl WHERE city in (A,C) and name (Bob, Rick)
to get following data:
1984 C Bob
1980 A Rick
1982 A Bob
1985 C Bob
One big difference to 'startkey' and 'endkey' view usages is that it's not range selection, it's two different dimension values.
In my real scenario I need to put 1-5000 ID's into 'IN' statement.
The only one way I found is to use indexing and N1ql http://docs.couchbase.com/prebuilt/n1ql/n1ql-dp3/#create-index.html but it seems not the fastest way to access data.
What's the best practice to access multi-dimensional key data with lots of keys values?
Is it okay to create big 'in' statements in N1ql?
You should do a view with the emit function, including a key with two dimension values, and returning the value, in this way:
emit([doc.Name, doc.City], doc.Birth);
And you will have to query it with the param keys:
keys=[["Bob", "A"], ["Bob", "C"], ["Rick", "A"], ["Rick", "C"]]
Related
MY SQL returns the following array...
id
staff
province
1
Ben
Ontario
2
Ben
Quebec
3
John
Manitoba
4
John
Saskatchewan
6
Kitty
Alberta
7
Kitty
Nova Scotia
I would like to have the record displayed like this...
staff
province
Ben
Ontario, Quebec
John
Quebec, Manitoba, Saskatchewan
Kitty
Alberta, Nova Scotia
what approach should I use to approach this?
Would be better to post the tables as well for clearer context.
You can use Aggregate functions and Grouping to help doing this. A GROUP BY to group the rows by staff column, then use GROUP_CONCAT() to concatenate province values in one string.
A reference of how you want it to be, unsure what table you are using or if there are any other factors but you can adapt as needed.
SELECT staff, GROUP_CONCAT(province SEPARATOR ', ') as province
FROM table_name
GROUP BY staff;
How can I count the number of somethings in each column, and rename as something?
For example - this is my table:
PATIENT
PatientNum City
---------------------------
1 New York City
2 Boston
3 Birmingham
4 Tyler
5 Boston
6 New York City
This is my SQL query:
SELECT
City, COUNT(DISTINCT PatientNum) AS Freq
FROM
PATIENT
GROUP BY
City
Expected result
City Freq
----------------------
Birmingham 1
Boston 2
New York City 2
Tyler 1
What is the relational algebra for this SQL query?
I am new to relational algebra, thank you for help!
Update:
I have do some research and write that. Is that right?
You need to use the group by operator, γ. The columns listed before the γ are the grouping columns, the columns listed after are your projections
So, your expression in relational algebra would be like this:
City γ City, COUNT PatientNum (PATIENT)
Relational projections always return distinct tuples.
Due to the way a particular table is written I need to do something a little strange in SQL and I can't find a 'simple' way to do this
Table
Name Place Amount
Chris Scotland
Chris £1
Amy England
Amy £5
Output
Chris Scotland £1
Amy England £5
What I am trying to do is above, so the null rows are essentially ignored and 'grouped' up based on the Name
I have this working using For XML however it is incredibly slow, is there a smarter way to do this?
This is where MAX would work
select
Name
,Place = Max(Place)
,Amount = Max(Amount)
from
YourTable
group by
Name
Naturally, if you have more than one occurance of a place for a given name, you may get unexpected results.
I have to compare addresses from two tables and get the Id if the address matches.
Each table has three columns Houseno, street, state
The address are not in standard format in either of the tables. There are approx. 50,000 rows, I need to scan through
At some places its Ave. Avenue Ave . Str Street, ST. Lane Ln. Place PL Cir CIRCLE.
Any combination with a dot or comma or spaces ,hypen.
I was thinking of combining all three What can be best way to do it in SQL or PLSQL for example
table1
HNO STR State
----- ----- -----
12 6th Ave NY
10 3rd Aven SD
12-11 Fouth St NJ
11 sixth Lane NY
A23 Main Parkway NY
A-21 124 th Str. VA
table2
id HNO STR state
-- ----- ----- -----
1 12 6 Ave. NY
13 10 3 Avenue SD
15 1121 Fouth Street NJ
33 23 9th Lane NY
24 X23 Main Cir. NY
34 A1 124th Street VA
There is no simple way to achieve what you want. There is a expensive software (google for "address standardization software") that can do this but rarely 100% automatic.
What this type of software does is to take the data, use complex heuristics to try to figure out the "official" address and then return that (sometimes with the confidence that the result is correct, sometimes a list of results sorted by confidence).
For a small percentage of the data, the software will simply not work and you'll have to fix that yourself.
Oracle has a built in package UTL_Match which has an edit_distance function (based on the Levenshtein algorithm, this is a measure of how many changes you would need to make to make one string the same as another). More info about this Package / Function can be found here: http://docs.oracle.com/cd/E18283_01/appdev.112/e16760/u_match.htm
You would need to make some decisions around whether to compare each column or concatenate and then compare and what a reasonable threshold is. For example, you may want to do a manual check on any with an edit distance of less than 8 on the concatenated values.
Let me know if you want any help with the syntax, the edit_distance function just takes 2 varchar2 args (the strings you want to compare) and returns a number.
This is not a perfect solution in that if you set the threshold high you will have a lot of manual checking to do to discard some, and if you set it too low you will miss some matches, but it may be about the best if you want a relatively simple solution.
The way we did this for one of our applications was to use a third party adddress normalization API(eg:Pitney Bowes),normalize each address(Address is a combination of Street Address,City ,State and Zip) and create a T-sql hash for that address.For the adress to compare do the same thing and compare the two hashes and if they match,we have a match
you can make a cursor where you do first a group by where house number and city =.
in a loop
you can separate a row with instr e substr considering chr(32).
After that you can try to consider to make a confront with substring where you have a number 6 = 6th , other case street = str.
good luck!
I need a setup in Access where some rows in a table are converted to columns...for example, lets say I have this table:
Team Employee DaysWorked
Sales John 23
Sales Mark 3
Sales James 5
And then through the use of a query/form/something else, I would like the following display:
Team John Mark James
Sales 23 3 5
This conversion of rows to columns would have to be dynamic as a team could have any number of Employees and the Employees could change etc. Could anyone please guide me on the best way to achieve this?
You want to create a CrossTab query. Here's the SQL that you can use.
TRANSFORM SUM(YourTable.DaysWorked) AS DaysWorked
SELECT YourTable.Team
FROM YourTable
GROUP BY YourTable.Team
PIVOT YourTable.Employee
Of course the output is slightly different in that the columns are in alphabetical order.
Team James John Mark
Sales 5 23 3
For more detail see Make summary data easier to read by using a crosstab query at office.microsoft.com