dimanche 26 juin 2016

Distinct not working as expected with sql

How does distinct work with the following table:

id  | id2    | time
-------------------
1   | 5555 | 12
2   | 5555 | 12
3   | 5555 | 33
4   | 9999 | 44
5   | 9999 | 44
6   | 5555 | 33

select distinct * from table

SQL Server sp_columns does not return result

I was trying to check the data types of each column. I have tried the code below. use AdventureWorks2014 exec sp_columns Person; However, the result return something like this. PS: I am using AdventureWorks sample database.

MySQL pivot data

I have data. A, STATUS, P A1, 1, P1 A1, 1, P2 A1, 1, P3 A2, 1, P3 A2, 1, P4 A2, 1, P5 A3, 0, NULL I want result same P, A1, A2, A3 P1, 1, 0, 0 P2, 1, 0, 0 P3, 1, 1, 0 P4, 0, 1, 0 P5, 0, 1, 0 How can I do it with mysql query?

Image upload path in MAC style instead of Windows style PHP Ecommerce

I have issues with uploading pictures on my local development environment[enter image description here][1] XAMPP, [What is shows when i use inspection tool to check the directory instead c//; ecom/resources/upload/name of image i get c/recource/enter image description hereuploadname of image Function to addproduct

How to select a particular row of query result

I have a following table in my Oracle database: How can I select the course which is done by most of the students? I am trying multiple variations in following SQL query but it's not working - select count(course) as pcourse, course from studies group by course order by pcourse dec;

SQL Error - has more columns than were specified in the column list

I'm trying to retrieve results from the below query but keep getting this error message: Msg 8158, Level 16, State 1, Line 1 'a' has more columns than were specified in the column list. My query: select * from (select customer_key as customer_id, updated_by as [Help Desk] from permission) a (nolock) where [Help Desk] is not null

Update table1 column if table2.date > NOW()

How can i update specific column in table1 only when table2.date is >= NOW(). I tried

UPDATE table1 JOIN table2 ON (table1.ownerid = table2.ownerid) SET table.test = 'disabled' WHERE FROM_UNIXTIME(table2.dateto) <= NOW();

But its seems to now work and no error at all

Date difference in days with decimal value, and excluding weekends

I would like to get the exact amount of days between two timestamps (ex. 1.45 days). The thing is BigQuery datediff function rounds the day and only accepts 2 timestamp arguments. SELECT datediff(start_time_pac_tz, end_time_pac_tz) as Date_difference Date_difference -6 Also I'm looking to exclude weekends. Any help is greatly appreciated.

MySQL Installer 5.7.13 Internal Error

I am new to mysql. I have downloaded mySql installer 5.7.13 of 320.2mb size. While installing i get error Internal Error(error retrieving product version) the installer will now close. screenshot Please can you help me to fix this.

Thanks in advance!

Use update statement in where clause

Is it possible to use an UPDATE statement within the WHERE clause of a SELECT statement?

I would like to execute something like this but it doesn't work:

SELECT * FROM table1 WHERE col1 < (UPDATE table1 SET col2='test' WHERE id=1)

I use mysql_query in PHP.

SQL command to view next command used

Is there a command like F9 which goes to previous command to the next command or a way to view all commands used? Using the retrieve command in zeus to go back to the command that was just used, how would i go to forward after using the F9 when i've gotten to far. Instead of restarting my search.

Check Before Inserting

I need to check a value before inserting. However for some reason I can't figure it out. Here's my code:

set @accID = (select id from table2); 

IF @accID IS NOT NULL THEN
    INSERT INTO task
    (account_id)
    VALUES (@accID);
END IF;

What is wrong with the code above as it shows invalid sql syntax error?

grouping a datetime column only by time

I have a column datedate format, with 15 min intervals and another column called datavalues with corresponding data. I want to roll up the data values to 1 hr. I am attaching a screen shot. enter image description here. So my new column should have the aggregated value of "DATA_VALUES" column. and the datetime column should represent just hours but not 15 min. Please help, I have tried cast() to convert to time but not able to proceed further.

mysql group by and order by id breaks

I have a table with columns id, q_id, Question, Answer, Q_Date

I have made a query which concatenates multiple rows having same q_id.

Here is the query:

select q_id, Question, Link, Q_Date,
    GROUP_CONCAT(Answer SEPARATOR 'n') as Answer
    from ask
group by q_id, Question, Link, Q_Date

What I want to do is order by the id column, but when I select id column, it shows all the rows, UnConcatenated.

please help

Php Select Statement works with id as record selector but will not work if I use a different column as a selector

Php Select Statement works with id(with unique values) as record selector but will not work if I use a different column(with unique values) as a selector

THIS WORKS

$Idart = "4";
$sql2 = "SELECT * FROM articles where id in ({$Idart})";
$results2 = $conn->query($sql2);
$row2 = $results2->fetch_assoc();

THIS DOES NOT WORK

$Idart = "5-6142-8906-6641";
$sql2 = "SELECT * FROM articles where IDStamp in ({$Idart})";
$results2 = $conn->query($sql2);
$row2 = $results2->fetch_assoc();

I've tried a variety of different things with MYSQL including deleting "id" column and making "IDStamp" the primary key. Any thoughts appreciated.

SUM two different tables columns in SQL ACCESS

Trying to figure out how to get this JOIN to work properly. Been sitting here for about 30 minutes. Can someone help me out? I am trying to subtract one form the other, to see the difference between invoice quantity and inventory volume.

SELECT Invoice.NameOfItem, SUM(Inventory.Volume - Invoice.Quantity) As TotalNeeded
FROM Invoice
INNER JOIN Inventory
ON Invoice.NameOfItem=Inventory.NameOfItem
GROUP BY Invoice.NameOfItem;

The issue is the output is incorrect.

 SELECT NameOfItem, SUM(Quantity) AS TotalNumberNeeded From Invoice GROUP BY NameOfItem

subtracted from

 SELECT NameOfItem, SUM(Volume) AS TotalNumberNeeded From Inventory GROUP BY NameOfItem

Is = -112. The output is currently "992"

Output results in a single table in console

I'm running the below code using POSTGRESQL in Jetbrains. I'm trying to output the results in a neat 2 column table (QUARTER, RESULTS) INSIDE the console. When I run the below code, it comes back, but in separate tables, making it annoying to have to consolidate the results from each. Is there a way to get multiple results in the same table so I can copy and paste the results INSIDE the console? Thank you

I'm running

SELECT COUNT(DISTINCT  CUSTOMER)  as Q216 FROM(
SELECT  *
FROM    TABLE
WHERE CUSTOMER IN ( SELECT   CUSTOMER
                           FROM     temp_08.Unemployment
                           WHERE TRANSACTION_DATE > '3/31/2016'))



SELECT COUNT(DISTINCT  CUSTOMER)  as Q116 FROM(
SELECT  *
FROM    temp_08.COF
WHERE CUSTOMER IN ( SELECT   CUSTOMER
                           FROM     temp_08.Unemployment
                           WHERE TRANSACTION_DATE between '12/31/2015' and '3/31/2016'))

Mysql to use all cores 20 cores I have

I am currently setting up a website that uses a mysql database having more than 4 crore rows of data. I am using one server for files and separate mysql server. Two servers are having 20 core, 64 gb ram. Meanwhile I could see while executing longest mysql queries my mysql server is using two cores maximum hence it is taking more than 38 seconds to execute my longest query. see the result below.

User@Host: dev_data @ localhost []

Query_time: 38.113460 Lock_time: 0.000514 Rows_sent: 10 Rows_examined: 48683733

How can i configure my mysql server so that it uses all the 20 cores to handle the mysql ? How can I achieve this ? Mysql version using 5.6

Atop result below.

enter image description here

samedi 25 juin 2016

Python, Flask -- count page visit and write in database

There is a small hello world Flask with visit statistics. I managed to count visits with redis, but I also have to add support storing date and number of visits in MySQL database. The current code is: I was trying to access previously created database. Currently I have no idea how to store such info in MySQL.

I was trying to access previously created database. Currently I have no idea how to store such info in MySQL.

from flask import Flask
from redis import Redis

app = Flask(__name__)
redis = Redis(host="redis")
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = 'user'
app.config['MYSQL_DATABASE_PASSWORD'] = 'passwd'
app.config['MYSQL_DATABASE_DB'] = 'hello'
app.config['MYSQL_DATABASE_HOST'] = 'mysql'
mysql.init_app(app)

@app.route("/")
def hello():
    visits = redis.incr('counter')
    html = "<h3>Hello, world!</h3>" 
           "<b>Visits:</b> {visits}" 
           "<br/>"
    return html.format(visits=visits)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=80)

How this can be solved? I will be grateful for any suggestions.

PostgreSQL backdating query

I am trying to write a query that will return counted records from the time they were created. The primary key is a particular house which is unique. Another variable is bidder. The house to bidder relationship is 1:1 but there can be multiple records for each bidder (different houses). Another variable is a count (CASE) of results of previous bids that were won. I want to be able to set the count to return the number of previous bids won at the time each house record was created. Currently, my query logs the overall number of previous bids won regardless of the time the house record was created. Any help would be great! Example:

SELECT h.house_id,
    h.bidder_id,
    h.created_date,
    b.bids_won
FROM house h
LEFT JOIN bid_transactions b
ON h.house_id = b.house_id
LEFT JOIN (
SELECT bidder_id,
    COUNT(CASE WHEN created_date IS NOT NULL AND transaction_kind = 'Successful Bid' THEN 1 END) bids_won
FROM bid_transactions
GROUP BY user_id
) b
ON h.bidder_id = b.bidder_id
ORDER BY j.created_date DESC