I have a SQL query like this
What I want is showing one row and adding columns dynamically
Like this:
How can I do that?
you must using pivot query.
SELECT cotation_study_uid, contation_label,[OMNIPAQUE 350*50ML] AS col1, [Crane TDM] AS col2, [Annulation produit] AS col3
(SELECT *
FROM yourtable)p
PIVOT (SUM(contation_prix) FOR cotation_label IN ('OMNIPAQUE 350*50ML', 'Crane TDM', 'Annulation produit'))
Related
I am querying an Oracle DB using Oracle SQL Developer. The query is running slowly. I am away that using Distinct is likely slowing me down. But I haven't been able to produce unique rows without the Distinct call. The database contains no duplicate rows. So, it is likely that I malformed this query. How do I speed up this query? The query is as follows:
SELECT DISTINCT
view1.table1.col1,
view1.table2.col2,
view1.table2.col3,
view1.table1.col4,
view1.table3.col5,
view1.table4.col6
FROM
view1.table1,
view1.table3,
view1.table2,
view1.table4
WHERE
table1.col1 = table2.col7
AND
table1.col4 = table3.col8
AND
table1.col4 >= '19-DEC-20'
AND
table4.col9 = table3.col10 ```
table1: INDEX(col4, col1)
table3: INDEX(col8, col5, col10)
table2: INDEX(col7, col2, col3)
table4: INDEX(col9, col6)
I am working with some SQL queries on DB2. Is it possible to select all the columns in a table and also specify certain conditions using the "as" keyword within that select statement? For example, is this query possible:
select
*,
col1 + col2 as sum1,
col3 - col4 as dif1
from
table;
Whenever I attempt this, I am getting the SQL0104 error and it is saying "Token , was not valid. Valid tokens: FROM INTO".
Thank you for your help.
EDIT:
This query is running inside an SQLRPGLE program on an AS400.
Put your table alias in front of the *. So:
select
t.*,
col1 + col2 as sum1,
col3 - col4 as dif1
from
table t;
I'm trying to append two tables in MS Access at the moment. This is my SQL View of my Query at the moment:
INSERT INTO MainTable
SELECT
FROM Table1 INNER JOIN Table2 ON Table1.University = Table2.University;
Where "University" is the only field name that would have similarities between the two tables. When I try and run the query, I get this error:
Query must have at least one destination field.
I assumed that the INSERT INTO MainTable portion of my SQL was defining the destination, but apparently I am wrong. How can I specify my destination?
You must select something from your select statement.
INSERT INTO MainTable
SELECT col1, col2
FROM Table1 INNER JOIN Table2 ON Table1.University = Table2.University;
Besides Luke Ford's answer (which is correct), there's another gotcha to consider:
MS Access (at least Access 2000, where I just tested it) seems to match the columns by name.
In other words, when you execute the query from Luke's answer:
INSERT INTO MainTable
SELECT col1, col2
FROM ...
...MS Access assumes that MainTable has two columns named col1 and col2, and tries to insert col1 from your query into col1 in MainTable, and so on.
If the column names in MainTable are different, you need to specify them in the INSERT clause.
Let's say the columns in MainTable are named foo and bar, then the query needs to look like this:
INSERT INTO MainTable (foo, bar)
SELECT col1, col2
FROM ...
As other users have mentioned, your SELECT statement is empty. If you'd like to select more that just col1, col2, however, that is possible. If you want to select all columns in your two tables that are to be appended, you can use SELECT *, which will select everything in the tables.
Suppose you have two tables in MS Access, Table1 and Table2 with the same structure (i.e. the same columns). I would like to write a query where if Table1 is empty then I have to get the data from Table2 else get the data from Table1.
I am not sure how to use IIF statements in such a way.
Just an idea. Don't have MS-Access on my machine, but SQL Server accepts it:
SELECT Col1, Col2, ... FROM (
SELECT 1 AS TAG, Col1, Col2, ... FROM Table1
UNION
SELECT 2 AS TAG, Col1, Col2, ... FROM Table2) AS TBL_PARENT
WHERE TBL_PARENT.TAG = (SELECT MIN(TAG) FROM
(SELECT TOP 1 1 AS TAG FROM Table1
UNION
SELECT TOP 1 2 AS TAG FROM Table2) AS TBL)
Explanation
If Table1 is empty, the SELECT MIN(TAG) will return 2, otherwise 1. The WHERE clause will then filter out all the rows that belong to the desired table. The outer-most SELECT is only being used becuz SQL Server doesn't allow using column alias (TAG is an alias here) in WHERE clause. Thus we have to create a temporary TBL_PARENT, so that we could access TAG column.
Since you require a somewhat conditional setting and MS Access does not run trigger or transact SQL, consider using VBA in addition to a stored or VBA SQL query.
Of course figure out which form event trigger --OnOpen, OnClick, AfterInsert, AfterUpdate-- to place this in:
IF DCount("ID", "Table1") = 0 Then
'VBA Recordset Query
db.OpenRecordset Table2sSQLString, dbOpenDynaset
'Stored SQL Query
DoCmd.OpenQuery "Table2Query"
Else
'VBA Recordset Query
db.OpenRecordset Table2SQLString, dbOpenDynaset
'Stored SQL Query
DoCmd.OpenQuery "Table1Query"
End If
Just a quick question. I'm using single values manually inputed by the user and doing an SQL query comparing to two columns, like:
SELECT col3,col1,col4 FROM table WHERE
col1='SomeReallyLongText' OR col2='SomeReallyLongText'
The repetition of SomeReallyLongText is tolerable, but my program also supports looping through an Excel document with several hundred rows - which means I'll be doing:
SELECT col3,col1,col4 FROM table WHERE
col1 IN('item1','item2',...'itemN') OR col2 IN('item1','item2',...'itemN')
And the query would be exhaustively long, which I can't imagine is efficient.
Is there a way to shorten this so two columns can be compared to the same IN(xxx) set?
If not, are there other (more efficient) ways of giving the set of values in the query?
(I'm using C# with .NET 4.0 Client Profile, using Excel Interop to access the file)
I'm not too sure about the performance you'd get with this:
SELECT col3,col1,col4 FROM table
WHERE EXISTS (
SELECT 1
FROM (VALUES
('item1')
, ('item2')
, ...
, ('itemN')
) AS It(m)
WHERE It.m IN (col1, col2, ...)
)
You can create a temp table to store all the values used inside the IN clause
IF OBJECT_ID('tempdb..#Sample') IS NOT NULL DROP TABLE #Sample
Create table #Sample
(name varchar(20))
Insert into #Sample
values
('item1'),('Item2'),....
SELECT col3,col1,col4 FROM table WHERE
col1 IN ( Select name from #Sample) OR col2 IN(Select name from #Sample)
or if you are using Linq to SQL then you can store the excel data in collection and use Contains method to query the DB
var excelVal = new string[] { 'item1','item2'... };
var result = from x in Table
where excelVal .Contains(x.Col1) || excelVal.Contains(x.Col2)
select x;