i would create a particular sql string.
I have this tables :
cities
-----------
city varchar not null primary key
etc
weeks
-----------
week varchar not null primary key
Insert_data
-----------
id int not null primary key
cityID varchar
week varchar
value1 int
etc
I would to bind asp.net gridview with this table structure:
+-----+-------+-------+-------+
| city| week1 | week2 | week3 |
+-----+-------+-------+-------+
| 1 | (y/n) | (y/n) | (y/n) |
| 2 | (y/n) | (y/n) | (y/n) |
| 3 | (y/n) | (y/n) | (y/n) |
| 4 | (y/n) | (y/n) | (y/n) |
| 5 | (y/n) | (y/n) | (y/n) |
+----+-------+-------+-------+
if there are records related to that week in insert_data the value will be Y.
It's possible to create a query string for this?
As always, when you need to create columns from rows with a "group by argument" (city), you can use the PIVOT operator.
http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
Above only applies to MS-SQL, doesn't apply to MySQL.
Or you can do it the old-fashioned way
SELECT
cities.city
,SUM(CASE WHEN weeks.week = 1 THEN Insert_data.value1 ELSE 0 END) AS week1
,SUM(CASE WHEN weeks.week = 2 THEN Insert_data.value1 ELSE 0 END) AS week2
,SUM(CASE WHEN weeks.week = 3 THEN Insert_data.value1 ELSE 0 END) AS week3
,SUM(CASE WHEN weeks.week = 4 THEN Insert_data.value1 ELSE 0 END) AS week4
-- ,etc. for each week (select distinct week from insert_data order by week)
FROM Insert_data
left join cities
cities.city= Insert_data.cityID
left join weeks
on weeks.week = Insert_data.week
group by cities.city
Either way, you need to dynamically hardcode the weeks in your SQL string.
You would now need an additional condition for y and n, and then you can concat the values.
,'(' + CAST(SUM(CASE WHEN weeks.week = 1 AND value1 = 1 THEN 1 ELSE 0 END) as varchar(10))
+ '/'
CAST(SUM(CASE WHEN weeks.week = 1 AND value1 = 0 THEN 1 ELSE 0 END) as varchar(10))
+ ')'
AS week1
or if it's just y or no, then
,
CASE
WHEN SUM(CASE WHEN weeks.week = 1 THEN Insert_data.value1 ELSE 0 END) > 0
THEN 'y'
ELSE 'n'
END AS week1
Related
I want to get the total maximum number of column CODE which the maximum is defined by the last five digits from mybarcode column.
mybarcode | code | judge | create_date |
-------------+------+--------+-------------+
M71X400001 | 7 | pass |
M71X400002 | 7 | pass |
M71X400005 | 7 | pass |
M71X400010 | 7 | pass |
M81X400001 | 8 | pass |
M81X400002 | 8 | pass |
M81X400007 | 8 | pass |
M91X400001 | 9 | pass |
M91X400003 | 9 | pass |
```
Example:
>The maximum value of 7 from CODE column is 10 ( from M71X4'00010')
>The maximum value of 8 from CODE column is 7 ( from M81X4'00007')
>The maximum value of 9 from CODE column is 3 ( from M91X4'00003')
The result should be 10+7+3=20.
And want display in the result table below.
```
SELECT DAY,
SUM(CASE WHEN judge = 'pass' then 1 else 0 end) pass,
SUM(CASE WHEN judge = 'fail' then 1 else 0 end) fail
**??? as number**
from MYTABLE
where MONTH(create_date) = '04' and YEAR(create_date) = '2019'
GROUP BY DAY
Result Table
day | pass | fail | number |
--------+------+--------+----------+
1 | 9 | 0 | 20 |
2 | 9 | 0 | ?? |
3 | 9 | 0 | ?? |
I think you need to do group by two times. Please try below code -
For MySQL -
SELECT
DAY,
SUM(pass),
SUM(fail),
SUM(max_barcode)
FROM (
SELECT
DAY,
SUM(CASE WHEN judge = 'pass' then 1 else 0 end) pass,
SUM(CASE WHEN judge = 'fail' then 1 else 0 end) fail,
Code,
CAST(MAX(SUBSTRING(mybarcode, 5)) AS SIGNED) AS max_barcode
FROM MYTABLE
WHERE MONTH(create_date) = '%s' and YEAR(create_date) = '%s'
GROUP BY DAY, Code
) AS CTE
GROUP BY DAY;
FOR MS SQL Server -
;WITH CTE AS (
SELECT
DAY,
SUM(CASE WHEN judge = 'pass' then 1 else 0 end) pass,
SUM(CASE WHEN judge = 'fail' then 1 else 0 end) fail,
Code,
max_barcode = cast(max(right(mybarcode, 5)) as int)
FROM MYTABLE
WHERE MONTH(create_date) = '%s' and YEAR(create_date) = '%s'
GROUP BY DAY, Code
)
SELECT
DAY,
SUM(pass),
SUM(fail),
SUM(max_barcode)
FROM CTE
GROUP BY DAY;
I have a simple database (using MySql) with which I need to generate a report.
I have a table that schedule_plan which are submitted by users.
and my table userinfo which contain name ,age dll
So I need a report which displays the days as columns as well as the names from a userinfo table.
Report
+------+--------+--------+--------+-------+
| Name | 1 | 2 | 3 | until day 30/31 |
+------+--------+--------+--------+-------+
| Bob | ON | ON | ON | ... |
| Joe | OFF | ON | OFF | ... |
| Jim | ON | ON | ON | ... |
if absence_code in schedule_plan is null then ON if not null OFF
create table attendance
(user_id int,attendance_code varchar(1),dte date)
*/
truncate table attendance;
insert into attendance values
(1,'a','2016-01-01'),
(1,null,'2016-01-02'),
(1,'a','2016-01-03'),
(1,'a','2016-01-04'),
(1,'a','2016-01-05'),
(2,'a','2016-01-01'),
(2,'a','2016-01-02'),
(2,null,'2016-01-03'),
(2,null,'2016-01-04'),
(2,'a','2016-01-05')
;
select a.user_id,
max(case when day(a.dte) = 1 then case when a.attendance_code is not null then 'On' else 'Off' end end) as day1,
max(case when day(a.dte) = 2 then case when a.attendance_code is not null then 'On' else 'Off' end end) as day2,
max(case when day(a.dte) = 3 then case when a.attendance_code is not null then 'On' else 'Off' end end) as day3,
max(case when day(a.dte) = 4 then case when a.attendance_code is not null then 'On' else 'Off' end end) as day4,
max(case when day(a.dte) = 5 then case when a.attendance_code is not null then 'On' else 'Off' end end) as day5
from attendance a
group by a.user_id;
I'm trying to get the total cost from the table below and if Include_Extra is set, use the value in the corresponding Extra_Seat_Cost column. I'm new to mysql, so any help would be greatly appreciated!
+-----------+-----------------+---------------+
| ID | Cost | Extra_Seat_Cost | Include_Extra |
+-----------+-----------------+---------------+
| 3 | 20 | 15 | 1 |
| 4 | 10 | 5 | 0 |
+----+------+-----------------+---------------+
The result should yield total = $45
You can use SUM with CASE WHEN:
SELECT SUM(Cost + CASE WHEN Include_Extra = 1 --if Include_Extra is bool delete = 1
THEN COALESCE(Extra_Seat_Cost,0)
ELSE 0 END) AS total
FROM table_name;
SqlFiddleDemo
I've added COALESCE in case Extra_Seat_Cost can be nullable. number + NULL produces NULL.
If you have grouping column use:
SELECT group_column, SUM(Cost + CASE WHEN Include_Extra = 1
THEN COALESCE(Extra_Seat_Cost,0)
ELSE 0 END) AS total
FROM table_name;
GROUP BY group_column;
cusID | Name | status | Date
---------------------------------
1 | AA | 0 | 2013-01-25
2 | BB | 1 | 2013-01-23
3 | CC | 1 | 2013-01-20
SELECT COUNT(cusID) FROM customer WHERE STATUS=0;
SELECT COUNT(cusID) FROM customer WHERE STATUS=1;
Is there a way of combing such two sql and return the results as one. Because want to avoid calling to DB everytime. I tried UNION of two statments, but only showing one result.
This is the shortest possible solution in MySQL.
SELECT SUM(status = 1) totalActive,
SUM(status = 0) totalInactive
FROM tableName
SQLFiddle Demo
and this is the CASE version
SELECT SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) totalActive,
SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) totalInactive
FROM tableName
SQLFiddle Demo
I have this table [Table 1]
cid | arrived | date_arrived
The [arrived field can have a value of [T] or [F], the value is [F] the date arrived field is NULL
1 records may appear only up to maximum of 2 (1 record for arrived=T and another record for arrived=F) But there are also records that may appear only once
1 | T | 2012-02-01
2 | F | [Null]
1 | F | [Null]
3 | T | 2012-03-05
I need a query that will show something like this
cid | arrived | not_arrived
1 Yes Yes
2 No Yes
3 Yes No
This works:
SELECT
cid,
SUM(arrived = 'T') > 0 as arrived,
SUM(arrived = 'F') > 0 as not_arrived
FROM [Table 1]
GROUP BY cid;
You can try it here: http://sqlfiddle.com/#!2/2b5a7/1/0
try
select cid,
case when find_in_set('T', group_concat(arrived)) > 0 then 'yes' else 'no' end as arrived,
case when find_in_set('F', group_concat(arrived)) > 0 then 'yes' else 'no' end as not_arrived
from table1
group by cid