tesuji 135 Master Poster

Can't complain. ODBC has only but a few flaws, it provides a great interface. You may need to use the ExecSQL() query. Another Link.

Ah, gracious nbaztec are you talking about something like that:

...
  rc = SQLAllocHandle( SQL_HANDLE_STMT, hDbc, &hStmt);
  SQLCHAR sqlStatement[] = "SELECT pName, convert(char(20), pDate, 104), quantity, salesprice " 
    "FROM products WHERE PID = ?;";
...
  prodID = prod_ID;
  rc = [B][U]SQLExecDirect( hStmt, sqlStatement, SQL_NTS )[/U][/B];
...
rc=SQLGetData(hStmt, 4, SQL_C_FLOAT, &oldSalesPrice, 0, 0); printf("\nSales price    %8.2f", oldsalesprice);
...

Interested in getting to know what the ... stand for? btw, with minor changes (e.g. date conversion) complete code runs on MS SQL server 2008, Sybase, Oracle, DD2, SQL Anywhere, PostgreSQL ...

-- tesu

tesuji 135 Master Poster

The ADO thing is very useful and is perfect..... But is ther anything which i can work on Linux???? Ubuntu....

On Linux there are two very simple methods: ESQL und ODBC. There is also a source forge project to develope ADO for Linux, though.

If one is using PostgreSQL on Linux (what seems to be obvious) there is no simpler way than ESQL, you might check the PostgreSQL's ESQL manual. However, I guess more than 80% of programmers are using ODBC on Linux.

Happy esqling!

-- tesu

tesuji 135 Master Poster

seriously, this *.cpp file includes only the following line...

int main{ return 0; }

N.B. I am in Codeblocks and i have changed the settings of linker as tesuji suggested.

>>> int main{ return 0; } ???


So what about this one:

int main(){return 0;}

tesu

tesuji 135 Master Poster

hi

if you want to select rows where duedate is from current year, you should extend where clause: WHERE ... AND YEAR(DatePaid) = YEAR(CURDATE()).

I am about to examine your select statements more exact this evening (just during watching world football championship). I have already got the funny feeling that something is going wrong concerning the logical insight of what aggregate functions are and how to use them practically. I'll answer if here is somewhat more clarity.

-- tesu

tesuji 135 Master Poster

Then I think I entirely miss the point tesuji is trying to make:

Why compare the 2?

>>> Why compare the 2 ?

akand said he would be able to read all data from the database in his program but he would not know how to store data from his program back to database. If so, he must know the select statement because reading data from database within a c program is done by select statement. To write data back to datbase needs UPDATE statement. So he just need to become acquainted with unknown UPDATE.

I hope these clears up my comparison. And indeed UPDATE statement is really really much more easier than SELECT statement.

-- tesu

tesuji 135 Master Poster

hi John

A single one-to-many relationship between entity A and B can be expressed by (crowfoot notation) A ------< B. That means the primary key of A is foreign key in B. If there simultaneously exists also a one-to-many relationship between entity B and A like B ------< A the relationship between A and B is many-to-many, like A ---< R >--- B.

In real world many-to-many relationships are quite usual, just consider entities student and course. A Student can attend more than one course, therefore we have student ----< course. A course usually is attended by more than one student, therefore we have course -----< student. Both one-to-many relationships make a many-to-many relationship: student ---< attend >--- course. Mapping this entity-relationship model to relational database we obtain three tables: student, course and a linking table attend, where the primary key of table attend must be form from the primary keys of table student and course.

-- tesu

tesuji 135 Master Poster

I have some data in a table in mysql.
I have written a program which can connect to the database and access all tables and data of mysql.
I have a variable in the C program. I want a particular data in one of the tables in the database to be stored in this variable...
can someone tell me how to retrieve this data and store it into the variable for some processing???

>>> I have written a program which can connect to the database and access all tables and data of mysql.

Which interface do you actually use, odbc, ado, ole db ?

If it is odbc within plain C (not c++) i can send you some examples how to get data from a sql table and store it back. This are examples once i wrote for MS SQL server 2008 and Sybase database. Because ODBC interface (use Version 3) is strictly standardized the call level interface is always the same no matter which database the c program is working with.

Btw, as you stated your program is already able to connect to database and obtain some data from a table, what means that you already know how to manage simple sql select statements, and the update statement is almost always simpler than select statement.

-- tesu

tesuji 135 Master Poster

Here is an example on how to group and order by year and month.

select year(orderDate) as Year, month(orderDate) as Month, 
 sum(quantity) as Quantity, sum(quantity*ItemPrize) as "Toal per Month" 
   from orderline join customerorder group by Year, Month order by Year, Month;

-- Result
/*
Year  Month  Quantity  Toal per Month
-------------------------------------
2009  10     36         7946.94
2009  11      3         2248.00
2009  12     32        17231.83
2010   1      3          192.00
2010   2      9         1365.95

Please consider that this result has been selected from a Sybase database. So possibly some details of the select need to be adjusted.
*/

If you decide to add a further column, for example say the item price, your above compact monthly settlement would be destroyed because itemprice must appear in group by clause.

I hope this example will be a little help.

Addition: FROM orderline JOIN customerorder is modern form of FROM orderline o, customerorder c where c.orderID = o.orderID. This only works if all primary and foreign keys are set up correctly.

-- tesu

tesuji 135 Master Poster

Hi andydeans,

still having problems?

Each select statement is incorrect, for example:

...
UNION SELECT c.ClientID, c.App1FirstName, c.App1LastName, u.FirstName, u.LastName, MONTHNAME(DatePaid) as MonthPaid, YEAR(DatePaid) as YearPaid, [B][U]sum(p.amount)[/U][/B] as sub_amt, DatePaid FROM (clients c JOIN users u on c.ClientUserID = u.UserID) JOIN mortgage p on c.ClientID = p.clients_ClientID WHERE DatePaid IS NOT NULL
...

Because of the aggregate function sum(...) a GROUP BY clause is absolutely necassary, where each column not appearing in aggregate function must be contained. Therefore you have to add

GROUP BY c.ClientID, c.App1FirstName, c.App1LastName, u.FirstName, u.LastName, MonthPaid, YearPaid

to the above SELECT and similar GROUP BY clauses to the other selects.

I did already express the necessity of GROUP BY several times if a select statement contains aggregate functions. You should carefully read and understand the example I showed in your other thread as somebody asked why that ORDER BY would have been that necessity.

Important: If a GROUP BY is necessary because of aggregate functions you must carefully decide which other columns should be listed in the select statement. If you choose too much, for example all columns of a table there wouldn't be any useful data to group by and therefore to sum up. In other words, the sum then only contains the single value of each row.

For example, you decide to sum up all sales amounts obtained per month: select product, month(date) as Month, sum (salesamount) from ... group by product, month order by month;

tesuji 135 Master Poster

Hi,
I had the same problem when I installed new codeblocks version 10.05 recently. I solved it by entering the complete path to kernel32.lib that way:

1. Identify kernel32.lib
on my computer, also running visual studio2008, I found complete path:
C:\Program files\Microsoft SDKs\Windows\v6.0A\Lib\kernel32.lib

2. on codeblocks v10 choose
Settings->Compiler and debugger-> linker setup
There ADD C:\Program files\Microsoft SDKs\Windows\v6.0A\Lib\kernel32.lib. I also added there odbc.lib to using odbc within c++

Both now work fine. There are no problems with new Visual Studio 2008. The same programs where I had had some trouble on codeblocks with kernel32.lib and odbc run perfectly there.

I hope this will also work on your codeblocks.

-- tesu

tesuji 135 Master Poster

Hi Borkoff

At first glance, your tables fulfill 3NF because
1) there aren't composed attributes (columns) nor repeating groups --> 1NF ok.
2) Also 2NF is fulfilled for 1NF is fulfilled and no column is functionally dependent on a subset of primary key columns. This is always true if the primary key only consists of one column.
3) And as far as I see there aren't any columns which transitively dependent on the primary key, obviously true for each of your table. So 3NF is also fulfilled, 2NF comprised.

As for the primary key of Procured_Products I think there exist a true natural key for this table is the result of the many-to-many relationship between products and suppliers. This primary key (Product_ID, Supplier_ID) is always necessary to define the many-to-many relationship (if it were omitted there would be some violation of normalization theory, I think so). Unfortunately, (Product_ID, Supplier_ID) isn't always unique, for example a product is to suppy repeatedly by same supplier. To solve this problem date or better timestamp should be included in PK.

-- tesu

tesuji 135 Master Poster

Hi

>>> if I remove the "employeeNumber into(11) NOT NULL," line completely the same error message returns for the next line.


Be that as it may, "into(11)" isn't a datatype --> int(11) seems to be correct.

-- tesu

tesuji 135 Master Poster

Hi Borkoff

In order to prove 3NF these pieces of Information are necessary and sufficient:

1. All involved tables (relations)

2. All columns (attributes) of those tables

3. Primary keys of all involved tables (necessary to prove 2NF and 3NF)

4. Semantics of the datasets (instances of relations) to determine functional dependencies

Usually 4th item is dropped tacitly for one often hasn't got enough instances when
starting design. Also this point isn't that important for homework, mostly.

I suggest that you repost enough data to fulfill first three items completely. That's an
absolute necessary condition for receiving any serious help. (Whether it is also
a sufficient condition depends on human beings goodness;))

-- tesu

tesuji 135 Master Poster

Hello rain2shine

Enormous task! Is this kind of homework, seems to be to big for it, so what else? What do you think of starting ERM design by yourself?

You will find good help here - on the basis of your first draft. If you don't already have a drawing program, I would suggest MySQL workbench (download older version, not that somehow imperfect beta vesion offered recently).

-- tesu

tesuji 135 Master Poster

Hi

>>> //This continues all the way to a[10], sum == 12

1. why not replacing them all by a single: a[sum]++; ?
2. did you really start with initializing a: for(i=0;i<12;i++)a=0; ?
3. using old compiler where int is 16 bit ?

-- tesu

tesuji 135 Master Poster

Sorry, this has been my mistake, I thought we were talking about mysql.
MS sql server has much more comprehensive system tables. You can get a good impression by downloading one of those great posters (such an originally colored poster of sql server 2008 is pinned on a wall in my office)

For example table sys.foreign_key_columns contains all foreign keys of a database..

-- tesu

tesuji 135 Master Poster

I see, all information on them can be found in MySQL's Information-schema tables (system tables, cataloge), for example, information on primary keys, foreign keys etc. is in table_constraints table.

select * from table_constraints;

Enough privileges?

-- tesu

tesuji 135 Master Poster

Hi tskellyfla

I immediately suggest, don't reinvent the wheel again. For your problem The GNU Multiple Precision Arithmetic Library is a MUST ;)

-tesu

tesuji 135 Master Poster

Hi andydeans

Still about solving your multi-select problem? I am sure that it can be solved in ways that I already ilustrated in which you should create a view consisting of 6 or 7 selects on your various products and aggregated them by UNIONs.

Once the view exists then totals depending on due dates and other grouping criteria can easily be generated by means of simple selects. You should really take into consideration my example given in: http://www.daniweb.com/forums/thread288573-2.html, it functions in this way. (I am doing such design tasks day in, day out on a truly sophisticated database having quite a few thousands tables.)

-- tesu

tesuji 135 Master Poster

Hi,
Aggregate functions cannot be used in WHERE clause directly. Instead you can apply HAVING clause to GROUP BY clause. Try this:

SELECT Table1.Name, Count(Table1.Name) AS NumNames 
  FROM Table1 GROUP BY Table1.Name HAVING NumNames=4;

This will select all occurrences of Table1.Name that appear four times in table1

btw, if you want to use aggregate functions in WHERE clause for some reason, you need to wrap them in UDFs. UDFs can freely be used in WHERE clause (at the cost of performance).

-- tesu

tesuji 135 Master Poster

Well, primary key, foreign key, not null, unique, check clause, also sometimes triggers
etc. are or may define constraints. So which of them are meant?

-- tesu

tesuji 135 Master Poster

I have a vector full of decimal values and I want to convert each element of the vector to ASCII represented strings (or char arrays). How do I do this? Each element of the array is 8 hex bits long and has been converted to it's decimal representation. So...

HEX(41424344) = DEC(1094861636) = ASCII('A','B','C','D')

I have the HEX to DEC part down fine but I can't quite figure out how to do the second part. I've tried using reinterpret_cast with many issues. Any simple solutions to this problem?

I feel like I can't make a conversion from my DEC stage to ASCII correctly because its bits 0 and 1 of the HEX that give char(0) and so on. With the dec representation there isn't a straightforward conversion is there?

Hi
If I understand you correctly, there are hexadecimal numbers (HEX(41424344)) given where each of them consists of 4 double-digit hex numbers. As for your example, unsigned int N = 0x41424344. Obviously you want to separate this 32-bit number into its 4 double-digit hex numbers 0x41, 0x42, 0x43, 0x44. This can easily be done by masking out each double-digit number and shifting the result to its lower right position to get the ordinal number for ASCII coding, for example:

unsigned hex = 0x41424344, mas = 0xff000000; char cha[4];
int j=0; while(hex>0){cha[j]=(hex&mas)>>24;j++;hex<<=8;}
for (j=0;j<4;j++) cout << cha[j] << "  "; //output: A  B  C  D

I thing your question should have been better posted to C forum for getting faster …

tesuji 135 Master Poster

Hi
seems to be a nice theory where you want to define a recursive relationship which is able to store trees and hierarchies. You should get acquaint with famous Joe Celko, the guy how wrote some important books only about this theory ---> google for Joe Celko's trees and hierarchies, and you probably get beatific then.

Also consider that Joe Celko ist the guy who stated the almost most important rule for designing relational databases:

"I call people who put a magical, universal ID-column with an auto-increment on all their tables ID-iots (by Joe Celko).

As for your example:

First create Table A:
create table a (ak int not null, x char, primary key (ak));

then table b which references itself recursively:
create table b (bk int not null, y char, af int not null, 
  primary key (bk, af), foreign key (af) references a on delete cascade);

-- tesu

tesuji 135 Master Poster

Addition to my prior posting (I am not further allowed to edit it for time seems to be expired):

For some special issues I also run wxDev-C++ 7.3.1.3 and code::Blocks 10.05. Both are really easily to handle but absolutely powerful. Because I also run it on Unix I myself now prefer code::blocks (the older version was awful). So it is a true relaxation working with them instead of getting somehow teased by using the Visual Studio.

-- tesu

tesuji 135 Master Poster

hi beaverkill,

I am just using MS Visual Studio 2008 (professional edition). Assuming that express edition doesn't differ from my edition that much, it is easy to include header files from directories other then your current project dir. I have a foreign language Visual studio here, I try to translate the steps into English:

1. Click Project and open the properties of your current project (ALT F7)

2. Open sub tree C/C++. Here you must select the first Entry named "Common settings" or something like that. This will show items like Warning level, debug information, Unicode etc. on the right-hand window which you can change.

3. The first Label, the first line, in this window is something like "Additional Include Directory". There you simply fill in the full path to the directory where your header file is located. For example: Headerfiles I often include in various projects are all centralized in directory d:\numeric\h. I only need to fill in this complete path in the box right-hand of the Label "Additional Include Directory".

4. Then I add #include "Matrices.h", which is located in d:\numeric\h, to the main cpp file and the class, functions, variables etc. of this distant header file are now accessible within my current project which is located elsewhere.


-- tesu

tesuji 135 Master Poster

To convert from 1 char to an int just subtract '0' from it. For example :

char ch = '1';
int i = ch - '0'; //i = int(1);

So just apply that to the whole array.

So all chars to be possible are numbers (0x30 .. 0x39) only ?

Why only should chars be converted into integers, aren't they already integers (0x00 ... 0xff) internally?

-- tesu

tesuji 135 Master Poster

To compute the median of discrete values continually read in, all values must be stored. (contrary to simple average where only running sum, current value and number are necessary). Also the list from where the median is to be taken must be sorted. So these steps are necessary to get the median of a sample:

1. Current read-in value must be inserted in the list (collection) in such a way that the list is ordered (usually ascending order)

2. Depending on whether the number of values is odd or even, the median is:
2.1 if odd, the median is the value taken from the center of the ordered list, e.g. list: 4,2,1,3,5; ordered: 1,2,(3),4,5; median: 3
2.2 if even, the median is the average of the two values taken from the center position of the ordered list. e.g. list: 4,2,6,1,3,5; ordered: 1,2,(3,4),5,6; median: (3+4)/2 = 3.5

It's clear that this is a current median which will change every time a new value is read.

//Pseudocode (assuming first value is stored in list[1], div means integral division):
  read v
  increment(n)
  insert (n, v, list)
  if (odd(n)) then median = list[1 + n div 2] 
     else median = (list[n div 2]+list[1 + n div 2]) / 2

-- tesu

tesuji 135 Master Poster

Hi

in JavaDB (same as Derby) columns which are foreign keys can be denoted by: CONSTRAINT NameOfConstraint REFERENCES refTable(PK_of_refTable), see Derby manual p.68. There are also conventional table-level foreign key constraints like: FOREIGN KEY (a, b, c) REFERENCES refTable (a, b, c), see Derby manual p.72.

btw, I think that the code fragment posted in by you should be more complete and should show your try of defining foreign keys at least.

-- tesu

tesuji 135 Master Poster

Sorry, in my above posting INSERT should be replaced by DELETE to refer to the original post! My given considerations concern to INSERT, UPDATE and DELETE though.

-- tesu

tesuji 135 Master Poster

"I am doing a simple INSERT into TABLE query.."
This is really not a good idea inserting 1M records that way for simple INSERT always checks primary keys, foreign keys, not null etc. This cannot be disabled. Contrary to BULK INSERT (see: http://msdn.microsoft.com/en-us/library/ms188365.aspx and http://msdn.microsoft.com/en-us/library/ms188267.aspx) where such checks aren't performed except you demand it explicitly.

-- tesu

tesuji 135 Master Poster

Oh yes itech7, you are in the right!

Today there are database systems which require that a single logical unit of work be surrounded by START TRANSACTION and COMMIT (or sometimes ROLLBACK). However, there are highly professional DBMS where START TRANSACTION is not mandatory, for example ORACLE, except for MySQL.

Nevertheless, the COMMIT (or sometimes ROLLBACK) after the above-mentioned INSERT statement is unambiguously required for any INSERT must always be part of a single logical unit of work unless you are a real gambler always playing Russian Roulette.

One should also think of that data inserted by INSERT without a final COMMIT will not be persistently stored in database (again, unless you are a real gambler always playing Russian Roulette).

-- tesu

tesuji 135 Master Poster

Hi

You should do this with BULK INSERT. There you have to choose an appropriate BATCHSIZE, possibly it's a good idea to split 1M records into some smaller portions. Also CHECK_CONSTRAINTS should NOT be used to prevent endlessly lasting checking of FK constraints and check clauses. If there is the possibility that you are inserting incorrect data then it's mostly a good idea to correct or remove these portions of data not until after the insertion task by invoking some sql statements.

You may also write a small program to round out plain mass data to complete BULK INSERT-statements.

-- tesu

tesuji 135 Master Poster

Hi
Karol has already enumerated all most important advantages of varchar over text DT, so it seems rather impossible to abandon varchar without good cause.

As for your five-column table: What do you further want to do with PK NoteID? Isn't there a true natural primary key given by triple (NoteUserID, NoteClientID, DateCreated). One should also taken into account that (NoteUserID, NoteClientID) obviously defines a many-to-many relationship between entities USERS and CLIENT.

-- tesu

tesuji 135 Master Poster

Addition: Delete statement must be finalized with COMMIT

-- tesu

tesuji 135 Master Poster

Thank you Dragon, I am still absolute beginner at English :(

-- tesu

tesuji 135 Master Poster

Hi
if i understand you correctly, you would like to delete all duplicate urls and only the url with the maximum pr value should remain.

/*
As for your example after deleting the duplicates the result set should then be:
url  pr
-------
a    6
b    6
c    6

The below code functions well on ms sqlserver and sybase:
*/
delete from mytable x from mytable x, mytable y
  where x.url = y.url and x.pr < y.pr;

In my case I had had some more columns to check whether duplicates exist. I took this old code and reduced the recursive references to only x and y to meet your requirement where only column URL exists containing duplicates. I hope MySQL has similar syntax and it will work. (You may also try on MySQL this: delete x from mytable x ... Furthermore, instead of second FROM clause MySQL possibly supports USING clause, so look at SQL Reference version 5.1...)

-- tesu

tesuji 135 Master Poster

Well, there might also be some ordinary mortals who GOT to do there profane programming homework when studying first-year computer sciences.

tesuji 135 Master Poster

hi mimis

num[i]=1;

// maybe if you replace this by

num[i]=0x31; //ascii coding for numeric 1 is hex 31 (third column, first row) or simpler num[i]='1';
 
// it will work

(didn't check rest of your code)

-- tesu

tesuji 135 Master Poster

Hi Ryan

Try this snippet:

int splitsum= 0; 
void splitadd(unsigned int n){
  int d = n % 10; n = n / 10; if ( n > 0) splitadd (n); splitsum += d;}

... 

// in main() add this: 
splitadd(12345); cout << "Result: " << splitsum << endl;  // Result: 15

-- tesu

tesuji 135 Master Poster

Well, no doubt :-/

One should consider some drawbacks of datatype text over varchar, for example attributes of datatype text as well as blobs will be stored separately from all other attributes (they are not stored within contiguous pages!), also if text attributes appear together with other attributes in a select statement, the resultset won't never be hold in main memory completely but be stored on disk. There are further limitations if attributes of text datatype be compared, sorted, manipulated with string functions etc.

So, if one doesn't need to handle more then 64k characters, why should he be burden with such unfavorable stuff? Therefore, varchar datatype is the far more appropriate datatype for dealing with such short notes, there is really no doubt whatsoever ;)

-- tesu

tesuji 135 Master Poster

hi andydeans,

I didn't forget you. The reason why I didn't respond earlier is I haven't as yet accessed to a MySQL database, only MS SQL Server and Sybase at hand to date. So I couldn't test the following sql statements.

CREATE OR REPLACE VIEW sumsum( Product, Client, Firstname, Lastname, Total) AS
  SELECT 'generalinsurance', c.clientsName, u.FirstName, u.LastName, sum(p.AmountGI) as sub_amt 
   FROM users u, clients c, generalinsurance p  
    WHERE u.UserID = c.ClientUserID AND c.CientID = p.Clients_ClientID  
	 AND DatePaidGI IS NULL 
	  GROUP BY c.clientsName, u.FirstName, u.LastName 
UNION
  SELECT 'protection', c.clientsName, u.FirstName, u.LastName, sum(p.amount) as sub_amt 
   FROM users u, clients c, protection p  
    WHERE u.UserID = c.ClientUserID AND c.CientID = p.Clients_ClientID  
	 AND DatePaid IS NULL 
	  GROUP BY c.clientsName, u.FirstName, u.LastName 
UNION
  SELECT 'buytolet', c.clientsName, u.FirstName, u.LastName, sum(p.amount) as sub_amt 
   FROM users u, clients c, buytolet p  
    WHERE u.UserID = c.ClientUserID AND c.CientID = p.Clients_ClientID  
	 AND DatePaid IS NULL 
	  GROUP BY c.clientsName, u.FirstName, u.LastName 
UNION
  SELECT 'mortgage', c.clientsName, u.FirstName, u.LastName, sum(p.amount) as sub_amt 
   FROM users u, clients c, mortgage p  
    WHERE u.UserID = c.ClientUserID AND c.CientID = p.Clients_ClientID  
	 AND DatePaid IS NULL 
	  GROUP BY c.clientsName, u.FirstName, u.LastName 
UNION
  SELECT 'referrals', c.clientsName, u.FirstName, u.LastName, sum(p.Commission) as sub_amt 
   FROM users u, clients c, referrals p  
    WHERE u.UserID = c.ClientUserID AND c.CientID = p.Clients_ClientID  
	 AND DatePaidReferral IS NULL 
	  GROUP BY c.clientsName, u.FirstName, u.LastName 
UNION
  SELECT 'overseas', c.clientsName, u.FirstName, u.LastName, sum(p.Commission) as sub_amt 
   FROM users u, clients c, overseas p  
    WHERE u.UserID = c.ClientUserID AND c.CientID = p.Clients_ClientID  
	 AND DatePaidOverseas IS NULL
      GROUP BY c.clientsName, u.FirstName, …
tesuji 135 Master Poster

Hi

Possibly you can start designing your triggers with oracle sql developer until they function properly. Then you can insert the final result in your Borland c++ program working together with BDE. By Acting this way you will feel certain that arising problems are only localized at turbo c++ or more likely at BDE.

Note btw, because this is a trigger fired every time a new record is inserted in table test which should insert a new generated sequence number, why not simply insert the sequence number in the original insert statement like in this way:

insert into test (id, ...) values (test_seq.NEXTVAL, ...);

which would do exactely the same task?

Note btw2: there is also a special Oracle database forum here, what seems to be a better place to post questions regarding oracle DB.

-- tesu

tesuji 135 Master Poster

hi andydeans

date or timestamp datatype is quite usual. If you want to group by month, you can apply month() or monthname() function, as I already stated in:

SELECT MONTHNAME(datePaid) as Mois, sum(amount) as monthly_Mortgage FROM mortgage
      WHERE DatePaid IS NULL GROUP BY Mois;

Recording year, month, day separately isn't that a good idea for one usually deals with complete date, also compact date is easier to handle within a program. If one occasionally needs elements of date-type attributes, applying appropriate functions is efficient.

-- tesu

tesuji 135 Master Poster

To avoid further misconceptions, so what about right shift operator >> ? Consider this program:

int main(int argc, char *argv[])
{  int i;
   i=0xff;       printf("%d    %d\n",i , i>>2); //  255    1020  (arithmetical right shift)
   i=0xffffff01; printf("%d    %d\n",i , i>>2); // -255     -64  (arithmetical right shift)
   return 1;
}

Binary representation of 0xff, 32 bit integers assuming, is (msb) 0000 0000 0000 0000 0000 0000 1111 1111 (lsb). Right-shifting this positive number by 2 places two things will happen:

firstly, 2 digits (11) are removed from lsb side
and
secondly, the original msb digit (0) is inserted twice on msb side, msb is short of "most significant bit".

The result is (msb) 0000 0000 0000 0000 0000 0000 0011 1111 (lsb) = 0x3f = dec 63. It's obviously that right-shift 1 place is equivalent to division by 2, and right-shifting 2 places means division by 4 such as 0xff>>2 = (dec 255/2 )/2=127/2=63.

There is an important fact to consider: In most programming languages the MSB codes the sign of integer numbers. So what will happen if we do a right shift on a negative integer number like the 2nd example in above program?

Binary representation of 0xffffff01 is (msb) 1111 1111 1111 1111 1111 1111 0000 0001 (lsb). Right-shifting this number 2 places will result in (msb) ??11 1111 1111 1111 1111 1111 1100 0000 (lsb) where 2 bits (01) were shifted out on lsb side but what to insert for (??) on …

tesuji 135 Master Poster

Hi sanagopi

I've just answered to your posting dealing with hexadecimal number 0xff where at least left shift operator has been annotated.

right-shift operator: that is kind of division by 2, and it functions analogously to left shift. You could understand both operators if you read my posting there.

-- tesu

tesuji 135 Master Poster

Hi tyson
I am glad to meet you here :) Let's given a table TuttiFrutti:

Fruits    	onStock		Location
--------------------------------------------
Apples          100			ABC
Oranges		300			BCA		
Plums		800			CBA
Oranges		200			BAC
Plums		100			CAB
Apples		150			ACB
...

Now consider these four select statements:

select fruits, locations, sum(onStock) from TuttiFrutti group by fruits, locations;
-- table TuttiFrutti will be produced completely for there is nothing to group by
select fruits, sum(onStock) from TuttiFrutti group by fruits; 
fruits		sum(onStock)
------------------------
Apples		250
Plums		900
Oranges		500
select sum(onStock) from TuttiFrutti;
sum(onStock)
------------
1650
select fruits, sum(onStock) from TuttiFrutti; --(won't never be executed on serious DBs)
-- Can not sum up apples and oranges?

Note btw, SQL-2003 Standard defined important expansions for group-by clause for doing OLAP, e.g. grouping sets, rollup, cube() which are very useful for creating complex queries.

I hope above examples do clarify something.

-- tesu

tesuji 135 Master Poster

Sorry Adak, I have to say that your answer is completely wrong.

The hexadecimal number 0xff stands for its decimal equivalent f*16 + f = 15*16 + 15 = 255. Therefore, the decimal value of 0xff is 255!

The numerical operator << does left shift. Each left shift by one place (<< 1) is identical with multiplication by 2. Therefore, << 2 makes a left shift by two places which is identical with multiplication by 4 (2 times 2).

If we consider that the length of int usually is 32 bits or 8 bytes, the variable i contains 000000ff or in binary (msb) 0000 0000 0000 0000 0000 0000 1111 1111 (lsb). By left shifting this binary number by two places we get (msb) 0000 ... 0011 1111 1100 (lsb). What additionally happened during left shift is that on the msb side two bits were dropped off and on the lsb side 2 zero-bits were inserted.

Now the result is 0x3fc. Its decimal equivalent is (3*16+f)*16 + c = (3*16+15)*16+12 =1020, which could also be reproduced by stepwise decimal multiplication of 2 times 2: 255 times 2 is 510, 510 times 2 is 1020.

I hope that this small "tutorial" could clarify some misconceptions.

-- tesu

tesuji 135 Master Poster

:( !!! ??? Are really all Egyptian people mad after speaking with a stammer by repeating those punctuation marks without any ceasing ??? !!!

-- tesu

tesuji 135 Master Poster

Meaning of "w" is:

Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.

If the file exists and you don't have correct permissions, fopen doesn't erase it and returns null pointer. If you don't have enough rights to create new file in current directory, fopen returns a null pointer (as Agni already stated).

-- tesu

tesuji 135 Master Poster

Well, I think what manutd4life is looking for is the run-length encoding algorithm, a simple algorithm for lossless data compression in which a sequence of the same character (Byte value) is stored as a single character (Byte value) following by its count, e.g. sequence aaaaaabbbc is RLE-coded by a6b3c1, just as manutd4life already explained (however, more usual is 6a3b1c coding).

There can be a vast number of code snippets found on the web, just searching for "run length encoding c++".

-- tesu