|
I'd like to create a SQL statement which would do such thing:
Having a table/query of these columns:
number of type integer
text of type text (string)
I'd like execute such a SQL Statement:
SELECT number,concatenate(text)
FROM table
GROUP BY number
Resulting rows should contain concatenation of every value in column "text" for each number value.
Is it possible at all?
modified on Thursday, December 23, 2010 5:55 AM
|
|
|
|
|
liquid_ wrote: Is it possible at all?
Yes, That is possible.
Suppose Consider AdventureWorks DB
First create UDF fn_Join as Below,
USE [AdventureWorks]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[fn_Join](@managerID int)
returns varchar(max)
AS
BEGIN
DEclare @ret varchar(max)
SELECT @ret = SUBSTRING(
(SELECT ',' + s.Title
FROM HumanResources.Employee s
WHERE ManagerID = @managerID
FOR XML PATH('')),2,200000)
RETURN(@ret)
END
That will give you a concatenated string of all the Title by giving ManagerID as a INPUT.
Now write the query to fetch all the distinct ManagerID and it's corresponding concatenated Title
select distinct ManagerID,dbo.fn_Join(ManagerID)
from HumanResources.Employee
WHERE ManagerID is not null
That's will give you a desired result.
|
|
|
|
|
Thanks but I'm afraid this does not work with MS Access. There is no straight way to create function or procedure or I don't know about it.
|
|
|
|
|
You could've mentioned MS ACCESS at a database.
liquid_ wrote: There is no straight way to create function or procedure or I don't know about it.
I also don't know as I've not worked much on ACCESS.
Regards,
Hiren.
modified on Thursday, December 23, 2010 2:52 AM
|
|
|
|
|
With MS Access I had to use VB function which does similar thing. Anyway thanks for an idea.
|
|
|
|
|
Hey all,
I am trying to construct a basic search query and am having trouble with my wildcard searches.
I am using C# Visual Studio 2008 and the dataset query designer.
The part of the query I am struggling with is:
WHERE (Land_use_code LIKE '%' +@Landusecode1 + '%')
However, when I run the query, it doesn't perform as I would expect.
There are multiple table entries, with some as follows:
R1
R3
R30
If I enter R and run the query, it returns nothing. If I enter R1, it returns all that match R1.
If I enter R%, it will return R1 and R3, but not R30. If I enter R%%, I will get all records.
I am wondering of a way to enable me to enter R% and get all records returned.
Thanks,
Joe
|
|
|
|
|
I think there is a problem with the quotes.
with a literal search, it would have to look like this:
WHERE Land_use_code LIKE '%R%'
so the entire literal string (with the percent signs if any) is inside quotes, as it is a string literal, not a numeric literal.
I'm not sure how that works with a variable in SQL.
FWIW: In C# it would be:
new SQLCommand("...WHERE (Land_use_code LIKE '%" + Landusecode1 + "%'");
BTW: you didn't mention what database was used; different databases might use different wildcard characters, and there could even be a character matching any single character, and another matching any sequence of characters.
|
|
|
|
|
Thanks Luc,
I am using SQL Server 2008.
So with your C# example there, you are passing in the variable Landusecode1 to the query and then applying the wildcards aren't you.
If that is how I need to approach it then I can manage to sort it out, I just thought there might be an easier way (as you said - possibly a multiple character wildcard).
Joe
|
|
|
|
|
Hi Joe,
This page[^] suggests SQL variables can sit inside quotes (I know PHP does that), so it could be as simple as:
WHERE Land_use_code LIKE '%@Landusecode1%'
Beware the (absence of) spaces inside the quoted stuff.
|
|
|
|
|
From the look of it, the original
Land_use_code LIKE '%' + @Landusecode1 + '%
is the correct way. I might need to look into using combo boxes with equals|like in it to control the results a bit more, and code the query as necessary from those.
Just a bit more than I wanted to have to do! But no doubt it will be good experience!
Thanks for your help
|
|
|
|
|
Joe Stansfield wrote: From the look of it, the original
Land_use_code LIKE '%' + @Landusecode1 + '%
is the correct way.
No way, you really need the percent signs and the known content all inside a single pair of quotes.
Where is yous snippet sitting, inside a programming language or inside an SQL procedure? maybe show a bit more of it, and somebody will fix it for you.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
modified on Wednesday, December 22, 2010 7:41 AM
|
|
|
|
|
It's sitting inside a datasource as a query (so similar to a stored procedure?) and then executed via a button and a table adapter in the programming language.
This is the full query as it sits at the moment:
SELECT File_Name, PID, Address_1, Address_2, Locality, Municipality, Postcode, Sale_date, Sale_price, Total, Capital_value, Land_area, Land_use_code, Room_count, Building_area, Construction_year, Wall_construction_code, Roof_construction_code, Land, Rate_1, Val_Ref, Source FROM vSaleSearch
WHERE (Municipality = @Municiaplity1 OR Municipality = @Municipality2)
AND (Locality LIKE @Locality + '%' OR Locality = @Locality1 OR Locality = @Locality2)
AND (Land_use_code LIKE '%' + @Landusecode1 + '%')
AND (Sale_price >= @Sale_Price1 )
AND ( Sale_price <= @Sale_Price2)
AND (Sale_date >= @Sale_Date1)
ORDER BY Sale_price
I'm trying to achieve the same thing on the locality. What I'd ideally like is for a person to be able to enter a string in the locality field and it take it as an "equals" query. Or include a wild card at the start or end to take it as a like and apply the wild card characters.
At the moment it achieves half of that, but not as user friendly as one might hope... I see what I want to happen all the time, but of course I'm not sure how exactly it is done...
|
|
|
|
|
When the query is a string built in a programming language (say C#), then this[^] seems to show the way. Basically, apply the wildcard characters (if you want them, this might depend on a CheckBox being checked) to the parameter before you set up the SQLParameter, then write your query without any quotes.
|
|
|
|
|
Add the percent signs to the parameter value.
parm.Value = "%" + foo + "%"
|
|
|
|
|
Joe Stansfield wrote: If I enter R%%, I will get all records.
And if you enter R%%% do you get no records?
Via SQL Server 2005 directly I am unable to replicate what you are seeing.
Joe Stansfield wrote: I am using C# Visual Studio 2008 and the dataset query designer.
It is possible that the latter is the problem.
In Server 2005 (not the designer) I get exactly or at least close to the behaviour you describe when I use the underscore rather than the percent sign. Especially when you try the different input values.
Even if the SQL created excluded the percent signs from your where clause when you entered them manually it should still work. Instead with a single one you get only a single match. With two you get a two character match. That suggests the underline (single character match.)
Presuming there isn't some other assumption that is wrong - such as that you are actually running some other query because this one didn't get applied.
Simple test. Remove the percent entirely from the expression. Then test with input values: R, R_ and R%.
Those should give different results. If not then I would suspect that there is some other assumption that is causing the problem.
|
|
|
|
|
Sorry about the delayed reply - christmas / new year break over here in sunny Australia!
I tried all of the things above, and get identical results when using R% as I do R_. I have found that when using R%% I do get all records, but I have since discovered this is only because the maximum length of a record is 3 characters. I get the same result if I use R%%%.
However, when I use the wildcard part of the query on the Locality choice, it works as I would expect it too, and returns all results that start with, say, 'Hob', if I enter Hob%.
The only difference I can think of between the two is that Locality is stored as 'NVARCHAR(50)' and Land_Use_Code as 'NCHAR(5)'. I would not have thought this would cause a problem, but I may be wrong!
In the mean time I will work around it and add multiple %% signs to the parameter before the query is executed. Unfortunately I have tried to get my head around writing parametrized queries from scratch, but have not been able to find a good resource that explains it along with using it to then fill a DataGridView.
Thanks for all your help though guys - has been great and I feel like I have learnt a lot!
|
|
|
|
|
If (Decrypt_TextBox.Text = "") Then
MessageBox.Show("Need to put in the file location or select file in grid for decryption")
Else
'declaring string for file'
Dim filename As String
'Getting the file name that is selected'
filename = getFilename()
'Starting to abstract the data from the database'
database.ConnectionString = "Data Source = test.db3;"
database.Open()
Dim command As SQLite.SQLiteCommand
Dim sqlstatement As String
command = database.CreateCommand
'The following will get the bytes of the file'
Dim filesize As Integer
sqlstatement = "SELECT FileSize From Vault WHERE FileName=@file;"
command.CommandText = sqlstatement
command.Parameters.AddWithValue("@file", filename)
Dim reader As SQLite.SQLiteDataReader
reader = command.ExecuteReader
filesize = reader.GetValue(0)
reader.Close()
Dim data(filesize) As Byte
'The following should get the data'
sqlstatement = "SELECT FileData FROM Vault WHERE FileName=@file;"
command.CommandText = sqlstatement
command.Parameters.AddWithValue("@file", filename)
reader = command.ExecuteReader
I have encrypted files in this database. I need to extract this file and decrypt it. I've been thinking about using a memory stream but these files will get fairly large (2GB). This is as far as I've gotten using vb .net framework 2.0. Any help is helpful?
|
|
|
|
|
where do the files originate, how did they get encrypted, how did they enter the database?
would retrieving them not be pretty much the reverse process?
and why is it you store them in the database to begin with? I wouldn't store gigablobs, I'd use files and store the file's path in the db.
|
|
|
|
|
At first its encrypted using SHA1 as a file and stored into the database. The problem is that sqlite libraries doesn't give me a clear picture on how to extract the file. After further research and bouncing ideas of people, they have suggested binarystream and it goes make since but i don't know how it will the end will be just yet.
|
|
|
|
|
I expect one uses a byte array to write or read a blob field; and a stream to encrypt or decrypt; so basically reading from the database and decrypting should be pretty much the same as encrypting and writing to the database. If the latter works for huge files, so should the former.
Suggestion: try and solve the problem without any encryption/decryption first; then add them in.
|
|
|
|
|
Still don't how to extract the file either way.
|
|
|
|
|
I have no experience with SQLite.
However I did search CodeProject articles for SQL read BLOB and found a lot, including this: Reading and Writing BLOB Data to Microsoft SQL or Oracle Database[^]. I'm sorry the examples are in C#, that does not change the philosophy, it merely changes the syntax of things.
I suggest you read it, or some of the other hits, and try what you learn. I once more stress the fact that IMO writing to a BLOB field and reading from a BLOB field are bound to be very similar.
|
|
|
|
|
I agree with you but now I'm having problems trying to convert the file back to its original form.
|
|
|
|
|
I have a database table that consists of an int primary key, several unique identifiers (most of which are used to reference data in other databases), an xml field and an infoset. I have non-unique, non-clustered indexes that I am trying to use to speed up the queries to the table, all of the indexes work well except for a single index on a uniqueidentifier field that retains a 99% fragmentation rate regardless of rebuild/reorganizing the index. Below is the details of the table, IX_external_guid_3 is the index with the high fragmentation. Can anyone tell me what might cause the high fragmentation, or what I can do to reduce the fragmentation on this guid field and speed up queries based on it?
Thanks,
Table
id(PK, int, not null)
guid(uniqueidentifier, null, rowguid)
external_guid_1(uniqueidentifier, not null)
external_guid_2(uniqueidentifier, null)
state(varchar(32), null)
value(varchar(max), null)
infoset(XML(.), null)
created_time(datetime, null)
updated_time(datetime, null)
external_guid_3(uniqueidentifier, not null)
FK_id(FK, int, null)
locking_guid(uniqueidentifer, null)
locked_time(datetime, null)
external_guid_4(uniqueidentifier, null)
corrected_time(datetime, null)
is_add(bit, not null)
score(int, null)
row_version(timestamp, null)
Indexes
PK_table(Clustered)
IX_created_time(Non-Unique, Non-Clustered)
IX_external_guid_1(Non-Unique, Non-Clustered)
IX_guid(Non-Unique, Non-Clustered)
IX_external_guid_3(Non-Unique, Non-Clustered)
IX_state(Non-Unique, Non-Clustered)
John
modified on Tuesday, December 21, 2010 2:57 PM
|
|
|
|
|
I've had a similar problem to this before, rebuilding or reorganizing wouldn't make a difference.
I ended up dropping the index and recreating it.
|
|
|
|
|