mercredi 20 juillet 2016

MySql SELECT query with LEFT JOIN falsely returns NULL

I'm trying to get values out of 2 tables in my DB, I used the following code:

    SELECT t1.name, t1.lname, t1.id, t2.grade 
    FROM users as t1 
    LEFT JOIN grades as t2
    ON t1.id = t2.userid and t2.choreid= '7';

It returns good values for : t1.name, t1.lname, t1.id

but where it comes to t2.grade instead of '100' or '50' it always returns NULL.

can anyone see why ?

SQL - error when using UNION

Here's my query:

Select top 1 ProductName, UnitPrice from Products
order by UnitPrice desc

UNION

Select top 1 ProductName, UnitPrice from Products
Where UnitPrice > 0
order by UnitPrice asc

I wanted to display the most expensive and the cheapest products in one table. That's why I used UNION to join the two queries. Unfortunately I get an error

Incorrect syntax near the keyword 'UNION'

That's why I'm asking for your help - what's wrong with my query?

I've tried to convert it to work with Laravel's DB class except it's not returning anything

I am trying to convert my Sql Query to Laravel Query and I am getting Error :?

SQLSTATE[HY000]: General error: 2809 General SQL Server error: Check messages from the SQL Server [2809] (severity 18) [(null)] (SQL: GetRegisteredUserData)

My query1:

SELECT * FROM  RegisteredUser WHERE DateCreated between '$startDate' AND  '$endDate' ORDER BY Yr DESC,MnthNo DESC

My query2:

SELECT * from  UserData where((Yr * 10000 + Mnth * 100 + 1) BETWEEN $startDate AND $endDate) ORDER BY Yr Asc, Mnth Asc

Select data from tables in sql

I have these tables and I want to select my messages and my friends' messages.

CREATE TABLE Friends(
    userId int,
    friendId int,
    CONSTRAINT PRIMARY KEY(userId, friendId)
);

CREATE TABLE Message (
    id       int auto_increment PRIMARY KEY,
    date     datetime,
    text     text,
    urlPhoto varchar(255),
    type     varchar(255),
    idAuthor int,
    idTarget int
);

Assuming my id is 1, it's easy to select my friends' ids:

SELECT friendId FROM Friends WHERE userid=1;

but I can't use that id to select the messages.

mardi 19 juillet 2016

Counting Distinct Across Multiple Columns (Vertica SQL)

Checked other questions without getting a clear answer. I saw a simple answer using MySQL, but it doesn't work in Vertica. Is there an efficient way to count distinct in vertica SQL across multiple columns.

For example, I know in MySQL you can do something like this:

SELECT l.computer, l.user, COUNT(DISTINCT l.computer, l.user) AS count
FROM login l 
GROUP BY l.computer, l.user

Is there a similar syntax in vertica?

Thanks, Ben

ORACLE SQL: how to update a table based in another column?

So, i have two tables, INTERNAMENTO and DOC_ADMISSAO2. i want to update a column from table INTERNAMENTO named DIASINTERNAMENTO with the same values of the table DOC_ADMISSAO2 called DIASADMISSAO, with some conditions..

update INTERNAMENTO a
set a.DIASINTERNAMENTO = (
       select b.DIASADMISSAO
       from DOC_ADMISSAO2 b
       where (a.EPISODIO = b.EPISODIO) AND (a.DATACRIACAO <= b.DATACRIACAO));

It gives me an error:

  1. 00000 - "single-row subquery returns more than one row"

Any advice?

In MySQL how to get the subset of a table ,having only those columns which are of numeric type?

SHOW COLUMNS FROM country ;

Shows information about all the columns in country and its dataTypes.

But following command gives error

SELECT* COLUMNS FROM country where TYPE = INT;

Error:- /* SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'COLUMNS FROM country where TYPE= INT' at line 1 */

What is the right way to do it ?