how could I reduce tables with same property - entity-framework

I have 7 table and all of them have same property.
my tables : saturday,sunday,monday, .. , friday.
Their property is sixToEight, EightToNine, .... .
Is there anyway to reduce tables?

You can Seprate tables as below
Table 1 - DaysMaster
DayId DayName
1 Saturday
2 Sunday
3 Monday
. ....
Table 2 - ShiftMaster
ShiftId ShiftName
1 sixToEight
2 EightToNine
Table3 - DayShiftMapping
DayShiftMappingId DaysId ShiftId Activity
1 1 1 NULL
2 1 2 eating breakfast
3 2 2 running
here i have created two master table for days and shift and in last table you can insert shift details like daysId - Reference from table 1 and ShiftId - reference from table 2 and add extra column as per your requirement
Hope this help you !

Related

Getting data from alternate dates of same ID column

I've a table data as below, now I need to fetch the record with in same code, where (Value2-Value1)*2 of one row >= (Value2-Value1) of consequtive date row. (all dates are uniform with in all codes)
---------------------------------------
code Date Value1 Value2
---------------------------------------
1 1-1-2018 13 14
1 2-1-2018 14 16
1 4-1-2018 15 18
2 1-1-2019 1 3
2 2-1-2018 2 3
2 4-1-2018 3 7
ex: output needs to be
1 1-1-2018 13 14
as I am begginer to SQL coding, tried my best, but cannot get through with compare only on consequtive dates.
Use a self join.
You can specify all the conditions you've listed in the ON clause:
SELECT T0.code, T0.Date, T0.Value1, T0.Value2
FROM Table As T0
JOIN Table As T1
ON T0.code = T1.code
AND T0.Date = DateAdd(Day, 1, T1.Date)
AND (T0.Value2 - T0.Value1) * 2 >= T1.Value2 - T1.Value1

Remove duplicates based on only 1 column

My data is in the following format:
rep_id user_id other non-duplicated data
1 1 ...
1 2 ...
2 3 ...
3 4 ...
3 5 ...
I am trying to achieve a column for deduped_rep with 0/1 such that only first rep id across the associated users has a 1 and rest have 0.
Expected result:
rep_id user_id deduped_rep
1 1 1
1 2 0
2 3 1
3 4 1
3 5 0
For reference, in Excel, I would use the following formula:
IF(SUMPRODUCT(($A$2:$A2=A2)*($A$2:$A2=A2))>1,0,1)
I know there is the FIXED() LoD calculation http://kb.tableau.com/articles/howto/removing-duplicate-data-with-lod-calculations, but I only see use cases of it deduplicating based on another column. However, mine are distinct.
Define a field first_reg_date_per_rep_id as
{ fixed rep_id : min(registration_date) }
The define a field is_first_reg_date? as
registration_date = first_reg_date_per_rep_id
You can use that last Boolean field to distinguish the first record for each rep_id from later ones
try this query
select
rep_id,
user_id,
row_number() over(partition by rep_id order by rep_id,user_id) deduped_rep
from
table

how do you group query by hours?

I'm trying to query surveys completed every hour in a given day.
the survey table is something like this:
id(SERIAL) - userid(INTEGER) - description - timeTaken(timestamp with time zone)
3 ; 1; "some random description"; "2015-01-17 04:30:24.983576-05"
5 ; 2; "sample about x"; "2015-01-17 04:30:24.983576-05"
7 ; 3; "survey about ducks"; "2015-01-17 05:30:24.983576-05"
basically for a given day lets say March 1st, I want to get all the survey rows grouped by the hour they were taken, i.e 7 rows at 1pm, 3 at 2pm, etc. But I'm not sure if its possible to group like this on pg or if I should do it client end.
EDIT: for the data above have id 3 and 5 grouped under 4 and id 7 grouped for 5. basically I want to display the data seperated by the hours they were completed in.
Thanks
You can use date_part to extract just the hour, which you can have in your group by clause. See http://www.postgresql.org/docs/9.4/static/functions-datetime.html.
By using extarct function in postgresql
for the following sample data
id userid descp timetaken
-- ------ ----------------------- ---------------------------
1 1 some random description 2015-01-17 15:00:24.9835760
2 2 sample about x 2015-01-17 15:00:24.9835760
3 3 survey about ducks 2015-01-17 16:00:24.9835760
4 3 survey about ducks 2015-01-01 19:00:24.9835760
5 3 survey about ducks 2015-01-01 16:00:24.9835760
6 3 survey about ducks 2015-01-01 19:00:24.9835760
I need to get the survey_count per hour in date 01-01-2015
select extract(hour from timetaken) survey_hour
,count(*) survey_count
from sur
where timetaken::date ='2015-01-01'
group by survey_hour

MySQLI, need to merge empty values with same ID into 1 row, and add to same row also if multiple inputs

I have a hard time figuring this one out. And i apologize if the answer is out there, i have searched through all of stackoverflow.
I have an order system, where i order by tableID, and continue to make rows in my Database if someone orders stuff. My problem is getting my data out on 1 row, in this example the table ID, so all the ordered stuff, shows on 1 line only.
I have 3 tables, Food, Drink, Dessert, all with a foreign key in my OrderTable.
id fk_tableid fk_drinkId fk_foodId fk_dessId amount
1 5 2 0 0 2
2 5 0 1 0 1
3 5 0 2 0 1
4 5 0 0 2 2
11 8 1 0 0 2
21 1 1 0 0 5
22 1 0 1 0 9
23 1 0 0 1 2
By a normal select, with left joins, i can get the data out on multiple rows, like this where i get those with tableId 5 and showing the name of the ordered consumable also:
id fk_tableId fk_drinkId fk_foodId fk_dessId amount foodName drinkName dessertName
1 5 2 0 0 2 NULL Sodavand NULL
2 5 0 1 0 1 Lasagne NULL NULL
3 5 0 2 0 1 Pizza NULL NULL
4 5 0 0 2 2 NULL NULL Softice
I tried using group_concat also, which put data on 1 line, but it seems to put everything on 1 line, not just grouped by tableId.
How i want it to be is something like this (the 2x Lasagne for example, is just how i want it to look at the site, but maybe i need to use 1xLasagne twice instead. It would just look messy with 1x Beer 10 times.):
fk_tableId fk_drinkId fk_foodId fk_dessId foodName drinkName dessertName fulllPrice
5 2,2 1,2 2,2 Pizza,Lasagne 2xSodavand 2xSoftice 195
I am aware my question might be wrongly formatted, but i also have a hard time 100% knowing what to google and search for here. I have tried for 2 days, and even asked my teacher who could not do it, he was doing something with CASES and sum(), but it did not work out either.
Any help will be much appreciated!
UPDATE:
Added SQL Query:
SELECT
menukort_ordre.id,
fk_tableId,
fk_drinkId,
fk_foodId,
fk_dessId,
amount,
menukort_food.name AS foodName,
menukort_drink.name AS drinkName,
menukort_dessert.name AS dessertName
FROM menukort_ordre
LEFT Join menukort_drink
ON (menukort_ordre.fk_drinkId = menukort_drink.id)
LEFT Join menukort_food
ON (menukort_ordre.fk_foodId = menukort_food.id)
LEFT Join menukort_dessert
ON (menukort_ordre.fk_dessId = menukort_dessert.id)
WHERE fk_tableid = fk_tableid
With GROUP_CONCAT i tried to do this instead, which put it on 1 row, but due to my WHERE, i get all data on 1 row.
GROUP_CONCAT(menukort_food.name ) AS foodName,
GROUP_CONCAT(menukort_drink.name) AS drinkName,
GROUP_CONCAT(menukort_dessert.name) AS dessertName
UPDATE:
First off I changed your database design since there was no need for 3 tables like that unless you really wanted them to be separated as such. I understand wanting to separate data, but there are times to do so and times not to do it. I understand since my personal database project has me breaking everything up. So the below solution will be based off of my design which is as follows.
Category
Code or ID (PK)
Category
This table will be a lookup table just to make sure drink and food and desert is spelled correctly. Frankly you don't need it unless you need that information specific and want it to be correct.
Next will be the table that stores the drinks, deserts, and food
Items
ID serial
Category
Name
Price
and final the order table that will keep track of the orders
Order
BillID
TableNum
ItemNum (fk)
ID (PK)
This way you can keep track of which table the food goes to and each check or bill. Your design was fine if you wanted to find out how much each table made in a day, but I'm assuming like an actual restaurant you would want to know for each bill. With this you can have multiple orders of a coke or whatever at the same table on the same bill.
Now on to the solution.
This doesn't have the count, but could work on it if you really need it, but frankly I think it is pointless to have a count unless you are going to ungroup the results and have something like this:
tableNum BillNum ItemNum ItemName
1 1 1 Coke
1 1 2 Steak
1 1 3 Pasta
1 1 1 Coke
then you could end up with something like this
tableNum BillNum ItemNum ItemName TimesBy
1 1 1 Coke 2
1 1 2 Steak 1
1 1 3 Pasta 1
The SQL CODE below will give you what you need I believe. I'm using my version of the database and I think you should too just because it is easier and there is no point to having 3 tables for each thing.
CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS (
select BillID, tablenum,ItemNum,Items.name,Items.price
from Orders, Items
where Orders.ItemNum=Items.id
);
create TEMPORARY TABLE IF NOT EXISTS table3 AS (
select SUM(price) as total, BillID
from table2
group by BillID
);
select table3.BillID, TableNum, GROUP_CONCAT(ItemNum order by ItemNum ASC) as ItemNum, GROUP_CONCAT(name order by name ASC) as Item, GROUP_CONCAT(price order by name asc) as ItemPrice, total
from table2, table3
where table2.BillID=table3.BillID
group by BillID;
DROP TABLE IF EXISTS table2;
DROP TABLE IF EXISTS table3;
A few other solutions would be to look into using something like php (programming) to help with this or a stored procedure.
If you need an explanation just ask. Also I'm curious is this for homework or a project? I just want to know why your doing this?
Hope it helps.

tsql sum data and include default values for missing data

I would like a query that will show a sum of columns with a default value for missing data. For example assume I have a table as follows:
type_lookup:
id name
-----------
1 self
2 manager
3 peer
And a table as follows
data:
id type_lookup_id value
--------------------------
1 1 1
2 1 4
3 2 9
4 2 1
5 2 9
6 1 5
7 2 6
8 1 2
9 1 1
After running a query I would like a result set as follows:
type_lookup_id value
----------------------
1 13
2 25
3 0
I would like all rows in type_lookup table to be included in the result set - even if they don't appear in the data table.
It's a bit hard to read your data layout, but something like the following should do the trick:
SELECT tl.type_lookup_id, tl.name, sum(da.type_lookup_id) how_much
from type_lookup tl
left outer join data da
on da.type_lookup_id = tl.type_lookup_id
group by tl.type_lookup_id, tl.name
order by tl.type_lookup_id
[EDIT]
...subsequently edited by changing count() to sum().