Got no answer on related thread, so i make this question. I've been searched for how to retrieve records value using where clause with multiple values and i got this.
table example :
|ID |PRICE|
|1 |3000 |
|2 |2000 |
|3 |1000 |
|4 |5000 |
|5 |4000 |
SQL query :
DM.Zread.Close;
DM.Zread.SQL.CommaText := 'select PRICE from DVD where ID in (1, 2, 3)';
DM.Zread.Open;
Above gave me an error, when i only put one 1 values which is (1) or (2) it's works fine.
Questions are :
how to straight it, so i could get the values from 3 different
records ?
how to apply it on string values instead ?
SQL is a TStrings subclass. When you set CommaText using the above, you are actually setting your query to:
select PRICE from DVD where ID in (1
2
3)
This obviously won't work.
You want to set the Text property or use Add() method to add separate lines.
Try using CommandText rather than CommaText on your SQL call
DM.Zread.Close;
DM.Zread.SQL.CommaText := 'select PRICE from DVD where ID in (1, 2, 3)';
DM.Zread.Open;
DM.Zread.SQL.CommandText := 'select PRICE from DVD where ID in (1, 2, 3)';
Related
I am an absolute beginner in Bigquery and SQL so apologies if this is a dumb question. I have a bigquery table like this
|Name|Value1|Value2|Value3|Value4|Value5|Value6|
|Ben |19 |45 |null |19 |13 |null |
|Bob |34 |null |12 |null |45 |43 |
My query only selects one row that matches the name in Name column. I want the result to only display columns that have non null values. For example if I do
SELECT * FROM mytable WHERE Name = "Bob"
I want the result to look like
|Name|Value1|Value3|Value5|Value6|
|Bob |34 |12 |45 |43 |
Similarly, if I select for Ben I want the result to look like
|Name|Value1|Value2|Value4|Value5|
|Ben |19 |45 |19 |13 |
I have tried SELECT IF but don't seem to get the syntax right.
You cannot select a variable amount of columns, but you may be able to create a SQL, with a combination of aggregate/pivot functions. You may be spending more time than it's worth trying to do it. I spend about two hours on the documentation, and I still feel almost clueless (If doesn't help that I don't have an account there, and my own database does not have the same exact functions).
See Google's BigQuery Documentation for examples.
I think you may be able to do it with UNNEST() and ARRAY(), but you'll lose the original column header information in the process.
I doubt if it can be achieved, because any SQL statement will act on record(s),i.e various columns, so if a column is null, it will affect all columns in the record that are to be retrieved. SQL STATEMENTS RETRIEVE ROWS(COLUMNS REFERENCED)
You can not do that dynamically in SQL. If you need a query like that you could create it manually but it depends on the results you want to achieve.
In the case you showed for example, the query below would work but you would lose the table's header reference.
SELECT value1,value2,value4,value5 FROM mytable WHERE value3 IS NULL AND value6 is NULL
UNION ALL
SELECT value1,value3,value5,value6 FROM mytable WHERE value2 IS NULL AND value4 is NULL
In this example it's possible to see that this kind of query is complicated to build if you have many conditions. Besides that, UNION ALL will always need the same number of columns in each separate query to work. If you need to create a generic query to do that, it's not gonna be possible.
I hope it helps
I am currently learning SQL and have done a sub query on a database and now want to take the highest version number. But have found out you can't use max on the subquery
Day|Version
---+-------
1 |1
1 |1
1 |2
1 |2
Along with a bunch of other data in the row. I want to select the rows with the highest version number.
Any suggestions? First time poster so sorry for the poor formatting
Cheers
Andrew
In access I have an update function where I can update information in an inventory database 1 at a time. I enter the Item name and I able to update the cost as well as a date.
Is there a way to write an sql Query to perform an update for 200 unique items?
EDIT:
I have
|ITEM NAME|ITEM COST|DATE CHANGE|
|A |$2.00 |1/1/1111 |
|B |$3.50 |1/2/1111 |
|C |$4.50 |1/3/1111 |
Let's say there are over 200 item names, I'd want to run a query to keep the item name but update the prices and date
|ITEM NAME|ITEM COST|DATE CHANGE|
|A |$3.00 |1/4/1111 |
|B |$1.50 |1/5/1111 |
|C |$84.50 |1/6/1111 |
I feel the only way to do it is to just do one long update, but I don't know if there is something better to do that.
I'm not sure if you mean that there's duplicate rows.. But I hope this could help by using the DISTINCT keyword from SQL. So if you were to write an update sql query it would be something like this:
UPDATE Accomodation
SET AccomodationName = "YourChoice"
WHERE AccomodationPrice = (SELECT DISTINCT AccomodationPrice
FROM Accomodation)
For more information about distinct, you can see here.
With a data table called Inventory and another table, Updates, with the same structure containing the 200 or so changes, the update would look like
UPDATE Inventory INNER JOIN Updates ON Inventory.[Item Name] = Updates.[Item Name] SET Inventory.[Item Cost] = [updates].[item cost], Inventory.[Date Change] = [updates].[date change];
I have a table that contains a column with comma separated values. I need to separate those values into new rows. Table looks like this :
ID| DATE|Value|Feed|
1|10-10-2014|5.00 |1,3,4
2|10-11-2014|21.00|1
54|01-15-2015|8.24 |2,15
1|02-22-2015|5.14 |1,3,4
And I need to break it out to :
ID| DATE|Value|Feed|
1|10-10-2014|5.00 |1
1|10-10-2014|5.00 |3
1|10-10-2014|5.00 |4
2|10-11-2014|21.00|1
54|01-15-2015|8.24 |2
54|01-15-2015|8.24 |15
1|02-22-2015|5.14 |1
1|02-22-2015|5.14 |3
1|02-22-2015|5.14 |4
So I believe I need a to write a table valued function but I'm having a difficult time figuring out where to start.
Any guidance would be great.
Thanks.
You are going to need the ID so that you can join back to your table, but here is some help for the Split function https://stackoverflow.com/a/10914602/3854195
EDIT: (How to use a cursor to combine the results from the Split function with your source table)
I setup this SQL Fiddle
We are facing a weird problem with one of our query.
Below is the query we are running
INSERT into test
SELECT
member.name as mem_name,
CASE WHEN ( member.dob>0 AND length (member.dob)=8 ) THEN (DATEDIFF(year,to_date("dob",'YYYYMMDD'), to_date(20140716,'YYYYMMDD'))) WHEN ( member.dob=0 ) Then 0 END As Age,
20140716021501
FROM
member
Below is the sample data present in our table.
|name |dob
|Ajitsh |0 |
|rk |51015 |
|s_thiagarajan |19500130 |
|madhav_7 |19700725 |
|1922 |0 |
|rekha |25478 |
|vmkurup |0 |
|ravikris |19620109 |
|ksairaman |0 |
|sruthi |0 |
|rrbha |19630825 |
|sunilsw |0 |
|sunilh |0 |
|venky_pmv |19701207 |
|malagi |0 |
|an752001 |0 |
|edsdf |19790201 |
|anuanand |19730724 |
|fresh |19720821 |
|ampharcopharma |19590127 |
|Nanze |19621123 |
The date of birth is stored in bigint as YYYYMMDD format.
In the data there are some rows, in which date is invalid like 0, 51015.
On some instances this query raises the following error.
INSERT INTO test not successful
An error occurred when executing the SQL command:
INSERT into test
SELECT
member.name as mem_name,
CASE WHEN ( member.dob>0 AND length (member.dob)=8 ) THEN (DATEDIFF(y...
ERROR: Data value "0" has invalid format
Detail:
-----------------------------------------------
error: Data value "0" has invalid format
code: 1009
context: PG ERROR
query: 92776
location: pg_utils.cpp:2731
process: query1_30 [pid=1434]
-----------------------------------------------
Execution time: 3.99s
1 statement failed.
But the strange thing is, it raises the error randomly and not all the time.
Many times it works without any change in query or dataset.
Sometime it also works in second or third attempt.
My doubt is that to_date function is giving this error. But why randomly
and not gives error on every run.
To support my assumption I also tried this small query.
SELECT to_date(20140716,'YYYYMMDD'), to_date(0,'YYYYMMDD');
But this also creates the same scenario. It raises error randomly, while
runs smoothly rest of the times.
If is it fine to ignore this type of values and just convert this to Date format you can follow the below way.
SELECT to_date('20140716','YYYYMMDD'), to_date('0','FMYYYYMMDD');
Here FM suppresses leading zeroes and trailing blanks that would otherwise be added to make the output of a pattern be fixed-width.