case-when

How to use a case-when statement in a mysql stored procedure?

与世无争的帅哥 提交于 2019-12-10 16:54:41
问题 I want to set the session_id automatically using the request_time parameter so i opted for a mysql stored procedure that contains a case statement.Here goes. create procedure upd_userinput(in request_time timestamp, out user_session_id int) begin update user_input; case request_time when time(request_time) < '9:15:00' && time(request_time) > '8:15:00' then set user_session_id = 1; when time(request_time)< '10:15:00' && time(request_time) > '11:15:00' then set user_session_id =2; end case; end

“IN” condition at CASE WHEN on WHERE CLAUSE?

回眸只為那壹抹淺笑 提交于 2019-12-10 16:29:05
问题 I have an complex situation. I want to write an sql query including "case when" condition on "where clause". Just like that: SELECT * FROM <table> WHERE <Column1> in CASE <Column2> WHEN 1 THEN ('OP', 'CL') WHEN 0 THEN ('RE', 'ST') END Column1 must be "in", not "=". Because there is multiple value at condition for Column1. That query returns "Incorrect syntax near ','." error. Can you give me any suggestion? (Sorry for my bad English.) EDIT : I think I misunderstood. If Column2 is 1, condition

correct way to create a pivot table in postgresql using CASE WHEN

陌路散爱 提交于 2019-12-07 03:01:17
问题 I am trying to create a pivot table type view in postgresql and am nearly there! Here is the basic query: select acc2tax_node.acc, tax_node.name, tax_node.rank from tax_node, acc2tax_node where tax_node.taxid=acc2tax_node.taxid and acc2tax_node.acc='AJ012531'; And the data: acc | name | rank ----------+-------------------------+-------------- AJ012531 | Paromalostomum fusculum | species AJ012531 | Paromalostomum | genus AJ012531 | Macrostomidae | family AJ012531 | Macrostomida | order

Sum(Case when) resulting in multiple rows of the selection

久未见 提交于 2019-12-06 14:55:20
I have a huge table of customer orders and I want to run one query to list orders by month for the past 13 months by 'user_id'. What I have now (below) works but instead of only listing one row per user_id it lists one row for each order the user_id has. Ex: one user has 42 total orders over his life with us so it lists his user_id in 42 rows and each row only has one payment. Typically I would just throw this in a pivot table in excel but I'm over the million row limit so I need for it to be right and have had zero success. I would like for the read out to look like this: user_id | jul_12 |

How to show the maximum number for each combination of customer and product in a specific state in Postgresql?

末鹿安然 提交于 2019-12-02 07:42:22
I just begin learning Postgresql recently. I have a table named 'sales': create table sales ( cust varchar(20), prod varchar(20), day integer, month integer, year integer, state char(2), quant integer ) insert into sales values ('Bloom', 'Pepsi', 2, 12, 2001, 'NY', 4232); insert into sales values ('Knuth', 'Bread', 23, 5, 2005, 'PA', 4167); insert into sales values ('Emily', 'Pepsi', 22, 1, 2006, 'CT', 4404); insert into sales values ('Emily', 'Fruits', 11, 1, 2000, 'NJ', 4369); insert into sales values ('Helen', 'Milk', 7, 11, 2006, 'CT', 210); ...... It looks like this: And there are 500

SQL Return 1,0 in new variable based on case when statement referring to multiple other variables

十年热恋 提交于 2019-12-02 04:27:28
I'm trying to create a new variable that populates with a 1 (true), 0 (false) in MySQL based on a series of dates and ladder levels (null-E). See fiddle: http://sqlfiddle.com/#!9/9975e1 Where the record_dates and ladder_levels aren't necessarily in sequential order. I'd like to return the ladder_change fieldvia a case when (?) statement, that says something like: First, look only within matching IDs (i.e. just for ID 324) Then, something like: case when record_date2 > record_date1 AND (ladder_level2 < ladder_level1 OR ladder_level2>ladder_level1) then 1, else 0 Any tips on how to achieve this?

Multiplying two columns which have been calculated on a CASE statement

倾然丶 夕夏残阳落幕 提交于 2019-12-02 03:37:47
问题 I am performing a SQL query in PostgreSQL using a CASE statement like this: SELECT CASE column1 WHEN something THEN 10 ELSE 20 END AS newcol1 CASE column12 WHEN something THEN 30 ELSE 40 END AS newcol2 COUNT(column3) newcol3 FROM table GROUP BY newcol1,newcol2,newcol3 I need a fourth column which has to be the result of newcol2 * newcol3 , how can I do that? If I put (newcol2 * newcol3) AS newcol4 I get a syntax error. 回答1: You can always use a CTE to abstract things away to a different level

MySQL Select Query to generate dynamic column Result

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 08:51:28
I need to write a query that returns a Column Dynamically. For example I have a table tblTest with columns: Id, Name, Type, Amount 1, Receipt, Cash 100 2, Receipt, Card 200 3, Receipt, Cheque 250 4, Receipt, Card 150 5, Receipt, Cash 100 6, Payment, Cash 300 7, Payment, Cheque 400 SQL Query : SELECT Name, SUM(CASE WHEN Type = 'Cash' THEN Amount ELSE 0 END) Cash, SUM(CASE WHEN Type = 'Card' THEN Amount ELSE 0 END) Card, SUM(CASE WHEN Type = 'Cheque' THEN Amount ELSE 0 END) Cheque FROM tblTest GROUP BY Name; it returns me, above result is as per my requirement but in my case Type Cash,Card

MySQL Select Query to generate dynamic column Result

时光怂恿深爱的人放手 提交于 2019-12-01 06:08:16
问题 I need to write a query that returns a Column Dynamically. For example I have a table tblTest with columns: Id, Name, Type, Amount 1, Receipt, Cash 100 2, Receipt, Card 200 3, Receipt, Cheque 250 4, Receipt, Card 150 5, Receipt, Cash 100 6, Payment, Cash 300 7, Payment, Cheque 400 SQL Query : SELECT Name, SUM(CASE WHEN Type = 'Cash' THEN Amount ELSE 0 END) Cash, SUM(CASE WHEN Type = 'Card' THEN Amount ELSE 0 END) Card, SUM(CASE WHEN Type = 'Cheque' THEN Amount ELSE 0 END) Cheque FROM tblTest

How to ORDER BY CASE in Doctrine2 (Symfony2)

你说的曾经没有我的故事 提交于 2019-11-30 03:04:12
I want to run this query by using Doctrine in Symfony 2.3. But it seems like Doctrine does not understand CASE statement. Can anyone help? Thank you in advance! SELECT max(id) id, name FROM cards WHERE name like '%John%' GROUP BY name ORDER BY CASE WHEN name like 'John %' THEN 0 WHEN name like 'John%' THEN 1 WHEN name like '% John%' THEN 2 ELSE 3 END, name Kapil If you are using createQueryBuilder then you can use like $query->addSelect("(CASE WHEN name like 'John %' THEN 0 WHEN name like 'John%' THEN 1 WHEN name like '% John%' THEN 2 ELSE 3 END) AS HIDDEN ORD "); $query->orderBy('ORD', 'DESC'