How i could combinate this two SQL statement? - sql

How i could combinate this two SQL statement. and make them run together at call.
SELECT tovar,kod,
100/SUM(CASE WHEN co='prijem' THEN kusy ELSE NULL END)*
SUM(CASE WHEN co='predaj' THEN kusy ELSE NULL END) as percenta,
SUM(CASE WHEN co='prijem' THEN kusy ELSE NULL END) AS prijate_ks,
SUM(CASE WHEN co='predaj' THEN kusy ELSE NULL END) AS predane_ks
from jednotypredaj WHERE (datum BETWEEN '$a' AND '$b') and (tovar LIKE '%$search%')
group by tovar
ORDER by predane_ks DES
and this statement
"INSERT INTO datadocasne
(fromd,tod)
VALUES
('$a','$b')";
Thanks for help

Put your statements between
BEGIN TRANSACTION
and
COMMIT;

Related

Write Hive queries to see how many missing values you have in each attribute

I want write hive query such that it I can see count of null values of each column
You can use this SQL - this will give you total count, null and not null count.
SELECT
count(*) total_cnt,
sum(case when data_col is null then 1 else 0 end) null_cnt,
sum(case when data_col is null then 0 else 1 end) nonnull_cnt
From mytable

SQL syntax for countif to be inserted

I have a table (tbl2) i have 4 columns with feedback attributes listed. I need to generate a pivot table like syntax in mysql. Need output somewhat like this pivot table. I am currently trying this
INSERT INTO 1 (`BAD/GOOD`, `PRICE YES`, `PRICE NO`, `TOTAL PRICE`) SELECT "BAD",COUNT(*) WHERE tbl2.PRICE="BAD" AND tbl2.Churn="YES",COUNT(*) WHERE tbl2.PRICE="BAD" AND tbl2.Churn="NO",COUNT(*) WHERE tbl2.PRICE="BAD" FROM tbl2_customers_churn
and also tried insert into as values
INSERT INTO 1 VALUES ("BAD",COUNT(*) FROM tbl2 WHERE tbl2.PRICE="BAD" AND tbl2.Churn="YES",COUNT(*) FROM tbl2 WHERE tbl2.PRICE="BAD" AND tbl2.Churn="NO",COUNT(*) FROM tbl2 WHERE tbl2.PRICE="BAD")
for bad and good count separately
Any advise on how to tackle this in SQL?
I think you want logic more like this:
INSERT INTO 1 (`BAD/GOOD`, `PRICE YES`, `PRICE NO`, `TOTAL PRICE`)
SELECT 'BAD',
SUM(CASE WHEN tbl2.PRICE = 'BAD' AND tbl2.Churn = 'YES' THEN 1 ELSE 0 END),
SUM(CASE WHEN tbl2.PRICE = 'BAD' AND tbl2.Churn = 'NO' THEN 1 ELSE 0 END),
SUM(CASE WHEN tbl2.PRICE = 'BAD' THEN 1 ELSE 0 END)
FROM tbl2_customers_churn tbl2;
Or a little more simply:
SELECT 'BAD',
SUM(CASE WHEN tbl2.Churn = 'YES' THEN 1 ELSE 0 END),
SUM(CASE WHEN tbl2.Churn = 'NO' THEN 1 ELSE 0 END),
COUNT(*)
FROM tbl2_customers_churn tbl2
WHERE tbl2.PRICE = 'BAD'

Problems with my WHERE clause (SQL)

I'm trying to write a query that returns the following columns:
owner_id,
number_of_concluded_bookings,
number_of_declined_bookings,
number_of_inquiries
However, the problem is that my WHERE clause messes up the query because I am querying the same table. Here is the code:
SELECT owner_id,
Count(*) AS number_of_cancelled_bookings
FROM bookings
WHERE state IN ('cancelled')
GROUP BY owner_id
ORDER BY 1;
It's easy to retrieve the columns individually, but I want all of them. Say I wanted number_of_concluded_bookings as well, that would mean I'd have to alter the WHERE clause ...
Help is greatly appreciated!
Consider conditional aggregations:
SELECT owner_id,
SUM(CASE WHEN state='concluded' THEN 1 ELSE 0 END) AS number_of_concluded_bookings,
SUM(CASE WHEN state='cancelled' THEN 1 ELSE 0 END) AS number_of_cancelled_bookings,
SUM(CASE WHEN state='declined' THEN 1 ELSE 0 END) AS number_of_declined_bookings,
SUM(CASE WHEN state='inquiries' THEN 1 ELSE 0 END) AS number_of_inquiries
FROM bookings
GROUP BY owner_id

PostgreSQl: select multiple rows and make it in one row

I'm using PostgreSQl and this is my table in database:
how can I write a query that gives me this result :
or you can write query like
select id_salarie,
max(case when date_pointage = '2015-01-01' then round(nb_heures::numeric,2) else null end) as "2015-01-01",
max(case when date_pointage = '2015-01-02' then round(nb_heures::numeric,2) else null end)as "2015-01-02",
max(case when date_pointage = '2015-01-03' then round(nb_heures::numeric,2) else null end) as "2015-01-03"
from my_table where id_salarie = 1
group by id_salarie;
Query looks huge and terrible but works for cubes

SQL query for object and child collection

I need help to write correct SQL query for my DB tables.
Now I have two simple tables:
Is it possible?
SELECT mainobjectid,name,
MAX(CASE WHEN ColumnNAme='ColumnName1' THEN ColumnContent ELSE 0 END)as ColumnName1,
MAX(CASE WHEN ColumnNAme='ColumnName2' THEN ColumnContent ELSE 0 END)as ColumnName2,
MAX(CASE WHEN ColumnNAme='ColumnName3' THEN ColumnContent ELSE 0 END)as ColumnName3
FROM t1 JOIN t2
ON t1.mainobjectid=t2.mainobjectid
GROUP BY t1.mainobjectid
Should work on most popular databases.