I am totally frustrated with the SQL recently. The bad guy is COMPUTE clause.
Since I am making a migration project from MSSQL 2000 server to Oracle 10i Server, a lot of SQL queries in the applications should be modified to meet the Oracle SQL requirements. What I found in the internet is:
---
> The Compute by clause of MSSQL basically allows you to get a running
> total at the bottom (end) of the report.
> In a way it is similar then using ".. group by .." with aggregate
> functions (sum) but in this case I am not trying to "... group by .."
> does not make sense in the context of the query, just want to get a
> summary (sum and count) of some columns at the end of the record.
The "standard" way to do this is to make a second query to compute the aggragates. However it is possible to combine the two if you really need the aggregates in the same result set.
> > > select A.ProdID, A.Description, A. Qty, A.Price
> > > from SoldItems as A
> > > where A.ListID = 15
> > > order by A.ProdID
> > > compute count(A.ProdID),sum(A.Price),sum(A.Qty)
SELECT ProdID, Description, Qty, Price
FROM
(SELECT A.ProdID, A.Description, A.QTY, A.Price, 1 AS Kind
FROM SoldItems AS A
WHERE A.ListID = 15
UNION ALL
SELECT count(B.ProdID), NULL AS Description, sum(B.Price), sum(B.Qty),
2 AS Kind
FROM SoldItems AS B
WHERE B.ListID = 15
) AS C
ORDER BY Kind, ProdID
;
---
After that I must stil fight with SHAPE clause... What a life!
Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts
2008-02-28
2008-01-07
Cron job with the log file
Today I need to write a cron job to make some sql queries automatically executed. Also a log file with the date is necessary according to my boss. :-)
So here is the solution:
---------------------------------------------------------------
# Date in Format yyyymmdd_hhmmss:
SQLNAME=/home/sybase/logs/table_`date +%Y%m%d`_`date +%H%M%S`.sql
# Every day at 23 o'clock the sql query will be executed
00 23 * * * root /opt/sybase/SYBSsa9/bin/dbisql -nogui -c "eng=enginename;dbn=dbname;uid=user;pwd=password;" /opt/sybase/SYBSsa9/bin/whatever.sql >> $SQLNAME
---------------------------------------------------------------
Some explainations:
So here is the solution:
---------------------------------------------------------------
# Date in Format yyyymmdd_hhmmss:
SQLNAME=/home/sybase/logs/table_`date +%Y%m%d`_`date +%H%M%S`.sql
# Every day at 23 o'clock the sql query will be executed
00 23 * * * root /opt/sybase/SYBSsa9/bin/dbisql -nogui -c "eng=enginename;dbn=dbname;uid=user;pwd=password;" /opt/sybase/SYBSsa9/bin/whatever.sql >> $SQLNAME
---------------------------------------------------------------
Some explainations:
- SQLNAME is a variable name. It consist two parts, the normal text and the date. Notice to that, please always enclose the date part in backticks ( ` ) , otherwise it will be not executed. This variable can be used later with the format $SQLNAME
- dbisql is the command line tool for Sybase. In whatever.sql you can write the sql queries you need.
2008-01-04
Sybase Anywhere SQL
Sybase Anywhere provides a possibility, which different clients with different date sources can be synchonised with a central DB server. Its manual is here: http://manuals.sybase.com/onlinebooks/group-pbarc/conn5/sqlug/
The SQL of Sybase is a little different from the standard SQL, for example:
>> Clearing a table "dummy"
delete dummy;
>> Output to a file
select * from dummy;
output to 'dummyfile.sql'
More helpful commands can be found at [selectorweb]
The SQL of Sybase is a little different from the standard SQL, for example:
>> Clearing a table "dummy"
delete dummy;
>> Output to a file
select * from dummy;
output to 'dummyfile.sql'
More helpful commands can be found at [selectorweb]
2007-08-09
The confusing difference beweteen different JOINs in SQL
There are two kinds of JOINs: one is INNER JOIN, the other is OUTER JOIN.
Sounds like similar, but very different:
INNER JOIN means the result of a query should exist in the source tables.
OUTER JOIN means the result of a query CAN be some NULLs partially.
INNER JOIN is a keyword of SQL, but OUTER JOIN not.
OUTER JOIN includes two types JOINs: one is LEFT JOIN, the other is RIGHT JOIN,
NOTICE: LEFT JOIN & RIGHT JOIN are keywords of SQL.
Now let's look at some examples to understand things more clearly.
Given two tables:
TABLE 1: exam
ID Score
------------------------
1 A-
2 B+
5 C-
TABLE 2: student
ID Name
------------------------
1 Joe
5 Geogre
7 Ben
First SQL query:
SELECT e.ID, s.Name, e.Score FROM exam AS e INNER JOIN student AS s ON s.ID=e.ID;
So we get:
ID Name Score
-----------------------------
1 Joe A-
5 Geogre C-
In fact, this query is equaled to:
SELECT e.ID, s.Name, e.Score FROM exam AS e, student AS s WHERE s.ID=e.ID;
Second SQL query:
SELECT e.ID, s.Name, e.Score FROM exam AS e LEFT JOIN student AS s ON s.ID=e.ID;
We should get the following this time:
ID Name Score
-----------------------------
1 Joe A-
2 NULL B+
5 Geogre C-
Third SQL query:
SELECT e.ID, s.Name, e.Score FROM exam AS e RIGHT JOIN student AS s ON s.ID=e.ID;
We should get the following this time:
ID Name Score
-----------------------------
1 Joe A-
5 Geogre C-
7 Ben NULL
So you find the difference!
LEFT JOIN will try to find every match for the table BEFORE the statement LEFT JOIN, when nothing matched can be found, then a NULL will be used.
RIGHT JOIN will try to find every match for the table AFTER the statement RIGHT JOIN, when nothing matched can be found, then a NULL will be used.
Sounds like similar, but very different:
INNER JOIN means the result of a query should exist in the source tables.
OUTER JOIN means the result of a query CAN be some NULLs partially.
INNER JOIN is a keyword of SQL, but OUTER JOIN not.
OUTER JOIN includes two types JOINs: one is LEFT JOIN, the other is RIGHT JOIN,
NOTICE: LEFT JOIN & RIGHT JOIN are keywords of SQL.
Now let's look at some examples to understand things more clearly.
Given two tables:
TABLE 1: exam
ID Score
------------------------
1 A-
2 B+
5 C-
TABLE 2: student
ID Name
------------------------
1 Joe
5 Geogre
7 Ben
First SQL query:
SELECT e.ID, s.Name, e.Score FROM exam AS e INNER JOIN student AS s ON s.ID=e.ID;
So we get:
ID Name Score
-----------------------------
1 Joe A-
5 Geogre C-
In fact, this query is equaled to:
SELECT e.ID, s.Name, e.Score FROM exam AS e, student AS s WHERE s.ID=e.ID;
Second SQL query:
SELECT e.ID, s.Name, e.Score FROM exam AS e LEFT JOIN student AS s ON s.ID=e.ID;
We should get the following this time:
ID Name Score
-----------------------------
1 Joe A-
2 NULL B+
5 Geogre C-
Third SQL query:
SELECT e.ID, s.Name, e.Score FROM exam AS e RIGHT JOIN student AS s ON s.ID=e.ID;
We should get the following this time:
ID Name Score
-----------------------------
1 Joe A-
5 Geogre C-
7 Ben NULL
So you find the difference!
LEFT JOIN will try to find every match for the table BEFORE the statement LEFT JOIN, when nothing matched can be found, then a NULL will be used.
RIGHT JOIN will try to find every match for the table AFTER the statement RIGHT JOIN, when nothing matched can be found, then a NULL will be used.
Subscribe to:
Posts (Atom)