Is there a SQL select query with following where clause? - tsql

Table with Records
ID Column
1 Java 8 Update 131 (64-bit)
2 Java 8 Update 161
3 Java Auto Updater
4 Java SE Development Kit 8 Update 102
5 Java(TM) 6 Update 12
6 shared.tp.aurora.ooc.java.bundle-4.0-core-nu
7 olap.oda.api.java-4.0-en-nu
8 repoaccess.container.java-4.0-core-nu
Select Records having "Java Update" only with ID's 1,2,3,4,5
Tried
Select ID, Column
where column like '%Java%'
Select ID, Column
where column like '%Java Update%'
Select ID, Column
where column like '%Java*Update%'

Using an AND:
SELECT ID, Column
FROM YourTable
WHERE column like '%Java%' AND column like '%Update%'

SELECT ID, Column
FROM YourTable
WHERE column like 'Java%Update%'
Assuming (not a good thing) you want all rows with Column value that starts with 'Java' and contains 'Update' somewhere after 'Java' - the above will help.

Related

count(1) and distinct behaviour in postgres

Imagine a table:
name age
John 20
Sam 60
Dave 30
John 15
I want to check count of distinct names, I query the table like so:
SELECT COUNT(1), DISTINCT(name)
FROM table
GROUP BY 2
But I get:
ERROR: syntax error at or near "DISTINCT"
Position: 18
But when I use:
SELECT DISTINCT(name), COUNT(1)
FROM table
GROUP BY 1
I do get what's expected:
John 2
Sam 1
Dave 1
Is there a reason why the first query is not working or am I making a mistake somewhere?
The distinct here is not required. GROUP BY means 'group by a distinct set of values'
so
SELECT COUNT(*), name
FROM table
GROUP BY name;
Will give you the result I think you want.

Postgres column to have Autoincrement feature based on values on another key column

I have a requirement where i need to design the table in postgres, where one of the column needs to have autoincrement feature. But the autoincrement should be based on the value on another column.
Table
Column A Column B
100 1
101 1
102 1
102 2
102 3
column A and Column B are the keys to the table. Now if i insert another row, with Column A equated as 100, then column B needs to auto populate as 2. If i attempt to insert value 102 into column A, then column B needs to populate on its own as 4.
Is there a way i can set an attribute to column B, during table creation?
Thanks
Sadiq
#AdrianKlaver is correct. You should use a timestamp and actually record when the version was saved. If you want the version number then you can generate it with the Window row_number function when queried.
select column_a
, row_number() over (partition by column_a
order by column_a,column_a_ver_ts) column_b
from table_name;
Alternative you could use the above query and create a view. See Fiddle

Postgresql 11 How can I reorder the physical rows in the database according to a timestamp

I have a table called streams which has several rows, I like to change the order of the physical rows in the database according to the created_on column . I have a total of 179 rows, row (id) 179 is supposed to have the latest created_on date, then row (id) 178 should have the second newest created_on date . As you can see from the small snippet below things are not in order for example the date on id 172 is newer than the one on id 179 . I could get the correct data by doing this
select * from streams order by created_on desc
however they want it to search by id such as this
select * from streams order by id desc
The following query above produced the results below . We had everything working correctly but we did a migration from Postgres 10 to Postgres 11 and messed up on the order of things.
You can re-generate the id column values with an UPDATE such as the following:
UPDATE streams SET id = tmp.row_id
FROM (SELECT id, row_number() OVER (ORDER BY created_on, id) AS row_id FROM streams) AS tmp
WHERE streams.id = tmp.id;
After that, you might want to re-order the rows physically on disk using CLUSTER:
CLUSTER streams USING <index-for-id-column>;

Export data from db2 with column names

I want to export data from db2 tables to csv format.I also need that first row should be all the column names.
I have little success by using the following comand
EXPORT TO "TEST.csv"
OF DEL
MODIFIED BY NOCHARDEL coldel: ,
SELECT col1,'COL1',x'0A',col2,'COL2',x'0A'
FROM TEST_TABLE;
But with this i get data like
Row1 Value:COL1:
Row1 Value:COL2:
Row2 Value:COL1:
Row2 Value:COL2:
etc.
I also tried the following query
EXPORT TO "TEST.csv"
OF DEL
MODIFIED BY NOCHARDEL
SELECT 'COL1',col1,'COL2',col2
FROM ADMIN_EXPORT;
But this lists column name with each row data when opened with excel.
Is there a way i can get data in the format below
COL1 COL2
value value
value value
when opened in excel.
Thanks
After days of searching I solved this problem that way:
EXPORT TO ...
SELECT 1 as id, 'COL1', 'COL2', 'COL3' FROM sysibm.sysdummy1
UNION ALL
(SELECT 2 as id, COL1, COL2, COL3 FROM myTable)
ORDER BY id
You can't select a constant string in db2 from nothing, so you have to select from sysibm.sysdummy1.
To have the manually added columns in first row you have to add a pseudo-id and sort the UNION result by that id. Otherwise the header can be at the bottom of the resulting file.
Quite old question, but I've encountered recently the a similar one realized this can be achieved much easier in 11.5 release with EXTERNAL TABLE feature, see the answer here:
https://stackoverflow.com/a/57584730/11946299
Example:
$ db2 "create external table '/home/db2v115/staff.csv'
using (delimiter ',' includeheader on) as select * from staff"
DB20000I The SQL command completed successfully.
$ head /home/db2v115/staff.csv | column -t -s ','
ID NAME DEPT JOB YEARS SALARY COMM
10 Sanders 20 Mgr 7 98357.50
20 Pernal 20 Sales 8 78171.25 612.45
30 Marenghi 38 Mgr 5 77506.75
40 O'Brien 38 Sales 6 78006.00 846.55
50 Hanes 15 Mgr 10 80659.80
60 Quigley 38 Sales 66808.30 650.25
70 Rothman 15 Sales 7 76502.83 1152.00
80 James 20 Clerk 43504.60 128.20
90 Koonitz 42 Sales 6 38001.75 1386.70
Insert the column names as the first row in your table.
Use order by to make sure that the row with the column names comes out first.

distinct key word only for one column

I'm using postgresql as my database, I'm stuck with getting desired results with a query,
what I have in my table is something like following,
nid date_start date_end
1 20 25
1 20 25
2 23 26
2 23 26
what I want is following
nid date_start date_end
1 20 25
2 23 26
for that I used SELECT DISTINCT nid,date_start,date_end from table_1 but this result duplicate entries, how can I get distinct nid s with corresponding date_start and date_end?
can anyone help me with this?
Thanks a lot!
Based on your sample data and sample output, your query should work fine. I'll assume your sample input/output is not accurate.
If you want to get distinct values of a certain column, along with values from other corresponding columns, then you need to determine WHICH value from the corresponding columns to display (your question and query would otherwise not make sense). For this you need to use aggregates and group by. For example:
SELECT
nid,
MAX(date_start),
MAX(date_end)
FROM
table_1
GROUP BY
nid
That query should work unless you are selecting more columns.
Or maybe you are getting the same nid with a different start and/or end date
Try distinct on:
select distinct on (col1) col1, col2 from table;
DISTINCT can't result in duplicate entries - that's what it does... removed duplicates.
Is your posted data is incorrect? Exactly what are your data and output?